Multithreading with Arduino
So far have managed nicely to write partly simultaneous stuff with interrupts, schedulers etc, but having some headache now when trying to really do some multithreaded stuff. For example:
- RGB LED color cycling/pulsing and controlling a stepper motor at the same time
- Multiple LED color algorithms which should be run simultaneously
It seems that the only way to go is to break the algorithm logics totally down in small pieces and just call the methods/classes from the main loop. For example with LED dimming just do one step color change, exit back to the loop and do eg. next stepper movement and over again. However, this basically means that majority of the nice LED color algorithms need to be heavily restructured to get rid off all loops and delays/waits. Also wondering if MySensors logic will actually have time to do anything because of the quite busy scheduling this will generate. Have anyone some nice ideas or tips to overcome these challenges?
Another alternative is to go multi-core: Use different Arduinos for each task and let the Arduinos communicate using Serial, SPI or i2c. Arduinos are cheap, and each sketch is easier to develop, maintain and troubleshoot than having one big sketch with everything.
Sushukka last edited by Sushukka
@mfalkvidd Thanks for the quick answer. I try to keep controllers well utilized before splitting the workload between several controllers. In current case I only need to control one 10W RGB LED and a stepper motor. This is connected to optic fibre star light ceiling where there are different programs to run. In front of the LED light source there is screen plate with random holes on a stepper motor creating blinking effect if wanted. This shouldn't be too hard to Arduino to handle. Just need chop my old LED color algorithms so that I can fit the stepper motor movements in (or more likely other way around as stepper motor requires finer controlling)
But then there is another project upcoming where there are stairway and about 12m WS2812 addressable LED strip waiting. On there clocked up ESP8266 and Fastled libraries are probably must. Also need to think about the wiring carefully.
Anyway multitasking requires gimmickry it seems. Having two core Arduinos or ESP8266s would be nice.
- optimized state machine
- ARMs have more power than simple 8bits, or esp32 for multicore
- or when you need critical timing, like mfalkvidd said, it's possible to separate function in multiple ICs
in all case, you will need some coding..
@Sushukka I developed a LED pattern blinking library that does all the blinking/fading for mono or RGB leds for you.
You just have to call an update function at regular intervals; that's it.
Maybe it can be of any use to you: https://github.com/Yveaux/LedPattern
vikasjee last edited by
Can this easily be modified for LED and Tone?
Is there any other library to do so?
@Yveaux Thanks Yveaux. I'll use this as a model with Adafruits multithreading examples. I want to learn how to handle this by myself because there are lots of projects where I need to handle parallelism somehow (not only leds). So far I have used basic scheduler based on "if currentMillis - previous > interval" style, but it's just a beginning. By using methods and program wide variables is a bit dirty way to accomplish this but so far been easiest to use. Now need to properly isolate these to own classes to keep the code clean. Getting rid of loops will need some thinking, but the biggest problem seems to be in my mind to undestand that eg. 10ms is enough to do lots of things. Ofc this is harder with 16MHz Arduino than 80/160MHz ESP8266. Anyway, this is an interesting area and I'm going to enjoy diving deeper to get more out of these already amazing microcontrollers.
Hehe, took couple of hours but now have four led sequences running "in parallel" on Wemos D1 Mini. Three fades with random intervals and one flickering led with random intervals imitating noise in electric net. Also couple of static leds running on same board. Seems that MySensors have no problem to get its slice of the cake so I'm happy now.
Btw, these leds are implemented in this:
For a perfectionist took terrible amount of time to build, but looks pretty awesome now.