Skip to content

arnabdebnath208/TimeoutScheduler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TimeoutScheduler

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.

Features

  • 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.

Installation

  1. Clone this repository:
    git clone https://github.com/arnabdebnath208/TimeoutScheduler.git
  2. Copy the src/TimeoutScheduler.h and src/TimeoutScheduler.cpp files into your Arduino project's folder, or install as a library in the Arduino IDE.

Usage

Basic Example

#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.

API Reference

TimeoutScheduler Methods

  • int setTimeout(void (*callback)(void), unsigned long delayMs);

    Schedule a task to run after delayMs milliseconds. 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()).

How It Works

  • 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.

License

This project is licensed under the MIT License. See LICENSE for details.

Author

Arnab Debnath

GitHub Repository

About

A lightweight Arduino library for scheduling one-time tasks after a specified delay (in milliseconds)

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages