Introduction :
When we start learning about
microcontrollers, and especially Arduino in our case, the first program that
you have written was most probably the famous blinking LED program. In that
program you certainly used to call the delay() function to set the time
interval between blinks. The only thing is that delay() sets Arduino on pause
mode, so how we can blink the LED while still using Arduino for doing other
tasks at the same time like sending serial data for example ? That’s why we are
going to learn about the Timer library to facilitate this task.
The library :
The library Timer has a simple functioning
principle. After adding the library to our project, we can create a variable of
type Timer, then we update it in the loop function and finally give some
instructions to our timer to perform the tasks like blinking an LED for 100ms
for example.
Basic Commands :
As said previously, we have to import the
Timer library to our program.
#include “Timer”
Then, we have to create our variable of type
timer :
Timer timer;
Also, we have to update our newly created
timer in the loop function as follows :
void loop(){
Timer.update();
}
Now we don’t need to use the delay() function
anymore. We have our timer to count time in the program.
The most used functions in the Timer library
are the following :
int every(long period, callback, int
repeatCount)
Run the 'callback' every 'period' milliseconds for a total of 'repeatCount' times.
int after(long duration, callback)
Run the 'callback' once after 'period' milliseconds.
int oscillate(int pin, long period, int
startingValue, int repeatCount)
Toggle the state of the digital output 'pin' every 'period' milliseconds 'repeatCount' times.
The pin's starting value is specified in 'startingValue', which should be HIGH or LOW.
int pulse(int pin, long period, int
startingValue)
Toggle the state of the digital output 'pin' just once after 'period' milliseconds.
The pin's starting value is specified in 'startingValue', which should be HIGH or LOW.
int update()
Must be called from 'loop'. This will service all the events associated with the timer.
Examples :
The following example is a program that passes
a pin from a state to another for a set period of time only once. To do it we
need to implement the ‘pulse’ method.
#include "Timer.h"
Timer t;
int pin = 13;
void setup()
{
pinMode(pin, OUTPUT);
t.pulse(pin, 10 * 60 * 1000, HIGH); // 10 minutes
}
void loop()
{
t.update();
}
The update method in the loop function takes
only few milliseconds to run, and the program can continue executing other
instructions while the timer counts the time in the setup function.
The second example is program that toggles the pin 13 from a state to
another every 100 ms and reads the analog pin 0 from the serial monitor every 2
seconds.
#include "Timer.h"
Timer t;
int pin = 13;
void setup()
{
Serial.begin(9600);
pinMode(pin, OUTPUT);
t.oscillate(pin,
100, LOW); // la fonction qui fait
clignoter la led toutes les 100 ms
t.every(1000,
envoi); // et celle-ci qui appelle la fonction 'envoi' toutes les secondes
}
void loop()
{
t.update();
}
void envoi() // fonction qui envoie par liaison série l'entrée du pin 0
{
Serial.println(analogRead(0));
}