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 usually than previously. For example, you usually need to management motors, replace a show and detect consumer interactions on the similar time, or carry out duties which have completely different timing or look forward to exterior occasions.
The standard means 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 be taught extra). This method results in bloated code, although, which is difficult to debug and keep, and in addition doesn’t assist a number of cores.
The Scheduler library already permits writing code in a cleaner means by splitting the sketch in a number of loop features, so that every one can concentrate on a particular activity. Nonetheless, this method known as cooperative multitasking, which implies you continue to must 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 based mostly on the normal “busy loop” paradigm, which doesn’t actually assist for low energy purposes the place you need to have threads relaxation as a lot as attainable whereas ready for an occasion or incoming information.
The best way to add multitasking to Arduino?
The purpose is to outline a standardized API that may be ported throughout all architectures and that, consistent with the Arduino philosophy, will make advanced issues straightforward for anybody. Multitasking is a tough idea, so right here we have now loads of margin to carry the Arduino method to make this out there to everybody.
We’re asking our technical neighborhood to be a part of the dialogue on GitHub, and we’re sharing our API proposal together with a totally working implementation as effectively. Tell us your ideas and let’s work collectively to finalize this new characteristic!
This neighborhood dialogue is hosted inside the model new repository, which is the brand new residence for the evolution of the Arduino language.