Coding Timers and Delays in Arduino

Introduction

Don't use delay( )

Using delay() causes your system to be stuck while waiting for the delay to expire. However replacing delays requires some care. This page explains in a step by step way how to replace Arduino delay() with a non-blocking version that allows you code to continue to run while waiting for the delay to time out.

Here are a number of simple sketches each of which turn a Led on when the Arduino board is powered up (or reset) and then 10sec later turns it off. The first one is an example of how you should NOT write the code. The second is an example of code the works and the third is an example of using the millisDelay library to simplify the code. There are also examples of single-shot and repeating timers.

If you already understand why you should not use delay() and are familiar with Arduino, the importance of using unsigned longs, overflow and unsigned subtraction, then you can just skip to Using the millisDelay library (Step 4)

The millisDelay library provides functionality delays and timers, is simple to use and easy to understand for those new to Arduino.

This instructable is also on-line at How to code Timers and Delays in Arduino

Step 1: How Not to Code a Delay in Arduino

Here is how NOT to code a delay in a sketch.

int led = 13;<br>bool ledOn;<br>
void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);   // initialize the digital pin as an output.
  digitalWrite(led, HIGH); // turn led on
  ledOn = true; // led is on
}<br>
void loop() {
  if (ledOn) {
    delay(10000);
    digitalWrite(led, LOW); // turn led off 
    ledOn = false;  // prevent this code being run more then once
    Serial.println("Turned LED Off");
  }
//  Other loop code here . . .
  Serial.println("Run Other Code");
}

In the setup() method, which Arduino calls once on starting up, the led is turned on. Once setup() is finished, Arduino calls the loop() method over and over again. This is where most of you code goes, reading sensors sending output etc. In the sketch above, the first time loop() is called, the delay(10000) stops everything for 10secs before turning the led off and continuing. If you run this code you will see that the Run Other Code is not printed out for 10sec after the startup, but after the led is turned off (ledOn equals false) then is printed out very fast as loop() is called over and over again.

The point to note here is that you really should not the delay() function at all in the loop() code. It is sometimes convenient to use delay() in the setup() code and you can often get away with very small using very small delays of a few milliseconds in the loop() code, but you really should avoid using the at all in the loop() method

Step 2: How to Write a Non-blocking Delay in Arduino

The previous sketch used a blocking delay, i.e. one that completely stopped the code from doing any thing else while the delay was waiting to expire. This next sketch shows you how to write a non-blocking delay that allows the code to continue to run while waiting for the delay to expire.

int led = 13;<br>unsigned long delayStart = 0; // the time the delay started
bool delayRunning = false; // true if still waiting for delay to finish<br>
void setup() {
  pinMode(led, OUTPUT);   // initialize the digital pin as an output.
  digitalWrite(led, HIGH); // turn led on<br>
  delayStart = millis();   // start delay
  delayRunning = true; // not finished yet
}<br>
void loop() {
  // check if delay has timed out after 10sec == 10000mS
  if (delayRunning && ((millis() - delayStart) >= 10000)) {
    delayRunning = false; // // prevent this code being run more then once
    digitalWrite(led, LOW); // turn led off
    Serial.println("Turned LED Off");
  }
//  Other loop code here . . .
  Serial.println("Run Other Code");
}

In the sketch above, in the setup() method, the delayStart variable is set to the current value of millis().

millis() is a built-in method that returns the number of milliseconds since the board was powered up. It starts as 0 each time the board is reset and is incremented each millisecond by a CPU hardware counter. More about millis() later. Once setup() is finished, Arduino calls the loop() method over and over again.

Each time loop() is called the code checks
a) that the delay is still running, and
b) if the millis() has move on 10000 mS (10sec) from the value stored in delayStart.

When the time has move on by 10000mS or more, then delayRunning is set to false to prevent the code in the if statement being executed again and the led turned off.

If you run this sketch, you will see Run Other Code printed out very quickly and after 10sec the Led will be turned off and if you are quick you might just see the Turned LED Off message before it scrolls off the screen.

