Machines that remember: states and sequences
A traffic light is green. What colour comes next? The light itself has to remember where it is in the cycle. How?
About 30 min · 3 goals · Checked by automated tests; not yet reviewed by an educator
1.Why should I care?
Many machines can't decide from the sensors alone: a washing machine must know whether it's washing or spinning; a traffic light must know whether it was just green. A state machine is the simple, powerful idea engineers use to give machines that memory.
- ◇ Explain what a state is and why some machines need to remember one.
- ◇ Describe a machine as a list of states and the rules for moving between them.
- ◇ Spot unsafe states and transitions in a simple controller.
2.Predict
A traffic light shows amber. In the UK it could be heading towards red or towards green. What does the controller need to decide correctly?
Guess first. Being wrong here is how the surprise works.
3.Discover
In the Traffic System Build, Level 1, judge each moment at the crossroads as safe or a conflict. Then say what each light must show next. Which moments are unsafe, and what rule would stop them?
Can't use the interactive? Read a description instead
The Traffic System Build's Level 1 shows six moments at a crossroads with traffic lights and a pedestrian crossing. Moments where two conflicting movements both have a go signal (for example, north–south amber while east–west is green, or walk while north–south is green) are conflicts. You also choose what the lights should show next at three points in the cycle.
4.Understand
A state is 'where I am'
A state is the machine's current situation: green, amber, red; or filling, washing, spinning. The machine is always in exactly one state, and it remembers it until something makes it move on.
Transitions: the rules for moving on
A transition is a rule like: 'in GREEN, after 30 seconds, go to AMBER'. Transitions can be triggered by time, by a sensor ('button pressed') or both. Draw states as circles and transitions as labelled arrows, and you have a state diagram.
Designing out danger
Some combinations must never happen: north–south green while east–west is green is a crash. Engineers list these unsafe states and design the transitions so they can't be reached, adding an all-red pause between greens for safety. A state machine is easy to check because it has a short, complete list of states.
Explain it another way
A board game piece on a track is a state machine: where you are is your state; the dice and the squares' rules decide your transition. You need to remember your square, or you can't take your next turn.
A finite state machine has a finite set of states, an initial state, inputs and a transition function: next state = f(current state, input). Storing the current state needs memory: with 8 states, 3 bits.
Lifts, vending machines, washing machines, game characters and robot behaviours (search → approach → grab → return) are all built as state machines. Safety engineers audit every transition.
Worked example 1
A simple pedestrian crossing has states: CARS GO, CARS STOP (amber), WALK, FLASHING. Write the transitions.
CARS GO → (button pressed and 20 s passed) → CARS STOP → (3 s) → WALK → (7 s) → FLASHING → (10 s) → CARS GO. The button only matters in CARS GO; pressing it during WALK changes nothing.
Common mix-ups
- A machine can always decide what to do from its sensors alone.
✓ Many machines also need to remember their state (where they are in a sequence) to choose the right next step.
5.Practice
Practice, not a test. Wrong answers get a hint and another try.
Practice 1
Which of these is a STATE of a washing machine?
Practice 2
A robot has 6 behaviour states. What is the fewest number of bits needed to remember which state it is in?
Practice 3
At a crossroads, which state must never be reachable?
Practice 4
A light cycle is: green 30 s, amber 3 s, all red 2 s, other road green 20 s, amber 3 s, all red 2 s, then repeat. How long is one full cycle, in seconds?
6.Apply it
Draw a state diagram for a microwave: IDLE, COOKING, PAUSED (door opened), DONE. Label every transition with what triggers it. Which transition is a safety rule?
7.Reflect
Concepts: Super-loops and finite state machines · Decisions and loops · Robot safety and ethics