Turn away when something is close, and keep exploring, choosing between two moves with if and else.
A robot vacuum never plans a route round your living room. It drives until it senses something, turns, and drives again, and in time it covers the whole floor.
Last lesson the robot stopped before a box. A robot that only stops is not much use for cleaning a floor. This time, when something is close, the robot turns away and keeps going: an if … else chooses between the two moves, again and again.
Make your prediction first. Help opens once you've locked it in.

robot.elecfreaksCuteBot.start()
basic.forever(function () {
if (robot.obstacleDistance() < 15) {
robot.motorTank(50, -50, 500)
} else {
robot.motorTank(50, 50)
}
})
Let the robot drive around for a while. Drag the boxes into its path. The arena has walls, but this simulator's sensor only sees boxes and other robots, so it may bump into a wall and slide along it. A real robot's sensor would see the wall too.

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

robot.elecfreaksCuteBot.start()
basic.forever(function () {
if (robot.obstacleDistance() < 15) {
robot.motorTank(50, -50, 500)
} else {
robot.motorTank(50, 50)
}
})
Watch the robot turn, then think about what forever does next.
Zara's program:

robot.elecfreaksCuteBot.start()
basic.forever(function () {
if (robot.obstacleDistance() > 15) {
robot.motorTank(50, -50, 500)
} else {
robot.motorTank(50, 50)
}
})
Real robot vacuums back up a little before they turn, so they do not scrape the furniture. Add that to the if.
Inside the if, before the turn: robot motor tank −50 −50 for 300 ms.
Show red lights while the robot is turning away and green while it drives, like a warning light on a forklift.
robot set color is in Robot. Click the colour to choose one.
One at the top of the if (red), one at the top of the else (green).
Back up a little before turning away.
This ticks by itself when your blocks do what is asked.
Red lights when it turns away, green lights when it drives.
This ticks by itself when your blocks do what is asked.
A robot that always turns the same way can get stuck going round the same corner. Make a robot vacuum that turns a random way:
pick random 0 to 1 (in Math) gives 0 or 1, like a coin toss.
Put a second if … else inside the first if, after the back-up: one branch turns right, the other turns left.
Its question: pick random 0 to 1 (in Math) = 0.
A robot vacuum: when something is close, back up, then turn left or right at random.
This ticks by itself when your blocks do what is asked.
On a real floor, try your vacuum in a box or a corner of the room built from books. Things to notice:
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. About 45 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
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.
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.
Next: follow a line instead of exploring at random. Real-world link: early robot vacuums worked like this; newer ones map the room.