Skip to content

Multitasking

In this tutorial you will learn how to control many different tasks at the same time and how to execute high priority tasks interrupting all other tasks.

Table of contents:


Equall timing for on and off state.

Non-equall timing for on and off state.

Eeee... How to blink more than one LED?!



Non-equall timing for on and off state.

Problems when:

The disadvantage of this solution is that every delay affects both leds. It may be difficult to control both leds independently. It may happend that changing timing of one led will force you to change code dractically.

Try to find extensible solution.

Equall timing for on and off state with only one led.

Non-equall timing for on and off state.

Because counterRedOff and counterRedOn are increased inclusively (either counterRedOff or counterRedOn) you can use one counter as it is showned bellow:

Non-equall timing for on and off state of two leds.

Problems with this improvement:

Lack of control over code "outside" delay -- it may take as much time as it want. In consequence, delay(1000) is not equall 100*delay(10). It means that you have to "collect" 100 delay(10)s regardles of the time passed between every two delay(10)s.


Non-blocking equall timing for on and off state.

Non-blocking non-equall timing for on and off state.

Non-blocking non-equall timing for on and off state of two different LEDs.



Interrupts

Add delay(10000) to simulate time-consuming task

Digital pins usable for interrupts:
Uno, Nano, Mini, other 328-based: 2, 3
Micro, Leonardo, other 32u4-based: 0, 1, 2, 3, 7
Mega, Mega2560, MegaADK: 2, 3, 18, 19, 20, 21 (pins 20 & 21 are not available to use for interrupts while they are used for I2C communication)