With the increased capabilities of Arduino and other microcontroller boards, including faster clocks or even multiple cores, the need to handle multiple tasks simultaneously arises more often than in the past. For instance, you often want to control motors, update a display and detect user interactions at the same time, or perform tasks that have different timing or wait for external events.
The traditional way to do this is to write non-blocking code so that the loop() function can run as fast as possible, updating state variables and calling the millis() function to ensure proper timing (see the “Blink without delay” example to learn more). This approach leads to bloated code, though, which is hard to debug and maintain, and also does not support multiple cores.
The Scheduler library already allows writing code in a cleaner way by splitting the sketch in multiple loop functions, so that each one can focus on a specific task. However, this approach is called cooperative multitasking, which means you still need to avoid blocking commands yourself. Also, it does not support multiple cores and even if it did it doesn’t protect you from accessing the same variables from multiple threads. Last but not least, it’s based on the traditional “busy loop” paradigm, which doesn’t really help for low power applications where you want to have threads rest as much as possible while waiting for an event or incoming data.
How to add multitasking to Arduino?
The goal is to define a standardized API that can be ported across all architectures and that, in line with the Arduino philosophy, will make complex things easy for anyone. Multitasking is a hard concept, so here we have plenty of margin to bring the Arduino approach to make this available to everyone.
We’re asking our technical community to join the discussion on GitHub, and we’re sharing our API proposal along with a fully working implementation as well. Let us know your thoughts and let’s work together to finalize this new feature!
This community discussion is hosted within the brand new repository, which is the new home for the evolution of the Arduino language.