1.Why should I care?
Doors, lifts, vending machines, washing machines and game characters all remember what they are doing and react differently to the same input depending on it. A state machine is the engineer's tool for making that behaviour clear, complete and testable.
- ◇ Describe a machine's behaviour as states, events and transitions.
- ◇ Trace a state machine through a sequence of events using a transition table.
- ◇ Spot missing transitions and stuck states, especially unsafe ones.
2.Discover
Choose the automatic door. Walk it through a whole cycle: person detected, fully open, timer ended, fully closed. Then try to trap someone: while it is closing, fire 'person detected'. Which row of the table lit up?
Loading the interactive…
Can't use the interactive? Read a description instead
The door state machine has states 0 Closed, 1 Opening, 2 Open, 3 Closing and events 0 Person detected, 1 Fully open, 2 Timer ended, 3 Fully closed. Rules: Closed + person → Opening; Opening + fully open → Open; Open + timer ended → Closing; Closing + fully closed → Closed; Closing + person → Opening. Any other event leaves the state unchanged. The runner lists the current state, highlights the rule that matched and keeps a log of transitions.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
An automatic door has states 0 = closed, 1 = opening, 2 = open, 3 = closing. Its events are 0 = person detected, 1 = fully open, 2 = timer ended, 3 = fully closed. Rules: closed + person → opening; opening + fully open → open; open + timer ended → closing; closing + fully closed → closed; closing + person → opening. Any other event leaves the state unchanged. The door is in state 2 and event 0 happens. What state number comes next?
4.Predict
A door is open and someone presses 'person detected'. With rules 'closed + person → opening' and 'closing + person → opening', what happens?
Guess first. Being wrong here is how the surprise works.
5.Understand
Three ingredients
A state is what the machine is doing now (closed, opening, open, closing). An event is something that happens (a person is detected, a limit switch reports 'fully open', a timer ends). A transition is a rule: in state S, on event E, go to state S′. The machine is in exactly one state at a time.
Tables beat tangled ifs
Write every rule as a row: from, event, to. The door needs only five rows. Anything not in the table is ignored: the state stays the same. A table lets you check every state–event pair systematically: four states × four events = 16 pairs to think about.
Complete and safe
A good state machine has no stuck states (every state has a way out) and handles every safety event in every state where it matters. 'Closing + person → opening' is the rule that stops the door shutting on someone. Engineers number states (0, 1, 2, 3) so firmware can store the current state in one variable.
Explain it another way
A board game: your counter is on one square (the state). The dice roll (the event) and the rules decide which square you move to. Some rolls on some squares do nothing.
This is a finite state machine (FSM). In code it is often state = table.get((state, event), state). Moore-style machines set outputs from the state (motor on while 'opening'); Mealy-style ones set outputs on transitions.
Lift controllers, traffic lights, ticket barriers and phone charging circuits are all specified as state machines, and safety reviewers read their tables row by row.
Worked example 1
Start: closed (0). Events: person, fully open, person, timer ended, person, fully open. Final state?
0 + person → 1 opening. + fully open → 2 open. + person → stays 2. + timer ended → 3 closing. + person → 1 opening (safety). + fully open → 2 open.
Common mix-ups
- The machine reacts the same way to an event every time.
✓ The reaction depends on the current state. That memory is what makes it a state machine. - If a case is not in the table, the program crashes.
✓ In a well-designed machine, unlisted events are deliberately ignored, and the designer has checked that ignoring them is safe.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
An automatic door has states 0 = closed, 1 = opening, 2 = open, 3 = closing. Its events are 0 = person detected, 1 = fully open, 2 = timer ended, 3 = fully closed. Rules: closed + person → opening; opening + fully open → open; open + timer ended → closing; closing + fully closed → closed; closing + person → opening. Any other event leaves the state unchanged. The door is in state 0 and event 3 happens. What state number comes next?
Practice 3
Using the door rules (0 closed, 1 opening, 2 open, 3 closing), start in state 0 and apply the events: person detected, fully open, timer ended, fully closed, timer ended. Which state number do you end in?
Practice 4
Someone deletes the row 'closing + person → opening'. What is the danger?
Practice 5
A vending machine has states idle, paid, vending, empty. No rule ever leaves 'empty'. What kind of problem is that?
7.Apply it
Design a state machine for a pedestrian crossing button: states, events (button pressed, timer ended) and a transition table. Check all state–event pairs and mark any you deliberately ignore, with a reason.
8.Reflect
Concepts: Super-loops and finite state machines