A lightweight Arduino library for scheduling one-time tasks after a specified delay (in milliseconds), similar to setTimeout in JavaScript. This library is ideal for running delayed or timed operations on Arduino-compatible microcontrollers without blocking the main loop.
- Schedule multiple one-time tasks with different delays.
- Non-blocking: does not halt the main loop.
- Simple API for adding, removing, and executing tasks.
- Efficient linked-list implementation for task management.
- Suitable for all Arduino-compatible boards.
- Clone this repository:
git clone https://github.com/arnabdebnath208/TimeoutScheduler.git
- Copy the
src/TimeoutScheduler.handsrc/TimeoutScheduler.cppfiles into your Arduino project's folder, or install as a library in the Arduino IDE.
#include <TimeoutScheduler.h>
TimeoutScheduler timeoutManager;
void task1() {
Serial.println("Task 1 executed");
}
void setup() {
Serial.begin(9600);
timeoutManager.setTimeout(task1, 1000); // Execute after 1000 ms
timeoutManager.setTimeout([]() {
Serial.println("Lambda task executed after 5 seconds");
}, 5000);
}
void loop() {
timeoutManager.handle(); // Check and execute due tasks
}See examples/AllUsage/AllUsage.ino for a complete example.
-
int setTimeout(void (*callback)(void), unsigned long delayMs);
Schedule a task to run after
delayMsmilliseconds. Returns a unique task ID. -
bool clearTimeout(int id);
Cancel a scheduled task by its ID.
-
int clearAllTimeouts();
Remove all scheduled tasks.
-
int getTimeoutTaskCount();
Get the number of pending tasks.
-
int handle();
Check and execute all due tasks. Call this frequently (e.g., in
loop()).
- Tasks are stored in a linked list, sorted by their scheduled execution time.
- The
handle()method checks the head of the list and executes tasks whose timeouts have expired. - After execution, tasks are removed from the list.
This project is licensed under the MIT License. See LICENSE for details.
Arnab Debnath