See Step 4, below for how the millisDelay library simplifies this code

Step 3: Unsigned Long, Overflow and Unsigned Subtraction

If you are familiar with unsigned longs, overflow, unsigned arithmetic and the importance of using an unsigned long variable, then you can just skip to Step 4 Using the millisDelay library.

The important part of the previous sketch is the test

(millis() - delayStart) >= 10000 

This test has to be coded in this very specific way for it to work.

Unsigned Long and Overflow

The delayStart variable and number returned from the millis() built-in function is an unsigned long. That is a number from 0 up to 4,294,967,295.

If you add 1 to an unsigned long holding the maximum value of 4,294,967,295 the answer will be 0 (zero). That is the number overflowed and wrapped around back to 0. You can imagine the overflow bit just gets dropped. e.g. in a 3 bit unsigned 111 is the maximum value (7) adding 1 gives 1000 (8) but the leading 1 overflows the 3 bit storage and is dropped so wrapping back to 000.

This means, eventually, when the cpu adds one more it variable holding the millis() result it will wrap around to 0. That is millis() will start counting from 0 again. This will happen if you leave your Arduino board running for 4,294,967,295mS i.e. about 49day 17hrs, say 50days.

Now let's consider another way of coding the test (millis() - delayStart) >= 10000

Arithmetically this test is equal to millis() >= (delayStart + 10000)

However if you start the delay after almost 50 days, for example when millis() returns 4,294,966,300 mS, then delayStart + 10000 will overflow to 995 and the test, millis() >= (delayStart + 10000), will immediately be true and there will be no delay at all. So this form of the test does not always work.

Unfortunately you are unlikely to come across this during your testing, but for it may crop up un-expectedly in a long running device, like a garage door control the runs continually for months. You will get a similar problem if you try and use
delayEnd = millis() + 10000
and then the test (millis() >= delayEnd)

Finally the delayStart variable must be an unsigned long. If you instead use a long (i.e. long int) or int or unsigned int , the maximum value they can hold is smaller than the unsigned long returned from millis(). Eventually the value retuned from millis() will overflow the smaller variable it is being stored in and you will find time has suddenly gone backwards. For example, if you use an unsigned int for startDelay, this will happen after 65 secs on an Uno board.

Unsigned Subtraction

One other point of interest is what happens to result of millis() - delayStart when delayStart is say 4,294,966,300 and we want a 10000mS delay.

millis() will wrap around to 0 before that happens. Remember that adding 1 to the maximum value an unsigned long can store wraps around back to 0. So one way of looking at calculating millis() - delayStart, where millis() has wrapped around and is smaller then delayStart, is to say "What number do I have to add to delayStart to equal millis() (after overflow)?" i.e. what is X in the equation delayStart + X == millis()

For example using a 3 bit unsigned variable again, to calculate 2 – 4 (unsigned), think of a clock face starting at 0 and adding 1 all the way round to 111 (7) and then back to 0. Now to get from 4 to 2 you need to add 6 (5,6,7,0,1,2) so 2-4 = 6 and this is in effect how the calculation works, although the CPU will perform the calculation differently.

So the difference of two unsigned longs will always be a positive number in the range 0 to 4,294,967,295. For example if startDelay is 1 and millis() has wrapped around to 0 (after 50 days) then millis() - startDelay will equal 4,294,967,295. This means that you can specify a DELAY_TIME anywhere in the range 0 to 4,294,967,295 mS and (millis() - delayStart) >= DELAY_TIME will always work as expected, regardless on when the delay is started.

Step 4: Using the MillisDelay Library

To install the millisDelay library. Downloaded the millisDelay.zip file.

Unzip this file to your Arduino/libraries directory (open the IDE File->preferences window to see where your local Arduino directory is). Some times the instructions for How to Install a Library - Automatic installation work, but not always. Unzipping the file manually is safest. Once you install the millisDelay library, there will be three interactive examples available that you can load on your Arduino board and then open the Serial Monitor (within 5sec) at 9600baud to use them. Here is the previous non-blocking delay sketch re-written using the millisDelay library.

