5 · Communication & data
State machine
Expert30 min
Goal
Structure a traffic light into explicit states (GREEN, AMBER, RED).
Why it matters
A state machine replaces unreadable delay() chains with clear logic.
Steps
- 01Declare enum State { GREEN, AMBER, RED };
- 02Handle transitions with millis() and a switch.
- 03Each state drives its own outputs.
- 04Add a pedestrian button that forces the transition.
This tutorial runs inside the Circuitly simulator: your wiring and your code are checked automatically at every step.
Next tutorials
- Control the board from the keyboard
Switch the LED on/off with the “on” and “off” commands.
- Log data as CSV
Produce “time;value” lines usable in a spreadsheet.
- Remember after power-off (EEPROM)
Keep a setting even after a restart.
- Three tasks in parallel
Blink, measure and display at three different rates.