State Machines: the short answer
A finite state machine (FSM) is a system that is always in exactly one of a fixed set of states, and moves between them when events happen. A traffic light is the classic example: Green, Yellow, Red, with timers as the events.
- Level
- Advanced
- Time
- About 6 min
- Topic
- Digital logic
Design a finite state machine on paper and write it as clean, non-blocking firmware.
Before this: Flip-Flops, Debouncing and Counters, Digital Inputs and Outputs
A finite state machine (FSM) is a system that is always in exactly one of a fixed set of states, and moves between them when events happen. A traffic light is the classic example: Green, Yellow, Red, with timers as the events. In hardware, an FSM is built from flip-flops that hold the state and logic that decides the next state. In firmware, the same idea gives programs that are easy to read, test and extend.
Start on paper. Draw each state as a circle, and each transition as an arrow labelled with the event that causes it and any action it takes. In a Moore machine the outputs depend only on the state (Red means red LED on). In a Mealy machine outputs can also depend on the input at that moment. Most firmware is closer to Moore, which is simpler to reason about.
In code, use an enum for the states and a switch statement inside loop(). Each case checks its events and changes state when one happens, recording the time with millis(). Never use delay() inside an FSM: with non-blocking timing the program checks buttons, sensors and timers on every pass, so the device always responds. When a new feature arrives, you add a state or a transition instead of untangling nested if statements.
Name the states and the events
A state machine lists the situations a device can be in, and what makes it move between them. Each state has a name such as IDLE, FILLING or ERROR, and each transition has an event that causes it, such as a button press or a timeout. Drawing the diagram first catches missing cases, like what happens when the stop button is pressed during a fill.
The code is then a switch on the current state, in which each case checks the events that matter in that state and sets the next one.
Avoid blocking waits
Replace delay calls with a timestamp of when the state started, and compare it with millis(). That way the loop keeps running and can still react to a button or a sensor while a timer runs. Every state should have a way out, either by an event or a timeout, so the device cannot get stuck.
Give the machine a safe state, such as everything off, and go there on any error or unexpected event.
Testing
Print each transition with its reason, and test every arrow in the diagram, including the ones that should never happen. Keep the outputs, such as switching a relay, in the state's entry code, so the same action is not repeated every loop.
- Using delay inside a state, so the device cannot react.
- Leaving a state without a timeout or exit.
- Not defining what happens on an unexpected event.
Practise this lesson with Pro
The explanation above is free for everyone. The practice for Advanced lessons in every domain are part of the Pro plan at ₹999 a month.
With Pro you also get: a worked example, a hands-on task, a 3-question check.
