TactivergeLite

Lite / Building Machines That Respond / Week 23

Timed sequences: the traffic light

A traffic light has no buttons in its basic form. What makes it change, and how do you prove it is never green both ways?

About 35 min · 3 goals · Checked by automated tests · Full interactive version

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.

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?

  1. 55 s
  2. 60 s
  3. 30 s
Guess first, then reveal

30 + 2 + 25 + 3 = **60 s**. Adding up the state durations gives the cycle time, which is the first number every traffic engineer works out.

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.

What the interactive shows (text version)

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.

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

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.

A programmer builds a traffic-light state machine in code, showing states, transitions and timers as a game developer uses them.

“Finite State Machines -- Game Programming Concepts Series” by Gamefromscratch (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

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

Intuitive

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.

Technical

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.

Mathematical

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.

Applied

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

state

state

cycles

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?

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.

Reflect

How would you convince a sceptical council officer that your timing plan can never show two greens at once?

Write your answer on paper or in your own notes. Lite never stores what you write.

← States, events and transitionsA state machine in twelve lines →