Tutorial: How and Why to use Timers instead of the Delay() Function

Please put some money in the tip jar by clicking on the donate button to support me so I can continue creating contend like this. P.S. please donate more than $1 as PayPal takes minimum $0.30 per transaction

 

A common problems with Arduino projects is that buttons or other input  sensors seem to be working intermittently or with a postponed reaction. In cases like this your project might be suffering from delays.

From the beginning we get taught to use the delay() function if we want to create timed delays on execution of specific parts of our Arduino sketch. The drawback is that your loop gets halted and functions above and below the delay() are not being executed during this interval.

A timer approach is a little harder to implement but your main loop keeps executing and only exclude the code and functions you want to exclude. To simplify this we can use timer libraries like the elapsedMillis.

Click here to download the elapsedMillis library

Let’s take a quick look at some simple examples below. 

The first step is to include your library and create a instance variable of the library You can do that in the top of your sketch. See below how that is done:

#include <elapsedMillis.h> //load the library
elapsedMillis timeElapsed;//Create an Instance

Now in your loop you use an if() statement to check if the appropriate interval has elapsed: and reset your timer back to zero. If you want to execute a block of code every 5 seconds you would do something like this:

loop(){

if (timeElapsed > 5000)
  {                                                        
Do Something      
timeElapsed = 0;              // reset the counter to 0 so the counting starts over...
  }

}

Example

To illustrate the effect of delays we are going to create a project that has 4 LED's that will blink in sequence with a one second interval. You will have a push button that will light all the lights at once when pressed.

I created 2 example sketches to show the effects of the delay() function and what happens when using the elapsedMillis.h library. To recreate the demo you are going to need the hardware listed below

Fritzing Bill of Materials

Part List

Amount Part Type Properties
2 Red LED color Red ; package 5 mm [THT]; leg yes
2 Yellow LED color Yellow ; package 5 mm [THT]; leg yes
1 Arduino Uno (Rev3) - ICSP type Arduino UNO (Rev3) - ICSP
4 330Ω Resistor resistance 330Ω; tolerance ±5%; bands 4; package THT
1 10kΩ Resistor resistance 10kΩ; tolerance ±5%; bands 4; package THT
1 Pushbutton package [THT]



Hardware Setup

Recreate the hardware setup shown below. You can click on the image to get a more clear view of how to create the connections

The Arduino Sketches

Files to download for this project:

After you have created your hardware setup,  download the Timer_demo1.ino file first and upload it to your Arduino Uno. Make sure you have first downloaded the elapsedMillis.h file and installed it in the Arduino IDE. For instruction how to install the library click here. The Timer_demo1 sketch uses delays to make the lights blink Sequentially. When you press the button to light up all the lights you will notice a delayed reaction or it looks like it only functions intermittently.

Now download Timer_demo2 and upload it to your Arduino Uno. This sketch uses the elapsedMillis library. You see that the code is more complex but the button functions whenever you press it. You will experience no delays or intermittent operation of your input button. 

If needed you can create multiple instances of the elapsedMills variable if you need more then one timer:

 

#include <elapsedMillis.h> //load the library
elapsedMillis timeElapsed1;//Create an Instance
elapsedMillis timeElapsed2;//Create an Instance

In conclusion, using timers instead of delays can improve the functionality of your projects. As with everything, it is not an tool for every occasion (as it can make your sketch unnecessary complicated), so always use your experience and see if this would work for you.

Ab Kurk3 Comments