TactivergeLite

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?

About 30 min · 3 goals · Checked by automated tests · Full interactive version

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.

Predict

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

  1. A separate true/false variable for each state
  2. One variable holding the state number (0–3)
  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.

You can also open the interactive on the full site when you have a faster connection.

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

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

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

state

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

pairs

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

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.

← Timed sequences: the traffic lightDC motors, servos and steppers →