Learning Rooms/△ Level 2 · Builder/Computing and algorithms
Finite State Machines -- Game Programming Concepts Series
Gamefromscratch · Length: see YouTube · captions on YouTube · nothing loads until you press play
By the end you can…
- Draw and trace a state diagram for a timed sequence.
- Calculate a cycle time and work out which state is showing at a given moment.
- Explain why safety states such as all-red exist and check a cycle against safety rules.
Watch it all: no segment has been checked by hand for this video yet.
1.Predict before you press play
A light shows red 30 s, red-and-amber 2 s, green 25 s, amber 3 s, then repeats. How long is one full cycle?
Pick one before you press play. A wrong guess is fine: it is what makes the video stick.
2.See the idea first
Play with this diagram for a minute (borrowed from the lesson “Doing several things at once (without waiting)”, on the same idea). It gives you something to hang the video on.
Loading the interactive…
Can't use the diagram? Read a description instead
The timing scrubber compares two loops over 2,000 ms. The blocking loop (toggle LED, read sensor, read button, delay 500 ms) runs every job only every 500 ms and misses two short button presses at 230–300 ms and 1,240–1,290 ms. The non-blocking loop runs the LED every 500 ms, the sensor every 100 ms and the button every 20 ms, so by 1,000 ms it has read the sensor 11 times and notices both presses.
3.Watch, actively
A programmer builds a traffic-light state machine in code, showing states, transitions and timers as a game developer uses them.
Length: see YouTube · captions on YouTube · source
Nothing loads from YouTube until you press play.
Watch for…
- → Watch how the programmer stores the current state and the time spent in it, and moves to the next state when the time is up.
Pause whenever you like. Rewatching a hard minute is a good habit, not a failure.
Can't stream it? Read the idea instead
A finite state machine is always in exactly one of a few named states, such as red, green or amber. Events or timers move it from one state to another along allowed transitions. Writing behaviour this way makes impossible combinations, like green both ways, impossible to reach.
What the video covers (our own note, written when we verified it): A programmer builds a traffic-light state machine in code, showing states, transitions and timers as a game developer uses them.
Written by Tactiverge in our own words, not a transcript. The full lesson below explains the idea without the video.
Read it instead
- Lesson: Timed sequences: the traffic light · The full lesson explains the idea in text, worked examples and diagrams, with no video needed.
- Engineering Atlas: Super-loops and finite state machines · A gripper's firmware can be written as states such as open, closing, holding and releasing, which makes every transition easy to test.
- Field Guide: D flip-flop · A short reference entry on the same idea.
Jot down one thing that surprised you and one question you still have.
4.Practise what you just saw
These come from the lesson that use this video and are checked on the server. Wrong answers get a hint and another try.
Practice 1 · a fresh version every time
A traffic light cycles: state 0 red for 20 s, state 1 red-and-amber for 2 s, state 2 green for 25 s, state 3 amber for 3 s, then back to red. It enters red at t = 0 s. Which state number is showing at t = 20 s? (A state that starts at exactly t is already showing.)
Practice 2
A junction cycle lasts 90 s. How many complete cycles happen in one hour?
Practice 3
Why do many junctions show all-red for 2 s between one road's amber and the other road's green?
Practice 4
A controller counts loop passes to time its states ('1,000 passes = 1 s'). Someone adds more code to the loop. What goes wrong?
5.Try it yourself
6.Where engineers use it
Real junction controllers add sensors (cars waiting, pedestrian buttons) and emergency pre-emption on top of the timed cycle, but the timed state machine and its safety interlocks remain at the core.
From the applied view in the lesson Timed sequences: the traffic light. Checked by automated tests only.
7.Build something with it
Follow-up project
Write the full state list for a two-road junction with a pedestrian phase (use the Traffic System Level 2 answer as a check). Calculate the cycle time and the fraction of the cycle each road gets green.
Where this video lives
- Timed sequences: the traffic light · Level 2, week 23
In the course catalogue: ROB-210 Programming for robots
The video is by Gamefromscratch, embedded from YouTube with privacy-enhanced mode and never autoplayed. The room around it (prompts, notes, practice, projects) is Tactiverge's, written with AI assistance and checked by automated tests only.