#include <millisDelay.><br>int led = 13;
millisDelay ledDelay;<br>
void setup() {
  pinMode(led, OUTPUT);   // initialize the digital pin as an output.
  digitalWrite(led, HIGH); // turn led on 
<br>  ledDelay.start(10000);  // start a 10sec delay
}
<br>void loop() {
  // check if delay has timed out
  if (ledDelay.isFinished()) {
    digitalWrite(led, LOW); // turn led off
    Serial.println("Turned LED Off");
  }
//  Other loop code here . . .
  Serial.println("Run Other Code");
}</millisdelay.h>

If you look at the millisDelay library code you will see that the previous sketch's code has just been moved to the start() and isFinished() methods in the library.

Is this a ledDelay or a ledTimer? You can use which ever term you like. I tend to use ...delay for single-shot delays that execute once and use …timer for repeating ones.

Step 5: Delay and Timer Examples

Here are two basic delay and timer sketches and their millisDelay library equivalents. These examples are for a once off (single-shot) delay and a repeating delay/timer.

Single-Shot Delay

A single shot delay is one that only runs once and then stops. It is the most direct replacement for the Arduino delay() method. You start the delay and then when it is finished you do something. BasicSingleShotDelay is the plain code and SingleShotMillisDelay uses the millisDelay library.

BasicSingleShotDelay

This sketch is available in BasicSingleShotDelay.ino

int led = 13; // Pin 13 has an LED connected on most Arduino boards.

