Showing posts with label arduino. Show all posts
Showing posts with label arduino. Show all posts

October 21, 2014

A substitute for the DELAY function - Arduino

Hello guys, how are you doing today? 

   Ever since I came across the Arduino platform, I felt really pissed off with (the existence) of the delay() function, because it was created with the (evil) solo purpose of wasting time between executions of the loop() function. 
   I have rapidly created a counter to substitute this function in every sketch of my own, with the downside of having to adjust the counted number for every single program (because different program sizes take different times to be executed). This scenario have just changed, because I am presenting you my very own professional way to not waste precious time of your Arduino. It is a sketch that allows you to run a piece of code every 'x' microseconds while leaving the rest of the time free for execution of more code!. 
   The code takes advantage of the micros() function, present in the stock Arduino language and IDE, which returns the time (in microseconds) that Arduino has been running since the last reset. Essentially what I do is to execute my main function (which I can freely choose) only every given time; this "given" time can be adjusted by updating its value (in microseconds) in an IF loop.

   The sketch is available on my GitHub and doesn't require any special library or installation, only the stock micros() function. Essentially your custom code, such as blinking leds, goes in between the quotes I have wrote (as seen below): 



   The main advantage in this code is that you can execute a code every 'x' microseconds (utilizing the sketch exactly as it is) or multiple codes every 'y' microseconds (by adding new IF conditions based on the same couter). You can not forget to fill your setup() function as needed; your timing will always be respected, since you don't write a code big enough (that takes more time than your delay) to be executed. 
   
   I would greatly appreciate to hear from you guys, any suggestion, criticism, upsides, downsizes, anything that can make the code work better. You can reach me on twitter @embedded_clovis or email: clovisf AT gmail DOT com .  


September 05, 2014

Beta-testing Arduino-ZERO

Hi guys, this is just an update post to tell you two things: 

1- I will be back to this blog once I go forward on my "chip tag for running clubs" project and once the things stabilize in the college (beginning of a new term is always crazy!).

2- I was selected as one of the Arduino-ZERO (link here) beta-testers! It has been so nice and so crazy test everything on that board, you have no idea. I have signed a waiver saying that I cannot share any information related to the platform, so I will only be telling you things related to this project when the beta-testing is over (and the product releases).

Stay tuned for news, and happy coding and hacking!! :-)


July 03, 2014

A cheap Chip Tag for every Running club

Hello folks, 

   Today's post is about my entry to the Hackaday Prize, a competition that will take its winner to space :) . The objective of the competition is to make (and show the world how you did) projects that are in some way "connected": any kind of communication is valid. There is already a plenty of projects being showed there, and I think mine is a bit special: it is related to something that I love doing and will help me doing it better.
   I am talking about road Running; I have been running for three years now and I always feel frustrated when there is no chip-timing in the small races I participate, or even in trail or stairs races. Of course everyone can have a GPS-enable Cellphone or wrist-watch, but the first ones are not always comfortable to carry and the last one in not cheap enough for everyone to have it.
   So I came up with a hack solution (link here) that may revolutionize small running communities and clubs: An Arduino-sized device that reads RFID tags and communicates to any Android cellphone via Bluetooth. So every runner will only carry a small key-chain sized RFID tag and all the processing is done into the reader, that will finally send runner's time and name to an App to be developed on Android.


   The actual schematic for the project is seen above; I haven't started putting the hardware together yet because the parts are still being shipped to me, but have started the programming, that will probably be done into a Freescale FRDM-K64F board (features an ARM Cortex-M4 inside). 
   More details to come as I advance into the project. Get updated on my Google Plus: +Clovis Fritzen and my Twitter: @ClovisDuino. See you guys later, 

June 27, 2014

Leds and Buttons - Freescale FRDM-K64F

Hi makers, programmers and curious :)

   This post will be about my first contact with the ARM technology, by means of my newly arrived Freescale FRDM-K64F development board. As a good Arduino user the first thing I wanted to do with my board was to blink the RGB led it features onboard, so I did it!. The code I developed can be found here . I got a lot of help and examples from the mBed online compiler.

my first ARM-based project

   The blinking LED example from mBed website uses a "wait()" function which I don't like (because wastes processing time), so I developed my own code based on a counter that controls the "entering or not" in the blinking function. It is possible to notice in the gif image above that all the led colors are being lit, one after another; I was satisfied with the results, so I moved to the next step: adding buttons to it!. 
--------------------------------------------

   Again my code is very simple (and can be found here): when no buttons are pressed the blue LED is on; when the left button is pressed the LED turns green and for the right button the LED turns red. It took me only a few minutes to write, compile and test both circuits!.

buttons and LED's on my ARM board

