Timer Callbacks

Timer Callbacks

Goal

Our goal in this project is to explore timer callbacks and use them to blink an LED to understand them better. We will use the Timer library from the PNG Arduino Framework for this example but you can also write your own.

Concept

Timers are perhaps the most used peripherals of microcontrollers. We’ve previously explored the Arduino millis() function to keep track of time but in order to do this, we had to repeatedly check the current time and compare it with the reference (previous) time to determine if sufficient time has elapsed. This makes the code a bit difficult to read.

Instead of repeatedly checking (polling) the current time, we can use timer callbacks to keep things simple. We can use a timer callback to automatically call a function once the predetermined time has elapsed. Setting up the timer is not complicated, but for this example we will explore the concept of timer callbacks and will use an external library from the PNG Arduino Framework. The source code below includes the necessary files, so you don’t have to add any libraries and can simple compile and run the sketch.

Let’s start by including the timer library and creating a new timer as follows:

#include "timer.h"

#define LED_EXTERNAL    0                 // external LED connected to pin 0
#define TIME_INTERVAL   1000               // 1000mS (1 second) time interval

Timer *ledTimer = new Timer(TIME_INTERVAL);        //create new timer that timesout every 1000mS (1 second)
Include library and create new timer

Next, we need to pass the callback function to the timer so that it knows which function to call once the time is up. We do this by using the setOnTimer() function. Because of how the PNG Arduino Framework is written, we need to us the arrow operator (->) instead of the dot operator (.). We then start the timer by calling the Start() function.

  ledTimer->setOnTimer(&timerCallback);    //let the timer know which function it should call once time is up
  ledTimer->Start();                       //start the timer
Set callback function and start timer

We need to define the callback function and we will simply toggle both the LEDs like in the previous examples. Here’s what the code will look like:

void timerCallback(){
  
  digitalWrite(LED_EXTERNAL, !digitalRead(LED_EXTERNAL));   // toggle LED_EXTERNAL
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));     // toggle LED_BUILTIN
  
}
Toggling LEDs

The timer library we use in this example makes use of the millis() function behind the scenes so we need to call the update() function to keep track of time. We do this in the loop as follows:

void loop() {
  
 ledTimer->Update();

}
Update timer

And that’s all! Doesn’t the code look much cleaner now? Here’s the result:

Piksey Blinking LEDs
Source

The complete Arduino project is available for download using the following link. Downloads are restricted to registered users. Registration is and will always be 100% FREE.

Project Source: P4_Timer_Callbacks

Dependencies

This project uses standard Arduino libraries and is thus compatible with all Arduino boards that contain the built-in LED (on pin 13) and digital pin 0. Make sure the correct board is selected in the Arduino IDE.