Lite / Building Machines That Respond / Week 23
A state machine in twelve lines
Can a whole automatic door fit in one variable and a handful of ifs?
Why should I care?
Diagrams are for thinking; code is what runs. Turning a transition table into a short, readable function is a skill you will use in every embedded project, and it makes the behaviour easy to test with a list of events.
- Implement a state machine in Python-style code with one state variable and if/elif branches.
- Trace the code by hand through a sequence of events.
- Decide which outputs belong to each state.
Predict
In code, what is the simplest way to remember which state a door is in?
- A separate true/false variable for each state
- One variable holding the state number (0–3)
- Read the motor to guess
Guess first, then reveal
One variable means the machine can only ever be in one state. Four true/false flags could accidentally be 'opening' and 'closing' at once, a bug that a single state number makes impossible.
Discover
Run the door through the events in the worked example below while reading the code. After each button, say which `if` branch would have run.
What the interactive shows (text version)
The door runner in this lesson mirrors the code: each event button calls the same rules as the step function, highlights the matching row (one row per elif branch) and logs the change. Events with no matching row leave the state unchanged, like the final 'return state' line.
Understand
The pattern
CLOSED, OPENING, OPEN, CLOSING = 0, 1, 2, 3
state = CLOSED
def step(state, event):
if state == CLOSED and event == 'person':
return OPENING
elif state == OPENING and event == 'fully_open':
return OPEN
elif state == OPEN and event == 'timer':
return CLOSING
elif state == CLOSING and event == 'person':
return OPENING
elif state == CLOSING and event == 'fully_closed':
return CLOSED
return state # anything else: no changeOne line per row of the transition table, plus a default.
Outputs follow the state
In the main loop, after updating the state, set the outputs from it: motor forwards while OPENING, backwards while CLOSING, off otherwise; start the hold-open timer when entering OPEN. Keeping outputs tied to states means you can never forget to switch the motor off.
Test with event lists
Because step is a pure function, you can test it without hardware:
s = CLOSED
for e in ['person', 'fully_open', 'timer', 'person']:
s = step(s, e)
assert s == OPENINGWrite one test for every row, and a few for events that should be ignored.
Explain it another way
Intuitive
The state variable is a bookmark: it says which page of the instructions you are on, so you always know which rules apply.
Technical
Alternatives include a dictionary keyed by (state, event) or a match statement. Enum types give states readable names. Keep transitions and outputs separate so each can be tested.
Applied
Firmware for lifts and medical devices is often generated from, or checked against, a state table, and test suites replay recorded event sequences against the step function.
Worked example 1
Trace step from CLOSED with events: timer, person, fully_open, person, timer, fully_closed.
timer: no rule → CLOSED. person → OPENING. fully_open → OPEN. person: no rule for OPEN → OPEN. timer → CLOSING. fully_closed → CLOSED.
Common mix-ups
State machines need a special library.
✓ One variable and some if/elif branches are enough for most small machines.If it works for the normal sequence, it is tested.
✓ Test the ignored events and the unusual orders too: that is where the safety bugs live.
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
Write step(state, event) for the pedestrian crossing you designed last lesson, then write assert tests covering every row of your table and three events that should be ignored. Trace one test by hand.
Reflect
What is one advantage of writing the transition table before writing any code?
Write your answer on paper or in your own notes. Lite never stores what you write.