The forever loop inside every robot
Infinite loops are a bug, right? So why does every traffic light controller run one on purpose?
About 25 min · 3 goals · Checked by automated tests; not yet reviewed by an educator
1.Why should I care?
A robot that stops checking its sensors is a robot that crashes. Every controller, from a thermostat to a self-driving car, is a loop that never ends: sense, think, act, again. Seeing that loop is the key to understanding how intelligent machines stay intelligent.
- ◇ Explain why control programs run in a forever loop.
- ◇ Combine a loop with sense–think–act and a condition.
- ◇ Describe a machine's cycle of states that repeats.
2.Predict
A traffic light runs green → amber → red → green… Where does the program end?
Guess first. Being wrong here is how the surprise works.
3.Discover
Judge six moments at the crossroads as safe or a conflict, then say what the lights must show next. Notice how the cycle wraps round from the last state back to the first, for ever.
Can't use the interactive? Read a description instead
The traffic activity can be replaced by this description: at a crossroads, north–south and east–west roads take turns. A safe cycle is: north–south green, north–south amber, all red, east–west green, east–west amber, all red, walk, flashing don't-walk, all red, then back to the start. It is a conflict if one road shows green or amber while the other shows green, or if walk shows while any road has green.
4.Understand
Forever on purpose
A control program looks like this:
forever: sense → think → act
It never finishes because the world never stops changing. A forever loop is only a bug when you wanted the loop to stop.
Conditions inside loops
Inside the forever loop sit IF rules: forever: read temperature; IF too cold THEN heater on ELSE heater off. The loop makes sure the rule is checked again and again; the rule decides what to do each time.
Cycles of states
Some machines step through a fixed list of states and wrap round: green, amber, all red, other road green, and so on. The traffic light's forever loop moves from state to state, with rules saying when each change is allowed and what must never be on at the same time.
Explain it another way
Your heart is a forever loop: beat, rest, beat, rest, for your whole life. It also adjusts as it goes: faster when you run, slower when you sleep.
Embedded controllers usually have a 'main loop' that runs until power is removed. Each pass reads inputs, updates the current state and writes outputs. A state machine inside it decides which state comes next.
A robot vacuum's main loop runs many times a second: read bump, cliff and wheel sensors; update its map; choose a direction; set wheel speeds. It only stops when you switch it off or it docks to charge.
Worked example 1
Write the forever loop for a night light.
forever: read the light sensor; IF light level < 30 THEN lamp on ELSE lamp off. Each pass takes a fraction of a second, so when the sun sets the lamp turns on almost immediately, and it turns off again at dawn.
Common mix-ups
- All infinite loops are bugs.
✓ Controllers run forever loops on purpose. It's a bug only when a loop that should stop doesn't. - A robot checks its rules once when switched on.
✓ It checks them on every pass of its main loop, many times a second.
5.Practice
Practice, not a test. Wrong answers get a hint and another try.
Practice 1
When is a loop that never ends the right design?
Practice 2
Where should the IF rule 'IF too hot THEN fan on' go in a thermostat program?
Practice 3
A traffic light's cycle is green 30 s, amber 3 s, red 37 s, then repeats. How many full cycles does it complete in 7 minutes (420 s)?
Practice 4
A robot's main loop runs every 20 milliseconds (0.02 s). How many passes does it make in one second?
6.Apply it
Write a forever loop for a pedestrian crossing: the light stays green for cars until the button is pressed, then goes amber, then red with the walk sign, then back to green. Which rule would you add so that nobody can ever see walk while cars have green?
7.Reflect
Concepts: Decisions and loops · The sense–think–act loop · Super-loops and finite state machines