# Moore Finite state machines in programming

#### Vindhyachal Takniki

Joined Nov 3, 2014
593
Explains use of Moore FSM in embedded. Who else use Moore FSM or any other FSM like Mealey?
2. I am used to superloop concept in which I design my own state, step by step. I had tried to put this Morre FSM in my ealier written code for ECG code. I found it extremely difficult by Moore FSM. Since there are so many tasks running, interconnections with ISR, multiple inputs , multiple outputs & other things, I got stuck. What is better method in these kind of situation.

#### Papabravo

Joined Feb 24, 2006
19,873
I have used both types on numerous occasions in hardware and in pure software. It is a very powerful technique.

#### JohnInTX

Joined Jun 26, 2012
4,713
FWIW I use state machines in virtually everything I write unless the chip supports a proper RTOS. After you get the hang of it, it makes coordinating many tasks much easier.

#### Vindhyachal Takniki

Joined Nov 3, 2014
593
Made a code from his course for street light.

Code:
enum
{
goN = 0U, waitN, goE, waitE
};

typedef const struct
{
uint32_t output;
uint32_t wait;
uint32_t next[4];
}street_light;

street_light sl_fsm[4] = {
{
0x21U,
100U,
{goN, waitN, goN, waitN},
},
{
0x31U,
100U,
{goE, goE, goE, goE},
},
{
0x41U,
100U,
{goE, goE, waitE, waitE},
},
{
0x51U,
100U,
{goN, goN, goN, goN},
},
};

{
uint32_t state = goN;
uint32_t input;
uint32_t output;

while(1)
{
output = sl_fsm[state].output;       /* set tthe output */
wait_delay_us(sl_fsm[state].wait);   /* delay for spcified time */
input = goN;                         /* input is goN for testing, otherwise input is from pins */
state = sl_fsm[state].next[input];   /* determine next state */

/* to remove compilre warming */
if(output)
{
__nop();
}
}

} /* function ends here */

#### WBahn

Joined Mar 31, 2012
28,191
Explains use of Moore FSM in embedded. Who else use Moore FSM or any other FSM like Mealey?
2. I am used to superloop concept in which I design my own state, step by step. I had tried to put this Morre FSM in my ealier written code for ECG code. I found it extremely difficult by Moore FSM. Since there are so many tasks running, interconnections with ISR, multiple inputs , multiple outputs & other things, I got stuck. What is better method in these kind of situation.
I prefer Moore machines, but sometimes a Mealy machine is simply a much better match to the application.

You can use an informal, ad-hoc design approach, which is often just find for small machines, or you can adopt a more formal, standardized approach, which can be needlessly cumbersome on small machines but can make the design of larger machines much more robust.

If doing it in software, FSM implementations lend themselves to switch() statements (if your language supports that). I find that I can implement the machine with more confidence if I set all of the relevant actions, including the next-state assignment, within every case. In and HDL this helps eliminate inferred latches, but in an MCU or other software it tends to slow down the machine because it has to do a lot of assignments that serve no purpose. But you can always go in and comment out the ones that you determine are truly redundant (I don't delete them because they serve as good documentation for what I expect those signals to have in that state.

#### xennar

Joined Jul 10, 2013
1
I'd been working on a table based state machine framework for Arduino when I read your discussion. The idea is that each state machine is its own object which can be shared like any Arduino library. It comes with a number of reusable state machines.

I'd be interested to hear your thoughts.

The code and doumentation are on Github:
https://github.com/tinkerspy/Automaton
https://github.com/tinkerspy/Automaton/wiki

Regards,
Tinkerspy