Sense, think, act (the control loop)
What you’ll learn
- The input → process → output model
- Why a robot repeats this loop over and over
- How to describe any robot behaviour as a loop
Input, process, output
Computer scientists call sense–think–act by another name: input → process → output (IPO).
┌──────────┐ ┌───────────┐ ┌──────────┐ │ INPUT │ ──▶ │ PROCESS │ ──▶ │ OUTPUT │ │ (sense) │ │ (think) │ │ (act) │ └──────────┘ └───────────┘ └──────────┘ ▲ │ └────────── the world changes ◀─────┘The arrow coming back is the important part. When the robot acts, the world changes (it moves closer to the wall, for example). Next time it senses, it sees a new situation and may decide something different. This repeating cycle is called a control loop.
Open loop vs closed loop
- Open loop: the robot acts without checking the result. “Drive forward for 3 seconds.” If a wheel slips, it never knows.
- Closed loop: the robot checks a sensor and corrects itself. “Drive forward until the distance sensor reads 10 cm.”
Closed-loop control is what makes robots reliable. Most of this course builds towards it.
Example: a thermostat
| Step | What happens |
|---|---|
| Input | Temperature sensor reads 18 °C |
| Process | Rule: “if colder than 20 °C, heat on; otherwise heat off” |
| Output | Heater switches on |
| Loop | A few minutes later it reads 20.5 °C, so the heater switches off |
Example: a line-following robot
| Step | What happens |
|---|---|
| Input | Left line sensor sees black, right sees white |
| Process | Rule: “line is to the left, so turn left” |
| Output | Left motor slows down, right motor keeps going |
| Loop | Next reading: both sensors see white, so drive straight |
Example: the same loop in code
function loop() { // runs again and again const d = distance(); // INPUT: read the sensor if (d > 20) { // PROCESS: decide motors(50, 50); // OUTPUT: drive } else { motors(0, 0); // OUTPUT: stop }}A worked example: robot vacuum
| Step | What happens |
|---|---|
| Input | Bump sensor is pressed |
| Process | Rule: “if bumped, back up and turn” |
| Output | Wheels reverse for 0.5 s, then spin to turn |
| Loop | Sense again: bump sensor now not pressed → drive forward |
A real robot runs this loop hundreds of times per second. That’s why it looks like it reacts instantly.
Try it
The virtual robot below starts with open-loop code: it drives forward for 5 seconds without looking.
-
Press ▶ Run. Does it stop before the box, or crash into it?
-
Now make it closed loop: delete the code and type this instead, then Run again.
function loop() {if (distance() > 20) { // SENSE + THINKmotors(50, 50); // ACT: drive} else {motors(0, 0); // ACT: stop}} -
Watch the
distancereading under the arena while it drives. Which part of the code is the input, the process and the output?
You’ll write much more of this in the Programming and Control sections. You can always come back to the full Robot simulator.
Robot commands
motors(left, right): wheel power, −100 to 100lineLeft(),lineRight():trueover the black linedistance(): cm to the nearest thing ahead (max 200)distanceLeft(),distanceRight(): cm to the sideslinePosition(): where the line is, −1 (left) to 1 (right), ornullmillis(): milliseconds since Runprint(value): show a message belowsetup()runs once;loop()runs 50 times a second
Stop further awayEasy
Change the closed-loop code so the robot stops 40 cm from the box instead of 20 cm.
Show an answer
Change the number in the condition: if (distance() > 40).
Slow down before stoppingMedium
Make the robot drive fast when the box is far away and slowly when it’s closer than 50 cm, then stop at 20 cm.
Use if, else if and else with two different distances.
Show an answer
function loop() { if (distance() > 50) motors(70, 70); else if (distance() > 20) motors(25, 25); else motors(0, 0);}Never get stuckHard
Make the robot explore the whole room without ever stopping: when something is close, turn away and carry on.
Show an answer
function loop() { if (distance() < 25) motors(-40, 40); // spin left until the way is clear else motors(60, 60);}Each time round the loop the robot re-checks, so it only turns for as long as the obstacle is in front.
Quick check
Finished this lesson?