The Arduino library LedTask shows the power of non-preemptive multitasking. LedTask shows an elementary example with four LEDs, and these run close to independently of each other.
try it and modifythe source code for exploration.- use it as a starter and move on to a more
sophisticated variantlike Makuna Task get inspiredand explore what more Arduino has to offer. Search in Library Manager - formultitask.
The purpose of this library is to be a practical eye-opener and encourage writing Arduino code differently without inefficient delay() calls.
Instantiation of the object and assigning the pin number:
LedTask Led1 = LedTask(7); // Control a LED
LedTask Led2 = LedTask(8); // Control another LED
LedTask Fan = LedTask(12); // Control a DC motor with PWM
Setup variants:
Led1.begin(100, 400); // LED time: on_ms,off_ms
Fan.begin(5.0); // Low frequency (5 Hz) PWM output on the 'Fan' pin
Loop methods usage:
Led1.updateBlinkLed(); // Blink led with set on/off timing
Fan.updatePwmTask(40); // Use 40% duty-cycle, with set frequency
An attached LED can use PWM to dim the intensity. See below for the complete library documentation. There is one last method, which is blocking (uses delay() calls, and thus should not be used in the Arduino loop():
// This produces a burst of three blinks, with 100ms on time and 150 ms off time.
Led2.pulseLedBlk(3, 100, 150);
Connect either four individual LEDs with a limiting 1 kOhm resistor to VCC, or get yourself the TinyLedSwitch - A Universal Breakout Board. Please see below for how to purchase it.
| Platform | Digital Pin# | Led# Cathode |
|---|---|---|
| Arduino Uno | 12 | Led1 |
| Arduino Uno | 13 | Led2 |
| Arduino Uno | 8 | Led3 |
| Arduino Uno | 7 | Led4 |
Find four free digital pins for any other microcontrollers and adjust them in the Arduino example sketch.
In the Arduino IDE, scroll down the long list below File->Examples and find LedTask.
Upload the code, and the four LEDs flash seemingly independently of each other.
Each instance of a LedTask requires just three lines of code:
- one to declare the instance
- one to setup timing in the setup
- and one call to update the loop
LedTask LedOne = LedTask(12);
void setup() {
// on_ms,off_ms
LedOne.begin(100, 400);
}
void loop() {
LedOne.updateBlinkLed();
}Another exciting example worth looking at is the capability to separate a short key press from a long key press.
Click on the green Library Manager badge above for instructions,
or use the alternative manual installation procedure.
- Navigate to the Releases page.
- Download the latest released ZIP archive in
~/Arduino/libraries. - Unzip the archive.
- Rename the new directory. Remove version-code, or master in the name like this for
LedTask. - Restart Arduino IDE.
- In Arduino IDE scroll down the long list below
Sketch->Include Libraryand findLedTask. Do you like the tiny breadboard-friendly 'LEDs, and switches break-out board'?
You can purchase all the latest designed boards on Tindie.
We appreciate your support.
The library API changed in release v0.2.0.
For a good starting point for understanding non-preemptive multitasking, thank Bill Earl and his write-up on the subject in his article.


