Arduino UNO Tutorial 3 - Timing

Arduino UNO Tutorial 3 - Timing

Moving on from Tutorial 1 where we flashed an LED on for a second then off for a second in a continuous loop.

The program is shown below and has one major flaw. We are using the delay() function to delay the processor for 1 second before toggling the LED on or off. While it is waiting, the microcontroller cannot perform any other functions in the main loop().

 

/* Flashing LED
 * ------------
 * Turns on and off a light emitting diode(LED) connected to a digital    
 * pin, in intervals of 2 seconds.  *  
 */ 
int ledPin = 13; // LED connected to digital pin 13 
void setup() {
   pinMode(ledPin, OUTPUT); // sets the digital pin as output 
} 
void loop() {
   digitalWrite(ledPin, HIGH); // sets the LED on
   delay(1000);                // waits for a second
   digitalWrite(ledPin, LOW);  // sets the LED off
   delay(1000);                // waits for a second 
}

First, lets make a quick change to the loop() function. We will replace the two pairs of statements which turn the LED on and off with just one pair. Instead of setting the value to HIGH and then to LOW we will read it's current value and set it to the opposite using the NOT function !

void loop()
{
  digitalWrite(ledPin, !digitalRead(ledPin));   // toggles the LED on/off
  delay(1000);                  // waits for a second
}

OK, now lets improve on the delay() functions. Instead we will use the Arduino millis() function.
The millis() function returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days. So, as long as you are not leaving your device on for more than 50 days, this is a great way to perform timing functions. If your device is intended to be left on for longer than 50 days, you will have to account for the rollover in your code.

Alternatively for faster timings the micros() function can be used. This returns the number of microseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 70 minutes.

We will use the millis() function to control our LED as before. The code for this is shown below

/* Flashing LED Version 2
 * ------------------------
 *
 * Turns on and off a light emitting diode(LED) connected to a digital  
 * pin, in intervals of 2 seconds using millis() function
 *
 */
int ledPin = 13;                 // LED connected to digital pin 13
unsigned long currentTime;
unsigned long loopTime;

void setup()
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  currentTime = millis();
  loopTime = currentTime;  
}

void loop()
{
  currentTime = millis();
  if(currentTime >= (loopTime + 1000)){  
    digitalWrite(ledPin, !digitalRead(ledPin));   // toggles the LED on/off
    loopTime = currentTime;  // Updates loopTime
  }
  // Other processing can be done here
}

This sketch uses two additional variables currentTime and loopTime.
In the setup() function both variables are initially set to the same value
In the main loop() function, currentTime is updated everytime through the loop.
Only when at least one second has passed and the currentTime variable is greater than loopTime + 1000 (1000 milliseconds = 1 second) does it then toggle the ledPin value and also reset loopTime to the same value as currentTime.

Notice there is no use of the delay() function so the processor is not tied up waiting and can carry out any additional processing in the main loop that is needed

Tutorial 2 - Servos

Tutorial 4 - Simple Cylon

Shopping Cart
0 items
 
DELIVERY/PAYMENT

   UK ORDERS OVER £50

UK - £2.40 orders below
                    £50.00
EU Airmail from £3.95
Worldwide from £7.95
More Details...

 
Specials
 
Brands
 
 Check out our videos