All tutorials

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

  1. 01Declare enum State { GREEN, AMBER, RED };
  2. 02Handle transitions with millis() and a switch.
  3. 03Each state drives its own outputs.
  4. 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