Lite / Building Machines That Respond / Week 23
States, events and transitions
An automatic door is closing when someone walks up. Should it keep closing? How would a machine even know what it is doing?
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.
Predict
A door is open and someone presses 'person detected'. With rules 'closed + person → opening' and 'closing + person → opening', what happens?
- It starts opening
- Nothing: no rule covers 'open + person', so it stays open
- It closes
Guess first, then reveal
The same event means different things in different states. In 'open', a person arriving changes nothing, so the door simply stays open. A transition table makes these choices explicit instead of leaving them to luck.
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?
What the interactive shows (text version)
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.
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
Intuitive
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.
Technical
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.
Applied
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.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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.
Reflect
Which state–event pair in the door did you nearly forget? What would have caught it?
Write your answer on paper or in your own notes. Lite never stores what you write.