State Machines in Embedded Software: Designing Predictable Behaviour
Embedded devices are full of modes: idle, measuring, charging, updating, faulted. When those modes are tracked with a collection of flags and checked in scattered if statements, nobody can say with confidence what the device does when an unexpected event arrives. A state machine replaces that with a defined set of states, the events each one handles and the transitions between them. This article shows how to design one that stays predictable.
At a glance
- 2^n
- The number of combinations n independent flags can form, most of them never intended.
- One state
- A state machine is always in exactly one state, so the current mode is never ambiguous.
- Events
- A button press, a timer expiry or a received message: inputs that drive every transition.
- Table
- A transition table lists every state and event pair, making gaps easy to spot in review.
The problem
Flags multiply silently. Three booleans can form eight combinations and eight booleans can form 256, yet the code was written with only a handful in mind. The rest are reachable through timing coincidences, and they are where intermittent field bugs come from.
Scattered logic is also hard to review. To know what a button press does, an engineer has to trace every place the flags are read and written, often across interrupts and tasks.
If you cannot draw the states and transitions on one page, the behaviour is not yet designed.
Designing the machine
Start with a diagram: each state, the events it responds to and where each event leads, with the action performed on the transition or on entering a state. Then decide explicitly what every state does with every event, including the ones it should ignore. Ignoring an event is a decision too.
Process one event at a time to completion before taking the next from a queue, so the machine never sees an event halfway through a transition. Where several states share behaviour, such as a common fault response, a hierarchical state machine lets them inherit it instead of repeating it.
What good looks like
List the modes the product has and the events that can occur, then draw the states and transitions before writing code.
Implement with a switch per state or a transition table, feeding events through a queue so each is handled to completion.
Test every state and event pair, including unexpected events, and log each transition so field behaviour can be reconstructed.
| combinations | |
|---|---|
| 2 flags | 4 |
| 4 flags | 16 |
| 6 flags | 64 |
| 8 flags | 256 |
| 10 flags | 1024 |
| State | Event | Next state |
|---|---|---|
| Idle | Charger connected | Precharge |
| Precharge | Cell above precharge threshold | Constant current |
| Constant current | Cell reaches 4.2 V | Constant voltage |
| Constant voltage | Current below termination level | Done |
| Any state | Over-temperature or timeout | Fault |
What it changes
Predictability
The response to every event in every mode is defined in advance.
Reviewability
The diagram and the code correspond, so reviewers can check one against the other.
Testability
Each transition is a small, separate test case.
Field Diagnosis
A log of transitions shows exactly what the device was doing before a fault.
Where to go next
Keep timing out of the states themselves. Start a timer on entry and treat its expiry as an ordinary event.
For larger designs, consider an established framework or code generator for hierarchical state machines rather than building one from scratch.
Problems this prevents
- Flag combinations nobody designed for
- Events silently ignored in some modes
- Behaviour that differs from the documented diagram
- Field faults that cannot be reconstructed
Discuss your project
Email support@aavico.com or use Get in Touch when the website finishes loading.