#include <arduino-timer.h>
Timer<10> MainTimer; // Timer with 10 task slots
Timer<>::Task DummyTask; // A task of the timer
unsigned int Counter = 0;
void setup() {
Serial.begin(9600);
Serial.println("Program started - Correct Handling of Timer ID");
Serial.println("----------------------------------------------");
Serial.println("After 5 seconds, the DummyTask will be canceled and");
Serial.println("will receive ID = 0, as it should.");
Serial.println();
/* Let's output Serial information every 2 seconds */
MainTimer.every(1000, SerialPrinter);
/* Set a dummy task */
DummyTask = MainTimer.every(1000, DummyFunction);
}
void loop() {
// put your main code here, to run repeatedly:
MainTimer.tick<void>(); // avoids ticks() calculation
}
bool SerialPrinter(void *) {
Serial.print("Timers: ");
Serial.print(MainTimer.size());
Serial.print(" | Dummy Task ID: ");
Serial.print(DummyTask);
Serial.println();
/*
After 5 seconds, we cancel the timer using .cancel() method
Canceling the timer using this method, correctly sets DummyTask to 0.
*/
if(++Counter == 5) {
MainTimer.cancel(DummyTask);
}
return true;
}
bool DummyFunction(void *) {
return true;
}