Week 4 · Timing & Evidence
Session 17: State machines
Learning outcomes
- Describe a behaviour as a state table
- Implement a timed state machine with a state variable and stateStart
- Explain why every state change must record stateStart
- Plan a state machine with a button event
Kit and components
- Uno, red, amber and green LEDs with 330 Ω resistors, push button, breadboard
Pre-reading about 20 min
Students read these on their phones before class. They are listed on the student Before class page.
Session plan
- 0:0010 minRecap
Quiz: millis()
- 0:1020 minDemo
From table to code
Draw the traffic light state table and turn each row into one branch. Remove stateStart = millis() and show the lights racing through the states.
- 0:3030 minBuild
Traffic light
Students build example 2 from the lesson and add a Serial report of each state change.
- 1:0040 minCode Along
State machines
- 1:4020 minPlan
Design a pedestrian crossing
In pairs, students write a state table for the lesson challenge's pedestrian button - the planning style Task A needs.
Checkpoint: ready to move on?
Students have a working traffic light driven by a state variable, and a written state table for the pedestrian version.
Extension for fast finishers
Code the pedestrian crossing from the state table.
Catch-up path
Code the two-state flasher first, then add the third state.
Homework and practice
Code Along 8.6.
Facilitator notes
- Students who can write a state table before coding will find Task A much easier.