unsigned long DELAY_TIME = 10000; // 10 sec unsigned long delayStart = 0; // the time the delay started
bool delayRunning = false; // true if still waiting for delay to finish void setup() { pinMode(led, OUTPUT); // initialize the digital pin as an output. digitalWrite(led, HIGH); // turn led on
// start delay delayStart = millis(); delayRunning = true; }
void loop() { // check if delay has timed out if (delayRunning && ((millis() - delayStart) >= DELAY_TIME)) { delayRunning = false; // finished delay -- single shot, once only digitalWrite(led, LOW); // turn led off } }

In the code above the loop() continues to run without being stuck waiting for the delay to expire.
During each pass of the loop(), the difference between the current millis() and the delayStart time is compared to the DELAY_TIME. When the timer exceeds the value of the interval the desired action is taken. In this example the delay timer is stopped and the LED turned off.

SingleShotMillisDelay

Here is the BasicSingleShotDelay sketch re-written using the millisDelay library. This sketch is available in SingleShotMillisDelay.ino

Here is the millisDelay version where the code above has be wrapped in class methods of the millisDelay class.

#include <millisDelay.h>

int led = 13; // Pin 13 has an LED connected on most Arduino boards.
millisDelay ledDelay;
void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); digitalWrite(led, HIGH); // turn led on
// start delay ledDelay.start(10000); }
void loop() { // check if delay has timed out if (ledDelay.isFinished()) { digitalWrite(led, LOW); // turn led off } }

Repeating Timers

These are simple examples of a repeating delay/timer. BasicRepeatingDelay is the plain code and RepeatingMillisDelay uses the millisDelay library.

BasicRepeatingDelay

This sketch is available in BasicRepeatingDelay.ino

int led = 13; // Pin 13 has an LED connected on most Arduino boards.

unsigned long DELAY_TIME = 1500; // 1.5 sec unsigned long delayStart = 0; // the time the delay started bool delayRunning = false; // true if still waiting for delay to finish
bool ledOn = false; // keep track of the led state
void setup() { pinMode(led, OUTPUT); // initialize the digital pin as an output. digitalWrite(led, LOW); // turn led off ledOn = false;
// start delay delayStart = millis(); delayRunning = true; }
void loop() { // check if delay has timed out if (delayRunning && ((millis() - delayStart) >= DELAY_TIME)) { delayStart += DELAY_TIME; // this prevents drift in the delays // toggle the led ledOn = !ledOn; if (ledOn) { digitalWrite(led, HIGH); // turn led on } else { digitalWrite(led, LOW); // turn led off } } }

The reason for using
delayStart += DELAY_TIME;
to reset the delay to run again, is it allows for the possibility that the millis()-delayStart may be > DELAY_TIME because the millis() has just incremented or due to some other code in the loop() that slows it down. For example a long print statement. (See the Adding a Loop Montor in Step 7)

Another point is to start the delay at the end of startup(). This ensures the timer is accurate at the start of the loop(), even if startup() takes some time to execute.

RepeatingMillisDelay

Here is the BasicRepeatingDelay sketch re-written using the millisDelay library. This sketch is available in RepeatingMillisDelay.ino

#include <millisDelay.h>

int led = 13; // Pin 13 has an LED connected on most Arduino boards. bool ledOn = false; // keep track of the led state
millisDelay ledDelay;
void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); // initialize the digital pin as an output. digitalWrite(led, LOW); // turn led off ledOn = false;
// start delay ledDelay.start(1500); }
void loop() { // check if delay has timed out if (ledDelay.isFinished()) { ledDelay.repeat(); // start delay again without drift // toggle the led ledOn = !ledOn; if (ledOn) { digitalWrite(led, HIGH); // turn led on } else { digitalWrite(led, LOW); // turn led off } } }

Step 6: Other MillisDelay Library Functions

In addition to the start(delay), isFinished() and repeat() functions illustrated above, the millisDelay library also has
stop() to stop the delay timing out,

isRunning() to check if it has not already timed out and has not been stopped,

restart() to restart the delay from now, using the same delay interval,

finish() to force the delay to expire early,

remaining() to return the number of milliseconds until the delay is finished and

delay() to return the delay value that was passed to start()

Microsecond version of the library

millisDelay counts the delay in milliseconds. You can also time by microseconds. It is left as an exercise to the reader to write that class. (Hint: rename the class to microDelay and replace occurrences of millis() with micros() )

Step 7: Word of Warning – Add a Loop Monitor

Unfortunately many of the standard Arduino libraries use delay() or introduce pauses, such as AnalogRead and SoftwareSerial. Usually the delays these introduce are small but they can add up so I suggest you add a monitor at the top of your loop() to check how quickly it runs.

The loop monitor is very similar to the blink example. A small piece of code at the top of the loop() method just toggles the Led each time loop() is executed. You can then use a digital multimeter with at Hz scale to measure the frequency of the output on the LED pin (pin 13 in this case)

The code is:-

// Loop Monitor – this checks that the loop() is executed at least once every 1mS
// (c)2013 Forward Computing and Control Pty. Ltd. // www.forward.com.au // // This example code is in the public domain. int led = 13; // don't use on FioV3 when battery connected // Pin 13 has an LED connected on most Arduino boards. // if using Arduino IDE 1.5 or above you can use pre-defined // LED_BUILTIN instead of 'led' // the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); // add your other setup code here }
// the loop routine runs over and over again forever: void loop() { // toggle the led output each loop The led frequency must measure >500Hz (i.e. <1mS off and <1mS on) if (digitalRead(led)) { digitalWrite(led, LOW); // turn the LED off by making the voltage LOW } else { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) }
// add the rest of your loop code here }

You can download the monitor code here. When I run this code on my Uno board, the multimeter on the Hz range connected between pin 13 and GND reads 57.6Khz. i.e. about 100 times >500hz.

As you add your code to loop() the Hz reading will reduce. Just check it stays well above 500Hz (1mS per loop() execution) in all situations.

Share

    Recommendations

    • Make it Glow Contest 2018

      Make it Glow Contest 2018
    • Optics Contest

      Optics Contest
    • Plastics Contest

      Plastics Contest

    Discussions

    0
    None
    Eric Brouwer

    4 weeks ago

    Hi, and thanks for explaining and sharing this library. I am sure a lot of people will benefit from this Instructable.