TimerInterrupt Library
How To Install Using Arduino Library Manager
This library enables you to use Interrupt from Hardware Timers on an Arduino, such as Nano, UNO, Mega, etc. It now supports 16 ISR-based timers, while consuming only 1 hardware Timer. Timers' interval is very long (ulong millisecs). The most important feature is they're ISR-based timers. Therefore, their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks.
This library enables you to use Interrupt from Hardware Timers
on an Arduino, such as Nano, UNO, Mega
, etc.
Why do we need this Hardware Timer Interrupt?
Imagine you have a system with a mission-critical function, measuring water level and control the sump pump or doing something much more important. You normally use a software timer to poll, or even place the function in loop(). But what if another function is blocking the loop() or setup().
So your function might not be executed, and the result would be disastrous.
You'd prefer to have your function called, no matter what happening with other functions (busy loop, bug, etc.).
The correct choice is to use a Hardware Timer with Interrupt to call your function.
These hardware timers, using interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy.
Functions using normal software timers, relying on loop() and calling millis(), won't work if the loop() or setup() is blocked by certain operation. For example, certain function is blocking while it's connecting to WiFi or some services.
The catch is your function is now part of an ISR (Interrupt Service Routine), and must be lean / mean, and follow certain rules. More to read on:
Installation
Use Arduino Library Manager
The suggested and easiest way is to use Arduino Library Manager
. Search for TimerInterrupt
, then select / install the latest version.
Usage
Before using any Timer, you have to make sure the Timer has not been used by any other purpose.
Only Timer1 and Timer2 are supported for Nano, UNO, etc. boards possessing 3 timers.
Timer3, Timer4 and Timer5 are supported for Arduino Maga boards.
How to use:
//These define's must be placed at the beginning before #include "TimerInterrupt.h"
#define TIMER_INTERRUPT_DEBUG 0
#define USE_TIMER_1 true
#define USE_TIMER_2 true
#define USE_TIMER_3 false
#define USE_TIMER_4 false
#define USE_TIMER_5 false
#include "TimerInterrupt.h"
void TimerHandler1(void)
{
static bool toggle1 = false;
static bool started = false;
if (!started)
{
started = true;
pinMode(LED_BUILTIN, OUTPUT);
}
//timer interrupt toggles pin LED_BUILTIN
digitalWrite(LED_BUILTIN, toggle1);
toggle1 = !toggle1;
}
void TimerHandler2(void)
{
static bool toggle2 = false;
static bool started = false;
if (!started)
{
started = true;
pinMode(A0, OUTPUT);
}
//timer interrupt toggles outputPin
digitalWrite(A0, toggle2);
toggle2 = !toggle2;
}
#define TIMER1_INTERVAL_MS 1000
#define TIMER2_INTERVAL_MS 2000
void setup()
{
Serial.begin(115200);
Serial.println("\nStarting");
// Select Timer 1-2 for UNO, 0-5 for MEGA
// Timer 2 is 8-bit timer, only for higher frequency
ITimer1.init();
// Using ATmega328 used in UNO => 16MHz CPU clock ,
if (ITimer1.attachInterruptInterval(TIMER1_INTERVAL_MS, TimerHandler1))
Serial.println("Starting ITimer1 OK, millis() = " + String(millis()));
else
Serial.println("Can't set ITimer1. Select another freq. or timer");
// Select Timer 1-2 for UNO, 0-5 for MEGA
// Timer 2 is 8-bit timer, only for higher frequency
ITimer2.init();
if (ITimer2.attachInterruptInterval(TIMER2_INTERVAL_MS, TimerHandler2))
Serial.println("Starting ITimer2 OK, millis() = " + String(millis()));
else
Serial.println("Can't set ITimer2. Select another freq. or timer");
}
void loop()
{
}