TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Introducing multitasking to Arduino

186 pointsby pyprismalmost 3 years ago

14 comments

primitivesuavealmost 3 years ago
A couple years ago, I managed a summer&#x2F;afterschool program that introduced thousands of kids (age 8-18) to the Arduino. When we would set up a software abstraction to simplify multitasking (something that came up often with more advanced students), we set up something like this:<p><pre><code> int time = 0; int max_period = 10000; void loop() { if (time % 100 == 0) { everyTenthSecond(); } if (time % 1000 == 0) { everySecond(); } &#x2F;&#x2F; ... time = (time + 1) % max_period; } </code></pre> This `loop` assumes functions are perfectly non-blocking, which obviously is not always true, but works well for 99% of actual use cases. If keeping accurate time is required, the student would compare `time` with the elapsed millisecond time since the last loop invocation, adjust accordingly, and check for any periodic functions that weren&#x27;t executed in that period. It gets tedious quite fast.<p>If I had to imagine a solution, it would be to simply have an easy way to enter parameters to the JS-equivalent of `setTimeout` and `setInterval`.
评论 #32323846 未加载
评论 #32322647 未加载
评论 #32325406 未加载
评论 #32327445 未加载
评论 #32322815 未加载
评论 #32326027 未加载
xkcd-sucksalmost 3 years ago
I&#x27;m crappy at embedded development but isn&#x27;t it pretty normal to handle &quot;multitasking&quot; with interrupts instead of using an event loop?
评论 #32321382 未加载
评论 #32321938 未加载
评论 #32324612 未加载
评论 #32322255 未加载
评论 #32322917 未加载
评论 #32321911 未加载
eldruinalmost 3 years ago
In Rust there is <a href="https:&#x2F;&#x2F;embassy.dev" rel="nofollow">https:&#x2F;&#x2F;embassy.dev</a> which is an async Rust framework for embedded. It has been shipping in products for years. Can be made real-time (for some definitions of it).<p>There is also RTIC <a href="https:&#x2F;&#x2F;rtic.rs" rel="nofollow">https:&#x2F;&#x2F;rtic.rs</a> which is a concurrency framework for real-time systems using interrupts. IIRC the car industry is interested in it.
评论 #32329410 未加载
fmakunboundalmost 3 years ago
Forths such as FlashForth <a href="https:&#x2F;&#x2F;www.flashforth.com&#x2F;index.html" rel="nofollow">https:&#x2F;&#x2F;www.flashforth.com&#x2F;index.html</a> on the Arduino support multiple tasks .. in addition to including a compiler and interactive REPL
ramaryalmost 3 years ago
We&#x27;ve done quite a bit of experimentation with adding preemptive multitasking support to non-hard real time software running on MCUs at my current company.<p>After a lot of head banging and dead ends, I&#x27;ve come to the conclusion personally that the embedded community could really use an implementation of the POSIX threading APIs or some meaningful subset thereof for various platforms. They&#x27;re already standardized, well understood&#x2F;used, and aren&#x27;t that hard to implement on an MCU.<p>Of course, there would probably be some semantics that wouldn&#x27;t make sense to or may not be possible to implement on MCUs, and there would be work required to support different cores, but these tradeoffs seem better to me than reinventing the wheel and probably needing to make the same tradeoffs at some point down the line with a ground up new API.<p>For Arduino, exposing POSIX APIs wouldn&#x27;t be very user-friendly. But wrapping something more user-friendly around them seems like a maintainable and extensible path for the project and community.
评论 #32323132 未加载
评论 #32323346 未加载
评论 #32327487 未加载
OlaChalmost 3 years ago
Energia IDE was a good example of multitasking for MCU. Here is an example: <a href="https:&#x2F;&#x2F;energia.nu&#x2F;guide&#x2F;foundations&#x2F;programming_technique&#x2F;multitasking&#x2F;" rel="nofollow">https:&#x2F;&#x2F;energia.nu&#x2F;guide&#x2F;foundations&#x2F;programming_technique&#x2F;m...</a>
fake-namealmost 3 years ago
ITT: Arduino people re-implement FreeRTOS badly.<p>Seriously, there are SO MANY mature, well-understood RTOS implementations for MCUs out there already, they really, really should just use one.
syncurrentalmost 3 years ago
Synchronous programming is also a good choice IMHO to handle multiple concurrent activities in a simple way on an embedded system.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;frameworklabs&#x2F;proto_activities" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;frameworklabs&#x2F;proto_activities</a>
ge96almost 3 years ago
Sounds good. I was working on a project where I could not receive websocket info until I was done running servo commands.<p>I know you can have multiple boards but yeah.
andrewstuartalmost 3 years ago
async&#x2F;await does not result in bloated code that is hard to maintain and debug.<p>It&#x27;s a strange thing to say - async&#x2F;await is now built in to most major programming languages - strange that Arduino would dismiss it so easily.<p>They are correct in that async&#x2F;await does not solve multicore utilisation, but it&#x27;s the most important and easiest way to implement parallelism on a single core.
评论 #32328202 未加载
mikewarotalmost 3 years ago
I see an opening for a very good set of tutorials on how to avoid all the pitfalls.<p>[edit] - Actually... they have some good points, I just worry about someone thinking they can just &quot;sprinkle threading&quot; into their code, and failing to understand that in doing so they <i>change the laws of physics</i> of the code.
评论 #32323521 未加载
oconnor663almost 3 years ago
I wonder if async Rust would be a good fit for this use case? It doesn&#x27;t solve the accidental blocking problem (although async-std did experiment with blocking detection at one point), but you can get transparent support for multiple cores and a lot of thread safety.
评论 #32328171 未加载
dariosalvi78almost 3 years ago
I like the simplicity of setTimeout and setIntetval of JavaScript. Maybe something similar plus some mutex primitive (which JS doesn&#x27;t need)?
HeyLaughingBoyalmost 3 years ago
Nice, but pointless addition to an old processor. ESP32&#x27;s are cheap and readily accessible and they come with FreeRTOS running already.<p>If I absolutely need to multitask on a Mega328 Arduino, I use the protothreads library. But really, for anything heavy, I&#x27;ll turn to an ESP32 or an STM32. Use the right tool for the job!
评论 #32321621 未加载
评论 #32321323 未加载
评论 #32321579 未加载