# What is Arduino Ticker Library?

ArduinoHardwareSoftware & Coding

The ticker library in Arduino helps you to perform fixed interval operations. It is a great alternative to using delay() as the interval, since this will provide non-blocking usage. This library doesn't use any hardware timer interrupts. Rather, it works with micros() and millis() to organize your tasks. All you need to provide this library is the name of the function to be called, at what interval, and how many times this should be repeated. The library does the rest.

## Example

In order to install this library, open the Library Manager, and search for 'Ticker'. Install the library by Stefan Staub.

Once the library is installed, go to File → Examples → Ticker → Ticker.

The example sketch begins with inclusion of the library.

#include "Ticker.h"

Then, the 5 functions that we will use as callbacks are declared. Also, a couple of global variables are defined. With the ticker library, there isn't any limit on the number of callbacks.

void printMessage();
void printCounter();
void printCountdown();
void printCountUS();
bool ledState;
int counterUS;

The next part is important. The 5 ticker objects are defined.

Ticker timer1(printMessage, 0, 1);
Ticker timer2(printCounter, 1000, 0, MILLIS);
Ticker timer3(printCountdown, 1000, 5);
Ticker timer5(printCountUS, 100, 0, MICROS_MICROS);

The syntax of the ticker object constructor is −

Ticker ticker_name(fptr callback, uint32_t timer, uint16_t repeats, interval_t mode)

Where,

• callback is the function you wish to call

• timer is the time interval in ms or us, depending on the mode.

• repeats is the number of times this callback should be triggered (0 means endless, and it is the default option)

mode can have 3 values −

• MILLIS − timer unit is milliseconds, Internal resolution in milliseconds

• MICROS − timer unit is milliseconds, Internal resolution in microseconds (DEFAULT)

• MICROS_MICROS − timer unit in microseconds, Internal resolution in microseconds

With default mode (MICROS), you can have time intervals up to 70 minutes. Use MILLIS for larger time intervals. Thus,

• timer1 callback is triggered just once, and immediately (interval = 0)

• timer2 callback is triggered every 1000 ms, with interval resolution in milliseconds.

• timer3 callback is triggered 5 times, every second.

• timer4 callback is triggered every 500 ms.

• timer5 callback is triggered every 100 microseconds, with internal resolution of microseconds.

Within the setup, you start the ticker objects defined earlier.

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
delay(2000);
timer1.start();
timer2.start();
timer3.start();
timer4.start();
timer5.start();
}

Within the loop, you constantly call .update() function, which essentially checks the ticker and runs the callback function if necessary.

void loop() {
timer1.update();
timer2.update();
timer3.update();
timer4.update();
timer5.update();
if (timer4.counter() == 20) timer4.interval(200);
if (timer4.counter() == 80) timer4.interval(1000);
}

Notice that there is no delay() in the loop, and even in the setup after the timers are begun.As per the official documentation, "If you use delay(), the Ticker will be ignored! You cannot use delay() command with the TimerObject. Instead of using delay, you can use the Ticker itself. For example, if you need that your loop run twice per second, just create a Ticker with 500 ms. It will have the same result that delay(500), but your code will be always state."

Essentially, ticker functions use the underlying timer that delay() uses. Therefore, you can't use delay along with ticker objects.

The .counter() function returns the number of executed callbacks. Thus, we change the interval of timer4 after 20 triggers and then again after 80 triggers.

Later, the definitions of the 5 callback functions are given, which are more or less selfexplanatory.

void printCounter() {
Serial.print("Counter ");
Serial.println(timer2.counter());
}
void printCountdown() {
Serial.print("Countdowm ");
Serial.println(5 - timer3.counter());
}
void printMessage() {
Serial.println("Hello!");
}
digitalWrite(LED_BUILTIN, ledState);
ledState = !ledState;
}
void printCountUS() {
counterUS++;
if (counterUS == 10000) {
Serial.println("10000 * 100us");
counterUS = 0;
}
}

You can use this library for performing tasks at a fixed interval, either endlessly, or for a certain number of times.

Published on 26-Jul-2021 09:31:57