Traffic Lights: Understanding State Machines

Green, Yellow, Red. Repeat. Traffic lights never get confused. They are Finite State Machines. How we use this logic to automate robot arms.

Traffic Lights: Understanding State Machines

A Traffic Light is simple.

  • Green -> Yellow.
  • Yellow -> Red.
  • Red -> Green. It never goes Green -> Red. It never goes Red -> Yellow. It follows a strict set of rules called a Finite State Machine (FSM).

The Spaghetti Code Problem

New programmers write code like a checklist: if (button pressed) { lift arm; open claw; spin motor; } This works for simple things. But what if you press the “Score” button while the robot is already “Intaking”?

  • The arm tries to go up.
  • The claw is still trying to grab.
  • The robot rips itself apart.

The State Machine Solution

We define States. The robot can only be in one State at a time.

States: INTAKE, TRANSFER, LIFT, SCORE, RESET.

Transitions:

  • In INTAKE:
    • Button A pressed? -> Go to TRANSFER.
    • Button B pressed? -> Ignore (Can’t score while intaking).
  • In TRANSFER:
    • Are we done? -> Go to LIFT.
  • In LIFT:
    • Arm extended? -> Go to SCORE.

Why It Matters

This makes the robot Idiot-Proof. Even if the driver panics and mashes every button on the controller, the code ignores invalid inputs. “I can’t Score because I am currently Resetting. Request Denied.”

Traffic lights prevent cars from crashing by enforcing order. State machines prevent robots from crashing by enforcing logic. If you want to write “Professional” code, stop writing if-else chains and start drawing state diagrams (circles and arrows).

Explore FIRST® Robotics

FIRST® (For Inspiration and Recognition of Science and Technology) is a global robotics community preparing young people for the future. Discover the ultimate sport for the mind and see how you can get involved in STEM and robotics!

Learn More at firstinspires.org