I hope you guys are enjoying my blog, and I surely want to here from you. See you all soon.

My project featured on SeeedStudio

Hello Makers,

   Last Tuesday, June 24th I had a project featured in a contest of Seeed Studio website: my 6-bit Arduino thermometer, that can be seen here. The contest consists of sharing your first maker project, and you can get $5 in credits to use in SeeeD Studio website. I found their initiative very nice, because it encourages other to become makers just like me, and turn the world into a better place.


   The project inside my blog: here
   The project on Seeed Studio: here

June 24, 2014

Second project - Temperature + humidity + LCD!

Hello coders, hackers, makers and curious visitors!

   The second project I worked on is a "temperature and humidity sensor with a LCD display" based on Arduino, the DHT11 serial temperature/humidity sensor, the LM35 linear temperature sensor and a 16x2 LCD screen. The project is similar to this one and features readings of temperature and humidity that are shown in a LCD display and sent via Arduino (USB- serial) to any PC terminal. Below I show a picture of the project working:

The prototype, when compared to Google Weather (upper right corner)

   The serial console (on arduino IDE) can be seen below, capturing successive readings from Arduino serial:

Screenshot of data captured through serial

   The complete schematic of this prototype is in the picture below; it was developed using Fritzing; You can download the schematic here



   The project was coded inside the Arduino IDE using some third-part codes, which I put together and made it work. The original codes are: DHT11 Library and LiquidCrystal . My code is available in the box below, as well as in this Github link.

/*
Modified by Clovis Fritzen in June 11th, 2014:
- The original program read the serial temperature/humidity sensor DHT11 (which is still part of this program);
- "Delay" function substituted by a counter (when the counter overflows the program enters the serial routine)
- Added the LM35 (10mV/C temperature sensor) reading via serial, to compare both sensors
*/
/*
Board int.0 int.1 int.2 int.3 int.4 int.5
Uno, Ethernet 2 3
Mega2560 2 3 21 20 19 18
Leonardo 3 2 0 1
Due (any pin, more info http://arduino.cc/en/Reference/AttachInterrupt)

This example, as difference to the other, make use of the new method acquireAndWait()
*/

#include
#include

int idDHT11pin = 2; //Digital pin for comunications
int idDHT11intNumber = 0; //interrupt number (must be the one that use the previus defined pin (see table above)
long count= 0; // counter for spacing the serial writings
long AcqPeriod= 0;
int sensorPin = A5; // input pin for LM35 (10mV/C)
int sensorValue = 0; // variable to store the LM35 temperature
int led= 13;


//declaration
void dht11_wrapper(); // must be declared before the lib initialization

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(9, 8, 5, 4, 3, 6);

// Lib instantiate
idDHT11 DHT11(idDHT11pin,idDHT11intNumber,dht11_wrapper);

void setup()
{
pinMode(led, OUTPUT);
Serial.begin(9600);
Serial.println("DHT11 temp/humidity sensor - Example program");
Serial.print("Lib version: ");
Serial.println(IDDHT11LIB_VERSION);
Serial.println("modified by Clovis Fritzen");
Serial.println("---------------");

// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(" DHT:|LM35:|HUM:");
}
// This wrapper is in charge of calling
// mus be defined like this for the lib work
void dht11_wrapper() {
DHT11.isrCallback();
}
void loop()
{

AcqPeriod= 400000; // set here the interval in which data is sent into serial

if (count < AcqPeriod) // Still need to make sure what this number represents in terms of
// seconds or cycles of clock
{
count= count++;

if (count < (AcqPeriod/2)) // just a routine to blink a led :)
{
digitalWrite(led, HIGH);
} else{
digitalWrite(led, LOW);
}
}
else
{

//Serial.print("\nRetrieving information from sensor: ");
Serial.print("\nRead sensor: ");
//delay(100);
count= '0';

sensorValue = analogRead(sensorPin);
sensorValue= sensorValue/2;

int result = DHT11.acquireAndWait();
switch (result)
{
case IDDHTLIB_OK:
Serial.println("done");
break;
case IDDHTLIB_ERROR_CHECKSUM:
Serial.println("Error\n\r\tChecksum error");
break;
case IDDHTLIB_ERROR_ISR_TIMEOUT:
Serial.println("Error\n\r\tISR time out error");
break;
case IDDHTLIB_ERROR_RESPONSE_TIMEOUT:
Serial.println("Error\n\r\tResponse time out error");
break;
case IDDHTLIB_ERROR_DATA_TIMEOUT:
Serial.println("Error\n\r\tData time out error");
break;
case IDDHTLIB_ERROR_ACQUIRING:
Serial.println("Error\n\r\tAcquiring");
break;
case IDDHTLIB_ERROR_DELTA:
Serial.println("Error\n\r\tDelta time to small");
break;
case IDDHTLIB_ERROR_NOTSTARTED:
Serial.println("Error\n\r\tNot started");
break;
default:
Serial.println("Unknown error");
break;
}

lcd.setCursor(7, 1);
// print the number of seconds since reset:
lcd.print(sensorValue);

Serial.print("Humidity (%): ");
Serial.println(DHT11.getHumidity(), 0); // ", 0" represents the number of decimal positions after the comma

Serial.print("DHT11 Temp (oC): ");
Serial.println(DHT11.getCelsius(), 0); // ", 0" represents the number of decimal positions after the comma

lcd.setCursor(13, 1);
// print the number of seconds since reset:
lcd.print(DHT11.getHumidity(), 0);

Serial.print("LM35 Temp (oC): ");
Serial.println(sensorValue);


//Serial.print("Temperature (oF): ");
//Serial.println(DHT11.getFahrenheit(), 2);

//Serial.print("Temperature (K): ");
//Serial.println(DHT11.getKelvin(), 2);

//Serial.print("Dew Point (oC): ");
//Serial.println(DHT11.getDewPoint());

//Serial.print("Dew Point Slow (oC): ");
//Serial.println(DHT11.getDewPointSlow());

// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(1, 1);
// print the number of seconds since reset:
lcd.print(DHT11.getCelsius(),0);

}

}

   I just want to share some notes with you:
