Finite State Machine Using Switch Statement.

Hi

I fully agree to the previous post. Additionally i can give some examples including state diagram and code.

My none-Arduino project to drive several power LEDs includes the user interface as state machine here GitHub - olikraus/tinyledlight: Automatically exported from code.google.com/p/tinyledlight. The state machine is implemented with switch case statements and is available here: GitHub - olikraus/tinyledlight: Automatically exported from code.google.com/p/tinyledlight

State machine for a rotary encoder software, part of m2tklib for Arduino. Sketch and state diagram are available here: Google Code Archive - Long-term storage for Google Code Project Hosting.. The state machine is implemented as "next step" lookup table.

Oliver

ok i see the task that im tying is way over my knowledge, urm maybe i should try and learn more or learn while doing it?
yes i am comfortable with to draw state transition diagrams and state transition tables but C++ is something i am still lacking but i really do want to learn this well

is there any simple project that i could do to enhance my understanding of Finite State Machine?

during my search i had found some interesting site related to this matter

http://www.mcgurrin.com/robots/?p=54

the more i search and trying to understand what is Finite State Machine, the more i understand and the more i realized how much i need to know when it come to programming . however i cant help but to see the similarity between Finite State Machine with the step sequence that i use to used when programming PLC

if(state=="try"){ //do actions here }
if(state=="it"){ // ....}

basically it be done like that.

thanks. i didnt thought of that

Switch and functions will certainly help you to build a state machine. Sometimes your tests and state transitions are simple enough that they can go directly in the switch. I like to keep loop small enough (like any other function) that it can be read without scrolling though, so generally, I'll call a function that handles each state from the (then) simple switch. Here's an example I posted a while back that illustrates both:

http://arduino.cc/forum/index.php/topic,106335.0.html

Sometimes, the system is so simple that a state machine is hardly worth it, but it still makes a nice framework. Also, if functionality is added later, you don't find yourself cursing that you should have used the concept from the start. This one shows this:

http://arduino.cc/forum/index.php/topic,59511.0.html

Here's another example, pretty pure, one function to handle each state:

http://arduino.cc/forum/index.php/topic,71867.0.html.

Repeating Jack's advice, if you use this switch and function call model, the state machine practically writes itself - as long as you have specified your states and possible transitions up front.

olikraus:
My none-Arduino project to drive several power LEDs includes the user interface as state machine here GitHub - olikraus/tinyledlight: Automatically exported from code.google.com/p/tinyledlight. The state machine is implemented with switch case statements and is available here: GitHub - olikraus/tinyledlight: Automatically exported from code.google.com/p/tinyledlight

Nice state diagrams! What did you use to draw them?

ash901226:
the more i search and trying to understand what is Finite State Machine, the more i understand and the more i realized how much i need to know when it come to programming . however i cant help but to see the similarity between Finite State Machine with the step sequence that i use to used when programming PLC

The first job then would be getting comfortable with C/C++ then. OO coding isn't needed to do decent FSMs. Start with something simple, three or four states at most. Being able to understand the problem at hand and visualize it in terms of states can take a little practice, but usually comes pretty quickly.

Lots of examples are available, several have been offered in this thread. A lot of good FSM exercises can be done with an Arduino, some switches to represent inputs, and LEDs to represent outputs. I'll offer one more at the risk of giving you more examples than you can enjoy, it's an implementation of the common garage-door controller project that a lot of folks have done. At this point it only senses the state of the door and lights up some LEDs accordingly, and just needs two switches and some LEDs to implement. I think it has four or five states at most. Not much code, and I also have state diagrams and tables to go with.

You can think of other examples and invent your own exercises. Lots of things in everyday life can be seen as FSMs. Appliances like washing machines go through a series of states and control valves and pumps. Traffic signals and crosswalks. See Wikipedia's article for a simple FSM representing a turnstile.

I've never programmed a PLC, but yes I wouldn't be surprised if there were a lot of similarities, so you probably have some good background there in thinking about the inputs, outputs, and states that represent a system.

yes thank you. The more i study what is Finite State Machine, the more i realize that most of its logic had been teach to me by my sensei. I actually use it b4 during my time working in the automation industries which actually give me some credential as a programmer back then, but that was only in ladder diagram and PLC. And yes as we speak i actually have been trying various task that i used to do while i was working. wow this really open up my eye to a whole new work of possibilities. I mean b4 i uses to think that PLC and arduino style programming have very little similarities but now, WOW!!! and i really mean WOW!!!!! all this actually click in my head rite now.
for instant now i can see how i could actually program a whole machine with this knowledge alone.

Understanding the FSM is a bit like finally getting the blink without delay example. Once you see it, there's very much a feeling of "Is that it?". Before that though, it seems baffling, especially in the abstract. Examples help - a lot.

one thing about PLC is that every thing is represented with HIGH and LOW. which really help. with arduino the possibility to represent something as HIGH and LOW and everything in between, WOW now everything i learn from my sensei could finally put to use. He thought me about Fuzzy logic and Sequence step, timing diagram. then he thought me some automation and finally b4 i when to work he thought me about Finite State Machine, although at the time he did not tell me its name just that it is an advance form of Sequence step. but now everything make sense now. So Thank you all for actually making me realize that my sensei had thought me all of this all along. Urm you know suddenly i have the confident in me to actually do any advance work with arduino.
Again i cant stress enough how much I'm Thanking all of you.... thank you thank you thank you ......

as a sign of my appriciation i give every one that reply to this post an approud

I found this nice/easy example to get started with

http://digital-diy.com/general-programming/finite-state-machines.html

one thing about PLC is that every thing is represented with HIGH and LOW. which really help. with arduino the possibility to represent something as HIGH and LOW and everything in between,

You must have never worked with a PLC having analog input modules as they are available and often used to read industrial 4-20ma current loops?

Lefty

i had once used analog input for PLC but only once and i really didnt get the chance to explore its capability and Analog input is such a premium that most company prefer using only the digital IO

Jack Christensen: Not sure if you found the software, but I recognize it as "Graphviz". Text-based descriptions make the graphics, and it's very cool.

www.graphviz.org/content/fsm

hockeyrink:
Jack Christensen: Not sure if you found the software, but I recognize it as "Graphviz". Text-based descriptions make the graphics, and it's very cool.

www.graphviz.org/content/fsm

Very cool, thank you!