Arduino library · Data and time

arduino-timer: call a function later, or every interval

Yes. You can use the arduino-timer library online in Mokxi with nothing to install, and the sketch runs in your browser. The example below, "Blink and report without delay()", runs on the Arduino Uno R3.

Built in on Mokxi: #include <arduino-timer.h> and it builds, with nothing to install. Free, and no account needed.

What arduino-timer does

arduino-timer by Michael Contreras is a small software timer for sketches that need a few things to happen on a schedule. You make one timer, give it functions with every(1000, blink) for a repeat, in(250, once) for a single call after a delay, or at(time, report) for a single call at a millis() value, and call timer.tick() from loop(). Nothing uses a hardware timer, so it works on every board and leaves PWM and Servo alone.

The one rule to learn is the handler’s return value. A handler is bool name(void *) and for an every() task its return decides what happens next: true keeps the task repeating, false cancels it. Tasks made with in() and at() run once whatever they return. That makes "blink five times and stop" a counter and a return false, with no separate cancel call needed.

Compared with the Ticker library and TaskScheduler, arduino-timer is the lightest: one object, a fixed table of 16 tasks by default, and no task objects to declare. The example counts a 100 ms tick, prints once after 250 ms, and reports the count at 1050 ms, all from a loop() that does nothing but tick().

Library Manager namearduino-timer
AuthorMichael Contreras
Include#include <arduino-timer.h>
Boardsevery board Mokxi compiles for
In the simulatorSimulated: it runs against the part the way it does on the bench.

Install it

It builds on every board Mokxi compiles for.

In Mokxi it is built in: #include <arduino-timer.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "arduino-timer" by Michael Contreras from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add arduino-timer to lib_deps in PlatformIO. Mokxi follows the calls of release 3.0.1, so a sketch written here builds there too.

The calls you will use

In the order a sketch meets them.

auto timer = timer_create_default()

A timer with room for 16 tasks, counting in milliseconds. Timer<4, micros> makes a smaller one in microseconds.

timer.every(1000, toggle_led)

Calls toggle_led every 1000 ms for as long as it returns true. Returns a task handle you can keep for cancel().

timer.in(250, once)

Calls once a single time, 250 ms from now.

timer.at(millis() + 1050, report)

Calls report a single time when millis() reaches the given value.

timer.tick()

Runs every task that is due. Call it on every pass of loop(); it returns the milliseconds until the next task.

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

timer.cancel(task)

Stops a task by its handle and clears the handle.

timer.size()

How many tasks are scheduled right now, useful to check the table is not full.

Example: Blink and report without delay()

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <arduino-timer.h>

auto timer = timer_create_default();
int ticks = 0;

bool tick(void *) {
  ticks++;
  return true;  // keep repeating
}

bool once(void *) {
  Serial.println("once fired");
  return false;
}

bool report(void *) {
  Serial.print("ticks: ");
  Serial.println(ticks);
  return false;
}

void setup() {
  Serial.begin(9600);
  timer.every(100, tick);
  timer.in(250, once);
  timer.at(millis() + 1050, report);
}

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

Open it in the editor, wired and ready to run

Common errors and how to fix them

invalid conversion from 'void (*)()' to handler type

The handler has the wrong shape. It must be declared bool name(void *) and return a value, for example bool blink(void *) { ... return true; }.

The repeating task runs once and stops

The handler returns false, which cancels an every() task. Return true from any handler you want to keep repeating.

every() returns no handle and the task never runs

The task table is full, 16 tasks by default. Cancel tasks you are done with, or make a bigger timer with Timer<32> timer; at the cost of a little RAM.

Questions and answers

Can I pass a value to the handler?

Yes. every(), in() and at() take an optional third argument that arrives as the handler’s void * parameter, so one handler can serve several pins.

Try arduino-timer without installing anything

Open the example, change the sketch, and keep your version in a free account.