Trace an Arduino state machine one transition at a time
Give each phase a name and decide which transition follows each state.
0 of 3 complete · Progress saved on this device
Work out the example
Give each phase a name and decide which transition follows each state. In a three-state cycle, go follows stop, caution follows go, then stop follows caution. Hidden changes to output pins are easier to detect when the current state is printed.
Decide what to check next
Separate the decision to change state from the code that updates physical outputs. The example advances exactly once per scripted event. Connect it to a timer only after the ordered trace matches your design.
Compare with the serial result
It prints state numbers 1, 2, 0. The linked project provides a real LED circuit.
Open the exact Uno example
It prints state numbers 1, 2, 0. The linked project provides a real LED circuit.
void setup() {
Serial.begin(9600);
enum State { Stop, Go, Caution };
State state=Stop;
for (byte event=0; event<3; ++event) {
state=state==Stop ? Go : state==Go ? Caution : Stop;
Serial.println(static_cast<byte>(state));
}
}
void loop() {}Verified with [email protected] on Uno R3 (2026-12-05 UTC). This check did not use physical hardware.