Arduino library · Data and time

Chrono: an example that runs

Yes. You can use the Chrono library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (LED). The example below, "Two LEDs on two chronos, and a stopwatch that pauses", runs on the Arduino Uno R3.

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

What Chrono does

A stopwatch checked in loop(): hasPassed(1000, true), elapsed(), stop(), resume() and add(), in milliseconds, microseconds or seconds. That is what Chrono, by Thomas O Fredericks, covers. The sketch below uses it on the Arduino Uno: “Two LEDs on two chronos, and a stopwatch that pauses”.

Thomas O Fredericks' Chrono library (1.2.0), a stopwatch on millis(), micros() or seconds that a sketch checks rather than waits on.

stop() freezes elapsed() and resume() carries on from it; add() moves it on; hasPassed(t, true) restarts the chrono as it answers, counted from when it passed so the beat does not drift. Like upstream it is safe across the wrap of millis().

The calls follow release 1.2.1 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager nameChrono
AuthorThomas O Fredericks
Include#include <Chrono.h>
#include <LightChrono.h>
Boardsevery board Mokxi compiles for
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesLED

Install it

It builds on every board Mokxi compiles for.

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

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

The calls you will use

In the order a sketch meets them.

Chrono ledChrono

Makes ledChrono, the Chrono object the calls below are made on.

stopwatch.stop()

Stops it and returns true or false.

stopwatch.resume()

Resumes it.

ledChrono.hasPassed(250, true)

Returns true or false for "has passed". Arguments: timeout, restart if passed.

stopwatch.isRunning()

Returns true or false for "is running".

Example: Two LEDs on two chronos, and a stopwatch that pauses

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

sketch.ino · Arduino Uno R3
// After the library's Blink example.
#include <Chrono.h>

Chrono ledChrono;
Chrono stopwatch;
int ledState = LOW;

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  delay(300);
  stopwatch.stop();
  Serial.print("stopped at ");
  Serial.println(stopwatch.elapsed() / 100 * 100);
  delay(500);
  Serial.print("still ");
  Serial.println(stopwatch.elapsed() / 100 * 100);
  stopwatch.resume();
}

void loop() {
  if (ledChrono.hasPassed(250, true)) {
    ledState = !ledState;
    digitalWrite(13, ledState);
  }
  if (stopwatch.hasPassed(1000) && stopwatch.isRunning()) {
    stopwatch.stop();
    Serial.println("a second of running time");
  }
}

Wiring

  • Arduino Uno R3 13 to LED A
  • Arduino Uno R3 GND to LED C

Open it in the editor, wired and ready to run

Try Chrono without installing anything

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