elapsedMillis: a variable that counts the milliseconds for you
Yes. You can use the elapsedMillis library online in Mokxi with nothing to install, and the sketch runs in your browser. The example below, "Do something every half second without delay()", runs on the Arduino Uno R3.
Built in on Mokxi: #include <elapsedMillis.h> and it builds, with nothing to install. Free, and no account needed.
What elapsedMillis does
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.
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 name | elapsedMillis |
|---|---|
| Author | Paul Stoffregen |
| Include | #include <elapsedMillis.h> |
| Boards | every board Mokxi compiles for |
| In the simulator | Simulated: 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.
- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- ATtiny85
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- ESP32-S3-CAM
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi
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.
#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);
}
}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.