Arduino library · Data and time

elapsedMillis: a variable that counts the milliseconds for you

elapsedMillis comes from Paul Stoffregen’s Teensy core and makes the usual blink-without-delay pattern shorter. Instead of keeping a previousMillis variable and writing millis() - previousMillis >= interval, you declare an elapsedMillis variable and read it: its value is always the milliseconds since it was last set. Set it to 0 to start counting again.

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

What elapsedMillis does

There is nothing running in the background. The variable stores the moment it was set, and every time you read it, it subtracts that from millis(). That means it costs four bytes, works on every board, and rolls over cleanly after 49 days just as a hand-written millis() check does. elapsedMicros is the same thing in microseconds, for short timings such as a pulse width.

The example checks one elapsedMillis every pass of loop() and prints a line each half second, without a single delay(), so anything else you add to loop() keeps running between reports. Declare the variable globally, or as static inside a function, so it keeps its value between passes.

Library Manager nameelapsedMillis
AuthorPaul Stoffregen
Include#include <elapsedMillis.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 <elapsedMillis.h> and the sketch compiles, with no download, no zip and no Library Manager.

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

The calls you will use

In the order a sketch meets them.

elapsedMillis sinceReport;

A millisecond counter that starts at 0 when it is created.

if (sinceReport >= 500)

Reads the counter like any unsigned long. True once half a second has passed since it was last set.

sinceReport = 0

Restarts the count. Subtract instead, sinceReport -= 500, to keep a steady rhythm even if a check comes a little late.

if (sinceReport >= 500) {
  sinceReport -= 500;
  digitalWrite(13, !digitalRead(13));
}

elapsedMicros sincePulse;

The same counter in microseconds, rolling over about every 71 minutes.

Example: Do something every half second without delay()

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

sketch.ino · Arduino Uno R3
#include <elapsedMillis.h>

elapsedMillis sinceReport;
int reports = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (sinceReport >= 500) {
    sinceReport = 0;
    reports++;
    Serial.print("half a second, ");
    Serial.println(reports);
  }
}

Open it in the editor, wired and ready to run

Common errors and how to fix them

The timer never fires, or fires on every pass

The variable is declared inside loop() without static, so it is created fresh at 0 on every pass. Declare it globally or as static elapsedMillis inside the function.

Blinks drift slowly against a clock

Setting the counter to 0 loses whatever time passed since it crossed the limit. Subtract the interval instead with -= so small delays do not add up.

Questions and answers

Is elapsedMillis the same as using millis() myself?

Yes, in what it does. It hides the subtraction and the stored start time inside a variable, which makes sketches with several timers easier to read.

Try elapsedMillis without installing anything

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