Go Down

Topic: async state machine for microcontrollers (Read 828 times) previous topic - next topic


Ive used single thread async state machines over the years in ms windows to control hardware. Now that im playing with arduino boards i would like to know if there are any libraries or discussions on implementing this pattern on an arduino. Thanks in advance. Doug


i would like to know if there are any libraries or discussions on implementing this pattern on an arduino.

That being the only "pattern" that really works on a microcontroller, what do you need a library for? Or discussion, for that matter? The whole concept is simply fundamental to the way microcontrollers work.


I guess i dont really understand these controllers. In ms sindows c++ i can use windows message dispatch, in c# i could use event-delegates. You suggest these are built in, i guess i just missed it. I thought there might be a post-dispath library somewhere but i guess i dont need it. Thanks.


You suggest these are built in

No. The Arduino runs a single thread. It is up to you to deal with events as they happen, including discovering that the events have happened.

A good example is serial data arriving. Periodically, you need to check to see if that has happened. That is where Serial.available() is useful. You then deal with (by collecting and storing) all pending serial data, until there is no more, or until the collected data represents a complete packet. Then, you deal with the collected data, and reset to be ready to collect more.

There are other asynchronous events completion tasks that trigger interrupts, such that you can provide an interrupt handler to deal with them. For instance, reading an analog pin typically takes time. You can just wait until the reading is complete, or you can use the asynchronous method (Nick Gammon has documented it) to start a reading happening and to be notified when it is complete.


Thanks, this helps. I also found


Afetr some lengthy searching.

Most of my apps can use a simple loop, but hey, i also want to have some fun programming as well.


I also found a link in playground


Any pros or cons on using this?


Roll your own to fit your needs, your algorithm.

Given setup() and loop() to address real time tasks, some form of state machine approach usually makes the most sense.

My code reads and evaluates serial data as it comes in, not starting after collection. I generally need less buffer space and string manipulation code. State machine method is very useful when reading and compiling data on-the-fly.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131