- Both the DHT11 and LM35 have a "step" (or resolution) of one degree Celsius, meaning they cannot read something like "26.3"; They a only capable of reading numbers with no decimal (e.g: "26").
- The contrast of the LCD screen can be adjusted by means of the potentiometer; Its backlight is always on (last two pins of the LCD on the schematic above).
- The circuit works independently on the presence of the USB cable on Arduino; it means that it will work on batteries for example!. in this case only the serial readings will be missing.
- The is a blinking led on pin 13 of Arduino (which is assemble on the board); That one is there just for fun.

   I hope you guys are enjoying my posts so far, and trying to validate my projects by doing it yourselves. If you have any suggestions/ recommendations or critics, please feel free to talk to me on Twitter, Google Plus and Facebook, as well as in the comments below. See you all next time!.

June 18, 2014

First project - Arduino Binary Thermometer with LM35

Hello folks! 

   As a first project post I will show you the steps I take to develop a binary thermometer with a LM35 temperature sensor. It is a bit similar to this one from instructables, but mine is arduino-powered. My intention on this project was simply to test the power of arduino (UNO R3) on handling one of its ports (6 bits) at a time, and also the efficacy of the LM35 digital temperature sensor.

The prototype schematic

   The schematic I created was drawn in Fritzing. My code (also available below) can be found in GitHub ( here). It is important to notice that the limits of temperatures that can be read are NOT the ones of the LM35, instead they are limited by the Arduino power supply voltage (0 +5V): so you are able to read  temperatures between +2 and +150 degrees Celsius.

// Arduino timer CTC interrupt example (timer), from www.engblaze.com
// This example is a binary meter - shows a analog value in a binary (6-bit) output
// Modified by Clovis Fritzen, to fit as a binary thermomter (in may/2014)

// avr-libc library includes
#include < avr/io.h >
#include < avr/interrupt.h >

int sensorPin = A0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor (LM35)
byte binaryValue= B00000000; // initialize port value as zeros

void setup()
{
DDRB = DDRB | B00111111; // set 6 pins in port B as outputs

// initialize Timer1
cli(); // disable global interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B

// set compare match register to desired timer count:
OCR1A = 15624; // for temperature
// turn on CTC mode:
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler:
TCCR1B |= (1 << CS10);
TCCR1B |= (1 << CS12);
// enable timer compare interrupt:
TIMSK1 |= (1 << OCIE1A);
// enable global interrupts:
sei();
}

void loop()
{
// You would put another stuff program here
}

ISR(TIMER1_COMPA_vect)
{
// read the value from the sensor :
sensorValue = analogRead(sensorPin);
//digitalValue= sensorValue;
binaryValue= byte(sensorValue/2);
PORTB = binaryValue;
}


   Some notes:
- The LM35 sensor is connected to the analog input A0;
- I put three colors of leds only to give an impression of "the warmer the color, the warmer the ambient (since the MSB is a red led, connected to pin 13 of arduino);
- I have only tested it indoors so far, and it showed to be accurate enough for a sensor like that, when compared to a DTH11 temperature sensor. 

I hope you have liked it, and I promised I will try to put some videos of the actual circuits working, for the next experiments.