Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

arduino-timer

> 编程语言
Open source

Non-blocking library for delaying function calls

357 stars0 likes1 views
WebsiteGitHub

About

Non-blocking library for delaying function calls

arduino-timer - library for delaying function calls

Simple non-blocking timer library for calling functions in / at / every specified units of time. Supports millis, micros, time rollover, and compile time configurable number of tasks.

Use It

Include the library and create a Timer instance.

cpp
#include 

auto timer = timer_create_default();

Or using the Timer constructors for different task limits / time resolution

cpp
Timer timer; // 10 concurrent tasks, using millis as resolution
Timer timer; // 10 concurrent tasks, using micros as resolution
Timer timer; // 10 concurrent tasks, using micros as resolution, with handler argument of type int

Call timer.tick() in the loop function

cpp
void loop() {
    timer.tick();
}

Make a function to call when the Timer expires

cpp
bool function_to_call(void *argument /* optional argument given to in/at/every */) {
    return true; // to repeat the action - false to stop
}

Call function_to_call in delay units of time (unit of time defaults to milliseconds).

cpp
timer.in(delay, function_to_call);
timer.in(delay, function_to_call, argument); // or with an optional argument for function_to_call

Call function_to_call at a specific time.

cpp
timer.at(time, function_to_call);
timer.at(time, function_to_call, argument); // with argument

Call function_to_call every interval units of time.

cpp
timer.every(interval, function_to_call);
timer.every(interval, function_to_call, argument); // with argument

To cancel a Task

cpp
auto task = timer.in(delay, function_to_call);
timer.cancel(task);

To cancel all Tasks

cpp
timer.cancel();

Check if a timer is empty - no active Tasks

cpp
if (timer.empty()) { /* no active tasks */ }

Get the number of active Tasks

cpp
auto active_tasks = timer.size();

Be fancy with lambdas

cpp
timer.in(1000, [](void*) -> bool { return false; });
timer.in(1000, [](void *argument) -> bool { return argument; }, argument);

Getting the number of ticks until the next Task

cpp
auto ticks = timer.ticks(); // usefull for sleeping until the next task
cpp
void loop {
    auto ticks = timer.tick(); // returns the number of ticks
}

Avoiding ticks calculation inside of tick

cpp
void loop {
    timer.tick(); // avoids ticks() calculation
}

API

…

Installation

Check out the instructions from Arduino.

OR copy src/arduino-timer.h into your project folder (you won't get managed updates this way).

Examples

Found in the examples/ folder.

The simplest example, blinking an LED every second (from examples/blink):

cpp
#include 

auto timer = timer_create_default(); // create a timer with default settings

bool toggle_led(void *) {
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
  return true; // keep timer active? true
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT

  // call the toggle_led function every 1000 millis (1 second)
  timer.every(1000, toggle_led);
}

void loop() {
  timer.tick(); // tick the timer
}

LICENSE

Check the LICENSE file - 3-Clause BSD License

Notes

Currently only a software timer. Any blocking code delaying timer.tick() will prevent the timer from moving forward and calling any functions.

The library does not do any dynamic memory allocation.

The number of concurrent tasks is a compile time constant, meaning there is a limit to the number of concurrent tasks. The in / at / every functions return NULL if the Timer is full.

A Task value is valid only for the timer that created it, and only for the lifetime of that timer.

Change the number of concurrent tasks using the Timer constructors. Save memory by reducing the number, increase memory use by having more. The default is TIMER_MAX_TASKS which is currently 16.

If you find this project useful, consider becoming a sponsor.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C++arduinoarduino-libraryarduino-timerconcurrent-tasks

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言