Skip to main content

Section 2.6 State Machine Design Pattern

This is the reading for Day 4. The goal is for you to arrive able to draw a state diagram, because on Thursday we’ll turn your own button code into a state machine and then design new ones from scratch.
A finite state machine (FSM) is a way of thinking about code that is especially useful for event-driven programs such as those running on embedded systems. It describes a program as a finite number of possible states, with transitions between them that capture the behavior and the logic. Think for example of a simple traffic light. It has three states: red, green and yellow. (Things get more complex once we add states such as red blinking or yellow blinking, so we won’t do that here.) Once the light is in state red it stays there until a certain amount of time has passed, at which point it transitions to the green state. (Let’s keep this simple too and not think about traffic-aware lights.) The timer going off to let the light know it is time to change is what we would call an event, and the switch from red to green a transition. Once the light is green it stays in that new state until another event occurs, namely another timer that indicates it is time to switch to yellow. The timer going off is the event again, and the transition is the switch to yellow. Next, yellow transitions back to red after another amount of time has passed.
We’ll use this pattern in our code. We keep track of what state our system is currently in via a state variable, and in our infinite loop we check only whether an event has happened that merits a state transition. So we never wait for a transition signal in a blocking while loop.
So a state is what the program remembers about where it currently is: not the value of a pin right now, but which of a small number of situations it is currently in. An event is something that happens and that the program notices, such as a pin going LOW or an interval elapsing. A transition is the move from one state to another that an event causes, and the action is whatever the program does on the way. A state machine is nothing more than a list of the states, and for each one, which events move it to what new state and what happens along the way.
Recall blinkySEQ.c from Lab 1: the program lights three LEDs in turn, and we can think of that as a machine with three states, one for each LED (Figure 2.6.1). The only event is the half-second interval elapsing. Each transition’s action is to turn the LED that is currently on off, light the next one, and restart the timer; after the third it wraps back to the first. Written as a diagram, circles are the states, arrows are the transitions, and each arrow carries the event that triggers it and then, after a slash, the action taken.
Figure 2.6.1. The running light of blinkySEQ.c drawn as a state diagram. Three states, one per LED; one event, the half-second interval elapsing; and three things done on each transition, which are to turn the LED that is on off, light the next one, and restart the timer. The machine wraps from the third state back to the first, which is what makes the light run continuously.
Notice what the diagram makes obvious and what is a bit harder to see from code alone: there are exactly three situations the program can be in, and exactly one thing that can move it between them. That is the value of drawing a state machine before writing any code. A blinking sequence is simple enough that a for loop handles it, but as soon as two different events can arrive, such as an interval elapsing and a button going down, the loop stops being enough and the diagram is what helps you keep the behavior straight.
For a button toggle, we need two states: UNPRESSED and PRESSED. We toggle the LED exactly once per press, when transitioning from UNPRESSED to PRESSED. Once in the PRESSED state we ignore further LOW readings until the pin goes HIGH again, which is what stops a finger held down for half a second from toggling the LED tens of thousands of times or more. Note that this does not cure bounce: while the contacts are chattering, if the pin really does go back above \(V_{\mathrm{IH}}\) and LOW again then that would register as a genuine UNPRESSED-to-PRESSED transition that would toggle the LED once more. The capacitor (or a delay in software) prevents this from happening.
Before coming to class, draw the state diagram for the button-toggle FSM described above on paper, the same way the running light is drawn in Figure 2.6.1. Label each state, each transition arrow with its triggering event, and the action taken on it.

Check Your Understanding Check Your Understanding

1.

In a button-toggle FSM with states UNPRESSED and PRESSED, the program is in state PRESSED and the button is still held down (pin LOW). What should happen on the next pass through the main loop?
  • Nothing: stay in PRESSED and do not toggle the LED.
  • Correct. The PRESSED state only acts when the button is released, so holding the button causes no additional toggles.
  • The LED toggles again.
  • That would happen with a flag-based approach that toggles on every LOW reading. The state machine prevents repeated toggles by staying in PRESSED until the button is released.
  • The state transitions back to UNPRESSED.
  • PRESSED → UNPRESSED only when the pin goes HIGH (button released).
  • The program gets stuck in an infinite loop.
  • There is no blocking loop inside the state machine; it returns to the top of while(1) each cycle.