Arduino library · Data and time

Tasker: an example that runs

Yes. You can use the Tasker library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (LED). The example below, "Blink on an interval, three beeps on setRepeated, one timeout", runs on the Arduino Uno R3.

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

What Tasker does

SetTimeout(), setInterval() and setRepeated(n) for plain functions (with an optional int), run from tasker.loop(); cancel(), isScheduled(), scheduledIn(). That is what Tasker, by Petr Stehlik, covers. The sketch below uses it on the Arduino Uno: “Blink on an interval, three beeps on setRepeated, one timeout”.

Petr Stehlik's Tasker library (3.0.0), JavaScript-style setTimeout(), setInterval() and setRepeated() run from loop().

A function may take an int, given after the time: setInterval(f, 500, 3). setRepeated(f, ms, n) runs it n times. cancel() (or clearTimeout(), clearInterval()) takes the function (and int) back; isScheduled() and scheduledIn() ask about it.

The calls follow release 3.0.0 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 nameTasker
AuthorPetr Stehlik
Include#include <Tasker.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 <Tasker.h> and the sketch compiles, with no download, no zip and no Library Manager.

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

The calls you will use

In the order a sketch meets them.

Tasker tasker

Makes tasker, the Tasker object the calls below are made on.

tasker.isScheduled(blink)

Returns true or false for "is scheduled".

tasker.setInterval(blink, 100)

Sets the interval to the value you pass in.

tasker.setRepeated(beep, 200, 3, 1)

Sets the repeated to the value you pass in.

tasker.setTimeout(later, 1000)

Sets the timeout to the value you pass in.

Example: Blink on an interval, three beeps on setRepeated, one timeout

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 <Tasker.h>

Tasker tasker;
int beeps = 0;

void blink() {
  digitalWrite(13, !digitalRead(13));
}

void beep(int n) {
  beeps += n;
  Serial.print("beep ");
  Serial.println(beeps);
}

void later() {
  tasker.cancel(blink);
  Serial.print("blink scheduled: ");
  Serial.println(tasker.isScheduled(blink) ? "yes" : "no");
}

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  tasker.setInterval(blink, 100);
  tasker.setRepeated(beep, 200, 3, 1);
  tasker.setTimeout(later, 1000);
}

void loop() {
  tasker.loop();
}

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

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