Students use if … else inside forever so the robot turns away from close objects and keeps exploring, then add a random turn to make a robot vacuum.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 8 min · Investigate 8 min · Modify 12 min · Make 12 min · Real robot 5 min (optional)
This lesson builds on Stop before you crash: the same distance question, but the answer is now a choice of two moves. if the distance is less than 15, turn away; else, drive forward. Inside forever, the robot checks and chooses again every time round, so it explores without a plan, which is how simple robot vacuums cover a floor.
In this simulator the distance sensor only sees boxes and other robots, not the arena's walls, so robots may bump a wall and slide along it. Say so up front; it is a good discussion of how a simulation differs from the real thing. A real sensor sees walls, but weakly at an angle.
pick random 0 to 1 (in Math, randint in code) gives 0 or 1. A second if … else inside the first picks the turn direction. Schools with MOVE Motor robots can use Kitronik's measure distance with the same logic on the real robot.
if … else chooses a move every time round, with no plan. Ask them to read the question aloud and say when it is true.
| What you will see | A question to ask | The fix |
|---|---|---|
| The robot spins in the open and hits boxes. | Read the question aloud: when is it true? | Use < (less than): turn when the distance is small. |
| The robot gets stuck turning back and forth. | Does it turn far enough to see past the box? | Make the turn longer (700 ms), or back up first. |
| It always turns the same way. | Where does the random choice go? | Inside the first if: if pick random 0 to 1 = 0, turn right, else turn left. |
One possible solution for each task. Other correct programs are fine.

robot.elecfreaksCuteBot.start()
basic.forever(function () {
if (robot.obstacleDistance() < 15) {
robot.motorTank(-50, -50, 300)
robot.motorTank(50, -50, 500)
} else {
robot.motorTank(50, 50)
}
})

robot.elecfreaksCuteBot.start()
basic.forever(function () {
if (robot.obstacleDistance() < 15) {
robot.setColor(0xff0000)
robot.motorTank(-50, -50, 300)
robot.motorTank(50, -50, 500)
} else {
robot.setColor(0x00ff00)
robot.motorTank(50, 50)
}
})

robot.elecfreaksCuteBot.start()
basic.forever(function () {
if (robot.obstacleDistance() < 15) {
robot.motorTank(-50, -50, 300)
if (randint(0, 1) == 0) {
robot.motorTank(50, -50, 500)
} else {
robot.motorTank(-50, 50, 500)
}
} else {
robot.motorTank(50, 50)
}
})
Stretch: Count how many times the robot has turned away (a variable, change by 1) and show it on the micro:bit.
Support: Give the random if … else with its question already set; students put a turn in each branch.
How does if … else inside forever let a robot explore a room without a plan?
Every time round, it checks the distance and chooses: turn away if something is close, else drive forward. Many small choices add up to exploring.
Next: follow a line instead of exploring at random. Real-world link: early robot vacuums worked like this; newer ones map the room.