Using State Machines In Your Projects

arduino, 16 button keypad and LCD display

 

[Tony] has developed a method of using a state machine to validate keypad inputs. His method checks the commands character by character as they are entered in by a 16 button keypad. State machines are often used to break down complex problems into sequential tasks, making code development easier. While [Tony’s] example uses the keypad, Arduino Uno, and a character LCD, the theory can be applied to numerous projects, such as this Dahlander motor switch.

As you see, state machines can be very versatile. Stick around after the break as we take a look at [Tony’s] state machine and provide a brief explanation of how it all works. 

The goal is to ensure a command is entered in to a system correctly – in this case it is being validated character by character with each key press. A state machine is used to achieve this goal. The command is:

 

XX@HH:MM#
Where:
XX = 1-99
HH = 0-24
MM = 00-59
# = Execute

 

Each value is considered a state. When the value is entered, it moves to the next state. So:

 

state machine flow diagram

 

Be sure to check out [Tony’s] project for more details and learn how he implements the above state machine in code.

 

21 thoughts on “Using State Machines In Your Projects

  1. State machines are the bomb(I know, I know….stuck in the 90’s) in certain situations. While I worked for a certain french tire maker that starts with “M”, we had a french tool called grafcet that was an elegant way of designing your SM. it specified the condition(s) to transition to the next state and any outputs for a given state….all in all a beautiful thing. We used them a LOT when designing PLC ladder logic programs.

    1. I also love state machines. I’ve found them much easier to debug, maintain, and read than a mess of ‘if…then’ statements with flags or other crazy stuff. There might be more ‘baggage’ or other up front work, but when it needs to be debugged (either the coding or fixing the end result because functionality requirements have changed) i just find it less confusing. Additionally, as with many other pieces of code, the right names can make documentation easier to understand.

      1. State machines are also excellent whenever you need to do several tasks at once. I.e. state machines never block code execution, and all the state info the machine needs is inside the machine. So assuming no variable name collisions and that the scan rate is fast enough, multiple machines happily coexist in a single code loop or ISR.

      1. 1. Enums get better type checking by the compiler
        2. Enums tend to get better treatment in debuggers (e.g., when printing variable values)
        3. Enums tend to get better treatment from static source code analysis tools than values in the pre-processor.

        1. 4. You can create enums without defining their integer value (and this removes a source of errors).
          5. You can now pass the enum type to functions, making it a lot more clear what this function expects, think: void switchSystemToState(StateMachine_type state); In this case the reader of your code could easily jump to the enum definition to find out which states there are.

          Note: I usually use enums in combination with a typedef statement to get rid of that “enum foo” clutter: http://stackoverflow.com/a/385033/849959

          1. 6. You can put a “first entry” and “last entry” item in the enum list and bounds-check against them:

            if((state > START_OF_LIST) && (state < END_OF_LIST))
            {
            // State is valid
            }

            As long as you add any new items between those two, the bounds move to suit without having to edit code.

      1. Not necessarily
        My microwave will accept 99:99 and count down. When it gets to 99:00 it goes to 98:59.
        I use this trick when I want a minute and a half, punching in 90 takes one less key stroke than 1:30

  2. State machines are also kinda nice when you have to parse human supplied text. I’ve used it most frequently when scraping html, looking for specific tags/attributes. The interesting bit is that when I learned state machines, it wasn’t in programming class – rather, it was Digital Systems Design which was a mostly hardware class (74xx series chips).

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.