Read the robot's distance sensor again and again, and stop before it hits something.
A car's parking sensor beeps faster as you back towards a wall, and a robot vacuum turns before it bumps the couch. Both measure how far away things are, many times a second.
This robot has a distance sensor on its front. It sends out a sound too high for people to hear and times the echo, so it can tell how many centimetres away the nearest object is (up to 40). In blocks, robot obstacle distance (cm) gives that number. You find it in Robot.
In the simulator, the robot drives around a small arena with a looping track and some boxes. You can drag the robot and drag the boxes with the mouse.
Make your prediction first. Help opens once you've locked it in.

robot.elecfreaksCuteBot.start()
robot.motorTank(50, 50)
basic.forever(function () {
if (robot.obstacleDistance() < 15) {
robot.motorStop()
}
})
The first block picks the robot (an Elecfreaks Cutebot). robot motor tank sets the left and right wheel speeds: 50 and 50 means straight ahead at half speed.
The robot simulator appears beside the micro:bit. Drag a box into the robot's path (or drag the robot so it faces a box), then restart the simulator. Watch the blue wave: that is the sensor's beam.

robot.elecfreaksCuteBot.start()
robot.motorTank(50, 50)
basic.forever(function () {
if (robot.obstacleDistance() < 15) {
robot.motorStop()
}
})
This version also shows an X when it stops, so you can see the moment the question became true.

robot.elecfreaksCuteBot.start()
robot.motorTank(50, 50)
basic.forever(function () {
if (robot.obstacleDistance() < 15) {
robot.motorStop()
basic.showIcon(IconNames.No)
}
})
Drag every box away from the robot and think about what the sensor sees.
Look at how far the robot travels after the X appears.
Mia's program:

robot.elecfreaksCuteBot.start()
robot.motorTank(50, 50)
if (robot.obstacleDistance() < 15) {
robot.motorStop()
}
Make the robot stop when the box is closer than 25 cm. Drag a box in front and test it.
Change 15 to 25 in the if.
Show an X on the micro:bit when the robot stops.
show icon is in Basic.
Put it inside the if, under robot motor stop. Choose the X.
Make the robot stop farther from the box: 25 cm.
This ticks by itself when your blocks do what is asked.
Show an X when the robot stops.
This ticks by itself when your blocks do what is asked.
Make a parking robot, like a car easing into a parking space:
Test it by dragging a box towards the robot, and away again.
Inside forever: if the distance is less than 10, stop. Else if it is less than 30, drive slowly. Else, drive at 50.
Click the + on the if block to add else if and else. Slow is robot motor tank 20 20.
A parking robot: full speed when the way is clear, slow when something is near, and stop when it is close.
This ticks by itself when your blocks do what is asked.
The same program runs on a real Cutebot: download it and put the robot on the floor facing a wall or a box. Things to notice and tune:
Students read the robot's distance sensor inside forever and stop before an obstacle, then build a parking robot that slows down as things get closer. About 45 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
The robot's distance sensor (ultrasonic) times an echo and reports how far away the nearest object is, in centimetres, up to 40. With nothing in range it reports 40. In blocks this is robot obstacle distance (cm), in the Robot drawer. The idea is the same as the micro:bit sensors lessons: a sensor is a number the world keeps changing, so the program must read it again in forever to follow it. Here the stakes are visible: read it once and the robot crashes.
Every program starts with a block that picks the robot (Elecfreaks Cutebot here). Schools with another supported robot (DFRobot Maqueen, Yahboom Tiny:bit and others in the robot extension) change only that block; everything else is the same. robot motor tank sets the left and right wheel speeds (−100 to 100).
The simulator has one arena: a looping line and some boxes. Students drag the boxes and the robot with the mouse. The robot does not stop instantly: it slows over a short distance, like a car, which is why the stop distance is 15 cm, not 1.
The robot only knows the distance when the program reads it, many times a second. Ask where the sensor is read and how often.
Stretch: Make a 'nervous' robot: when something comes closer than 10 cm, it reverses (robot motor tank −30 −30) for half a second, then stops.
Support: Give the parking robot's if / else if / else blocks with the numbers filled in, and ask students to drag in the right motor block for each part.
Next: instead of stopping, turn away and keep exploring, like a robot vacuum. Real-world links for discussion: parking sensors, automatic doors, warehouse robots that slow down near people.