This class is a simple timer implementation that allows you to check if a certain amount of time has passed and optionally reset the timer. It can be useful for various timing-related tasks in embedded systems or other applications.
This library was originally developed by PKAE Electronics.
This library is now maintained under @ardlib Organization.
Direct Link https://www.youtube.com/watch?v=R7UKZ58vIyc
Using the Zip File from this repository
Downloading the Zip file of this repository from :
https://github.com/ardlib/bosejis_PKAE_Timer/archive/refs/heads/main.zip
And installing using the Add .ZIP Library feature:
-
For Arduino IDE 1.8.19 and above:
Go to Menu
Sketch -> Include Library -> Add .ZIP Library
-
For Arduino IDE 1.8.19 and above:
Go to Menu
Tools -> Manage Libraries... -
Search for
PKAEorPKAE Timeryou would see the Library appear. -
Click on Install to get the library.
The constructor initializes the timer with an optional initial delay. If no delay is provided, it defaults to 0.
It sets the member variable _delay to the specified delay, initializes nStarted with the current time using the millis() function, and sets nLastStarted and nCount to 0.
Example:
PKAE_Timer Flash_LED(500);The above line defines a timer instance called Flash_LED and the delay is set to 500 milliseconds.The count down starts the moment the instance is created.
The IsTimeUp function checks if the specified time has elapsed since the timer was last started.
It calculates the effective delay to check against as the maximum of the fixed delay _delay and a dynamic delay nDynamicDelay.
If the elapsed time is greater than the effective delay, it returns true indicating that the time is up.
If lReset is true, it increments the count nCount, updates nLastStarted to the previous start time, and sets nStarted to the current time
Example:
if (Flash_LED.IsTimeUp()) {
// put code here e.g. toggle the LED state On/Off.
}This will execute the code with in the curly braces whatever the timer (500m/s in our example) is reached. The timer automatically resets itself to start again.
You can pass an optional dynamic delay to check if an alternative duration has lapsed or not.
Example:
if (Flash_LED.IsTimeUp(250)) {
// put code here to execute if 250m/s has lapsed.
}You can also pass an optional boolean expression to determine if the timer is reset or not. Handy if you wanted to temporarily stop the flashing for example.
Example:
if (Flash_LED.IsTimeUp(250,false)) {
// put code here to execute if 250m/s has lapsed. The timer will not reset.
}The Reset function resets the timer by updating nLastStarted to the previous start time and setting nStarted to the current time using `millis()``.
Example:
Flash_LED.Reset();This will effectively restart the timer counter for the Flash_LED timer instance.
Retains a counter of the number of iterations that the timer instance has triggered.
Example:
xCount=Flash_LED.nCount;xCount will store the number of times this timer has triggered,
Retains the millis() reading at the time in millis that the timer was started (either by its initial creation or by its last reset).
Example:
xStarted=Flash_LED.nStarted;xStarted will store when the last timer started for this timer instance. i.e. the millis() reading at the time it last initiated.
Retains the millis() reading of the previous time timer was started.
Example:
xLastStarted=Flash_LED.nLastStarted;xLastStarted will store when the previous timer started for this timer instance. i.e. the millis() reading at the time it previously initiated.
RepeatTimer is a minimal and efficient Arduino library for scheduling periodic callbacks using millis(). It is designed to work reliably on memory-constrained boards like the Arduino Uno and Nano.
Safe for AVR — uses plain function pointers, not
std::function.
- Call a function every N milliseconds
- Safe
millis()wraparound handling - Tracks trigger count (
nCount) - Supports dynamic interval or callback updates
- Minimal RAM and code footprint
- Designed for AVR and other constrained platforms
- No support for lambdas or captures — uses plain C-style function pointers.
- Ideal for Arduino Uno, Nano, Pro Mini, ATmega328P-based boards.
void blinkLED() {
static bool state = LOW;
state = !state;
digitalWrite(LED_BUILTIN, state);
}RepeatTimer myTimer(1000, blinkLED); // Fire every 1000msvoid loop() {
myTimer.update();
}Create a new timer that calls callback() every interval_ms.
Checks the timer and calls the callback if the interval has elapsed.
Change the timer interval.
Set or update the callback function.
Restart the timer countdown from now.
Returns true if both callback and interval are set.
Number of times the callback has been triggered.
Make sure to run the clang-format --verbose -i src/*.h src/*.cpp on the repo.
To clean the Example Sources clang-format --verbose -i examples/**/*.ino.
In order to keep the Compatibility make sure to test the Arduino Compliance
run arduino-lint --compliance strict.
This project is released under the MIT License. See the LICENSE file for details.
Sources: https://github.com/ardlib/bosejis_PKAE_Timer
bosejis_PKAE_Timer - PAKE Timer Library
Copyright (c) 2024 PKAE Electronics Copyright (C) 2025 by Abhijit Bose (aka. Boseji)
SPDX-License-Identifier: MIT
Full Name: MIT License
For more details please visit https://spdx.org/licenses/MIT.html.
