Week 4 · Timing & Evidence

Session 17: State machines

Teach & build120 minLesson 11: Non-Blocking Timing & 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.

  1. Lesson 11: State machines: one variable, many steps
  2. Theory Reference: State machines
  3. Lesson 11: Debounce without delay

Session plan

  1. 0:0010 min
    Recap

    Quiz: millis()

  2. 0:1020 min
    Demo

    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.

  3. 0:3030 min
    Build

    Traffic light

    Students build example 2 from the lesson and add a Serial report of each state change.

  4. 1:0040 min
  5. 1:4020 min
    Plan

    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.