Skip to content
Tactiverge

School/△ Builder/Week 23

A state machine in twelve lines

Can a whole automatic door fit in one variable and a handful of ifs?

About 30 min · 3 goals · Checked by automated tests

1.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.

2.Predict

In code, what is the simplest way to remember which state a door is in?

Guess first. Being wrong here is how the surprise works.

3.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.

Loading the interactive…

Can't use the interactive? Read a description instead

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.

4.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1

state is CLOSING and the event is 'person'. Which value does `step` return?

5.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 change

One 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 == OPENING

Write one test for every row, and a few for events that should be ignored.

Explain it another way

The state variable is a bookmark: it says which page of the instructions you are on, so you always know which rules apply.

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.

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?

state

Practice 3

What would happen if you deleted the final `return state` line?

Practice 4

The door has 4 states and 4 events. How many state–event pairs should a thorough test suite consider?

pairs

Practice 5

A learner uses `opening = True` and `closing = True` flags instead of one state variable. What new bug becomes possible?

7.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.

8.Reflect

Concepts: Super-loops and finite state machines · Decisions and loops