Pages: [1]   Go Down
Author Topic: async state machine for microcontrollers  (Read 593 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, this helps. I also found

http://www.state-machine.com/arduino/

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I also found a link in playground

http://arduino.cc/playground/Code/QP

Any pros or cons on using this?
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: [1]   Go Up
Jump to: