1.Why should I care?
Timed sequences run washing machines, crossings, production lines and theatre lighting. The traffic light is the classic example because getting it wrong is so obviously dangerous, which is exactly why engineers learn to check sequences rigorously.
- ◇ 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.
2.Discover
Do Level 2 of the Traffic System build: make the junction's controller as a cycle of timed states, run it, and pass the safety audit. Read every audit failure carefully: each one names a missing state or transition.
Can't use the interactive? Read a description instead
The Traffic System Level 2 editor lets you list timed states for a junction (for example north–south green, amber, all red, east–west green, amber, all red, walk, flashing don't walk, all red), run the cycle, and check it against a safety audit: no conflicting greens, amber after every green, all-red before every green and walk, and every road and the crossing served.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
A traffic light cycles: state 0 red for 25 s, state 1 red-and-amber for 2 s, state 2 green for 15 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 = 47 s? (A state that starts at exactly t is already showing.)
4.Apply it
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.
5.Understand
Time is an event too
In a timed sequence the main event is 'time in this state is up'. Each state has a duration. The controller remembers the current state and when it started; when the duration has passed, it moves to the next state. UK lights go red → red-and-amber → green → amber → red.
Where are we at time t?
Add the durations to get the cycle time C. To find the state at time t, first find how far into the current cycle you are (t minus whole cycles), then lay the states end to end. With red 30, red-amber 2, green 25, amber 3 (C = 60 s): t = 95 s is 35 s into a cycle, which falls in green (32–57 s).
Safety states
A junction has two directions. Between one road's amber and the other road's green, engineers add all-red for a couple of seconds so a car that just ran the amber can clear. A safety audit checks rules such as: never two conflicting greens, amber after every green, all-red before every green. These are checkable facts about the state list.
Explain it another way
A recipe with timings: boil for 10 minutes, then drain, then fry for 3 minutes. The kitchen timer is the event that moves you on.
Timed FSMs store state and an entry timestamp; on each loop pass, if now - entered >= duration[state]: state = next[state]; entered = now. Using elapsed time (not counting loop passes) keeps timing correct even if the loop speed changes.
C = Σ durations. Position in cycle: x = t mod C. State k is showing when start_k ≤ x < start_k + duration_k, where start_k is the sum of the earlier durations.
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.
Worked example 1
Red 20 s, red-amber 2 s, green 15 s, amber 3 s. Which state is showing at t = 77 s?
C = 40 s. 77 − 40 = 37 s into the cycle. Red 0–20, red-amber 20–22, green 22–37, amber 37–40. 37 s is the first moment of amber: state 3.
Common mix-ups
- If each state looks right on its own, the sequence is safe.
✓ Safety lives in the transitions: what follows what, and what never happens at the same time. Audit the whole cycle, including the wrap-around. - A traffic light only needs three states.
✓ UK lights use red-and-amber as a fourth state, and real junctions add all-red clearance states.
6.Watch
→ 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.
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.
The video is optional: everything you need is also in the lesson text and the interactive.
7.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · 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 3
A junction cycle lasts 90 s. How many complete cycles happen in one hour?
Practice 4
Why do many junctions show all-red for 2 s between one road's amber and the other road's green?
Practice 5
A controller counts loop passes to time its states ('1,000 passes = 1 s'). Someone adds more code to the loop. What goes wrong?
8.Predict
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?
Guess first. Being wrong here is how the surprise works.
9.Reflect
Concepts: Super-loops and finite state machines · Real-time computing on robots