Multitasking is the ability of a microcontroller to execute several tasks or processes over the same time horizon. In practice, an Arduino cannot execute tasks in parallel, but it can arrange and execute a number of tasks one after the other in a very short space of time. This gives the illusion of multitasking. In this article, we’ll look at a few examples of multitasking.
If your project requires you to run tasks in parallel (multithreading), you’ll need to consider other microcontrollers or microcomputers.
Using interrupts
Description
Interrupts enable the microcontroller to execute a function when an event occurs on one of the interrupt pins. Rather than constantly reading the value of a sensor, the program will only trigger when the sensor value changes. This solves many task layout problems.
Code
const byte interruptPin = 2; void setup() { pinMode(interruptPin, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(interruptPin), onEvent, CHANGE); } void loop() { } void onEvent() { Serial.println(F("Action")); }
Using the millis() function
Description
The millis() function returns an unsigned long variable representing the number of milliseconds elapsed since the microcontroller was powered up. In particular, it allows you to perform an action when a time interval has elapsed without blocking the rest of the program.
Code
unsigned long currentTime=0; unsigned long previousTime=0; bool ledState=LOW; void setup() { Serial.begin(9600); } void loop() { currentTime=millis(); if((currentTime-previousTime)>200){ previousTime=currentTime; Serial.println(F("Action ")); } }
Using the microprocessor’s built-in timers
Timers are microcontroller registers that increment when a clock pulse is received.
Using Timer libraries
Arduino time management libraries are numerous. As a general rule, they interface with the Arduino’s timer registers. To name the best-known: TimerOne.h MsTimer2.h, TimerThree.h
Using task management libraries
These libraries allow tasks to be executed in an orderly fashion, giving the impression of multithreading.
Examples include TaskManager, ProcessScheduler, ArduinoThread or FreeRTOS. Find these libraries in the Arduino reference list.
Multitasking with the Arduino Due board
For boards based on SAM architecture, such as the Arduino DUE, there’s a library that lets you manage multiple tasks in different loop() functions. This also works for Zero, MKRZero and MKR1000 boards.
Multi-core microcontroller: ESP32
The NodeMCU ESP32 microcontroller is equipped with a dual-core microprocessor and uses the FreeRTOS OS, enabling it to run tasks in parallel.
Multithreading with Raspberry Pi
The Raspberry Pi is a microcomputer, which means it can perform several tasks at the same time. What’s more, its microprocessor is equipped with four cores, enabling it to perform true multitasking. You can, for example, run several Python scripts or C++ programs (or others) at the same time. It’s also possible to use multithreading libraries within a Python script.