TimerKernel is a lightweight Arduino library for managing non-blocking timers
- Non-Blocking, No
delay(), for best code responsiveness - Support for multiple time units (
MINUTE,SECOND,MILLISECOND,MICROSECOND) - Multiple independent timers can be used simultaneously
- Simple and intuitive API
- Supports Floating Point duration for ease of use and percise timing
- Supports running timer only a stated number of times
To install this library:
-
Download ZIP
Click the greenCodebutton on this repository page, then select "Download ZIP". -
Open Arduino IDE
Launch the Arduino IDE on your computer. -
Install ZIP Library
- Go to Sketch > Include Library > Add .ZIP Library...
- Navigate to where you downloaded the ZIP file.
- Select the ZIP file and click Open.
-
Done!
The library is now installed and ready to use. You can find examples for this library in File > Examples > TimerKernel.
Note:
- Do not unzip the downloaded file before installing.
- If you are updating an existing library, delete the old version from your
librariesfolder first.
Traditionally, using millis() for timed events requires manual tracking of multiple variables:
unsigned long previousMillis_1 = 0; //store time for first event
unsigned long previousMillis_2 = 0; //store time for second event
const long interval_1 = 1000; //interval for first event
const long interval_2 = 2000; //interval for second event
void setup(){
}
void loop() {
//check time since program started, and store in "currentMillis"
unsigned long currentMillis = millis();
//conditional that checks whether 1 second has passed since last event
if (currentMillis - previousMillis_1 >= interval_1) {
previousMillis_1 = millis();
//execute a piece of code, every *1 second*
}
//conditional that checks whether 2 seconds have passed since last event
if (currentMillis - previousMillis_2 >= interval_2) {
previousMillis_2 = millis();
//execute a piece of code, every *2 seconds*
}
}With TimerKernel, this becomes much simpler and easier to manage:
#include <TimerKernel.h>
TimerKernel timer_1;
TimerKernel timer_2;
const long interval_1 = 1000; //interval for first event
const long interval_2 = 2000; //interval for second event
void setup(){
}
void loop() {
//conditional that checks whether 1 second has passed since last event
if (timer_1.hasExpired(interval_1)) {
//execute a piece of code, every *1 second*
}
//conditional that checks whether 2 seconds have passed since last event
if (timer_2.hasExpired(interval_2)) {
//execute a piece of code, every *2 seconds*
}
}- simple_led_blink - demonstrates how to use
toggleState()to blink a led. - timed_event - demonstrates how to use
hasExpired()to do a task periodically. - run_n_times - demonstrates how to run timer only a specific number of times.
- run_n_times-advanced - demonstrates how to restart timers on button press.
bool hasExpired(double duration, TimeUnit unit = MILLISECOND, int runAmount = INFINITE);hasExpired() runs runAmount of times.
It checks whether the specified time duration has passed.
By default, the duration is expressed in milliseconds.
| Parameter | Type | Description |
|---|---|---|
duration |
double |
Time interval in desired unit |
unit |
enum |
Time unit (MINUTE, SECOND, MILLISECOND, MICROSECOND) |
runAmount |
int |
Number of times to run hasExpired() |
Returns:
trueif the specified time duration has elapsed andhasExpired()has not yet been executedrunAmounttimes, otherwise returnsfalse.
bool toggleState(double duration, TimeUnit unit = MILLISECOND, int runAmount = INFINITE);toggleState() runs runAmount of times.
It toggles state every time the duration elapses.
By default, the duration is expressed in milliseconds.
| Parameter | Type | Description |
|---|---|---|
duration |
double |
Time interval in desired unit |
unit |
enum |
Time unit (MINUTE, SECOND, MILLISECOND, MICROSECOND) |
runAmount |
int |
Number of times to run hasExpired() |
Returns:
- The toggled state (
trueorfalse) iftoggleState()has not yet been executedrunAmounttimes. otherwise returnsfalse.
void resetTimer();Resets both hasExpired() and toggleState(), causing them to start counting from zero. Calling this function restarts the cycles of both timers until runAmount is reached.
Returns:
- Nothing
void resetToggleState();Resets toggleState() function, causing it to start counting from zero. Calling this function restarts the toggle cycle until runAmount is reached.
Returns:
- Nothing
void resetHasExpired();Resets hasExpired() function, causing it to start counting from zero. Calling this function restarts the timing cycle until runAmount is reached.
Returns:
- Nothing
Note:
- By Default, all durations are interpreted in millisecond.
- By Default, all timers run infinitely.
- This project is licensed under the MIT License.