Introducing multitasking to Arduino
August 2nd, 2022—
With the elevated capabilities of Arduino and different microcontroller boards, together with quicker clocks and even a number of cores, the necessity to deal with a number of duties concurrently arises extra typically than prior to now. As an example, you typically wish to management motors, replace a show and detect person interactions on the identical time, or carry out duties which have completely different timing or look forward to exterior occasions.
The standard approach to do that is to write non-blocking code in order that the loop() perform can run as quick as attainable, updating state variables and calling the millis() perform to make sure correct timing (see the “Blink immediately” instance to study extra). This strategy results in bloated code, although, which is difficult to debug and keep, and likewise doesn’t assist a number of cores.
The Scheduler library already permits writing code in a cleaner approach by splitting the sketch in a number of loop capabilities, so that every one can concentrate on a selected process. Nonetheless, this strategy known as cooperative multitasking, which suggests you continue to have to keep away from blocking instructions your self. Additionally, it doesn’t assist a number of cores and even when it did it doesn’t shield you from accessing the identical variables from a number of threads. Final however not least, it’s primarily based on the standard “busy loop” paradigm, which doesn’t actually assist for low energy purposes the place you wish to have threads relaxation as a lot as attainable whereas ready for an occasion or incoming knowledge.
Tips on how to add multitasking to Arduino?
The purpose is to outline a standardized API that may be ported throughout all architectures and that, according to the Arduino philosophy, will make advanced issues straightforward for anybody. Multitasking is a tough idea, so right here now we have loads of margin to deliver the Arduino strategy to make this obtainable to everybody.
We’re asking our technical neighborhood to be part of the dialogue on GitHub, and we’re sharing our API proposal together with a completely working implementation as effectively. Tell us your ideas and let’s work collectively to finalize this new function!
This neighborhood dialogue is hosted inside the model new repository, which is the brand new residence for the evolution of the Arduino language.