Arduino library · Data and time

CountDown: an example that runs

Yes. You can use the CountDown library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (LED). The example below, "A countdown that pauses, then lights an LED at zero", runs on the Arduino Uno R3.

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

What CountDown does

A timer counting down to zero in milliseconds, microseconds, seconds or minutes: start(), remaining(), stop(), cont(), isRunning(). That is what CountDown, by Rob Tillaart, covers. The sketch below uses it on the Arduino Uno: “A countdown that pauses, then lights an LED at zero”.

Rob Tillaart's CountDown library (0.3.5), a timer that counts down to zero in milliseconds, microseconds, seconds or minutes.

start(days, hours, minutes, seconds) counts in seconds and start(days, hours, minutes) in minutes. stop() holds the count, cont() carries on from it, restart() starts the last count again. isRunning() is false once it reaches zero.

The calls follow release 0.3.5 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 nameCountDown
AuthorRob Tillaart
Include#include <CountDown.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 <CountDown.h> and the sketch compiles, with no download, no zip and no Library Manager.

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

The calls you will use

In the order a sketch meets them.

CountDown CD(CountDown::MILLIS)

Makes CD, the CountDown object the calls below are made on.

CD.stop()

Stops it.

CD.remaining()

Returns the remaining as a whole number.

CD.isRunning()

Returns true or false for "is running".

Example: A countdown that pauses, then lights an LED at zero

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

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

CountDown CD(CountDown::MILLIS);

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  CD.start(1000);
  delay(400);
  CD.stop();
  Serial.print("stopped with ");
  Serial.println(CD.remaining() / 100 * 100);
  delay(500);
  Serial.print("still ");
  Serial.println(CD.remaining() / 100 * 100);
  CD.cont();
}

void loop() {
  static bool told = false;
  if (!told && !CD.isRunning()) {
    told = true;
    digitalWrite(13, HIGH);
    Serial.print("zero at ");
    Serial.println(millis() / 100 * 100);
  }
}

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 CountDown without installing anything

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