← Robotics challenges

Stop before you crash

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.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

The program as blocks
Show as code
stop.ts
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.

A box is in front of the robot. What happens when the program runs?

RunRun it and compare the result with your prediction.

Run

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.

The program as blocks
Show as code
stop.ts
robot.elecfreaksCuteBot.start()
robot.motorTank(50, 50)
basic.forever(function () {
    if (robot.obstacleDistance() < 15) {
        robot.motorStop()
    }
})
Try it yourselfYour edits are saved in this browser.
It opens full screen, so the robot simulator has room, and takes a few seconds to load. Esc comes back. Your work in this step is kept.

InvestigateTrace how each line of code connects to what you see.

Investigate

This version also shows an X when it stops, so you can see the moment the question became true.

The program as blocks
Show as code
stop.ts
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:

The program as blocks
Show as code
robot.elecfreaksCuteBot.start()
robot.motorTank(50, 50)
if (robot.obstacleDistance() < 15) {
    robot.motorStop()
}
With nothing in front of the robot, what does obstacle distance give?

Why stop at 15 cm instead of 1 cm?

Mia's robot crashes into every box. Which part is wrong?

ModifyChange the code on purpose and check the result.

Modify

Make the robot stop when the box is closer than 25 cm. Drag a box in front and test it.

Need a hint?

Change 15 to 25 in the if.

Show an X on the micro:bit when the robot stops.

Need a hint?

show icon is in Basic.

Another hint

Put it inside the if, under robot motor stop. Choose the X.

Try it yourselfYour edits are saved in this browser.
It opens full screen, so the robot simulator has room, and takes a few seconds to load. Esc comes back. Your work in this step is kept.

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.

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Make

Make a parking robot, like a car easing into a parking space:

Test it by dragging a box towards the robot, and away again.

Try it yourselfYour edits are saved in this browser.
It opens full screen, so the robot simulator has room, and takes a few seconds to load. Esc comes back. Your work in this step is kept.
Need a hint?

Inside forever: if the distance is less than 10, stop. Else if it is less than 30, drive slowly. Else, drive at 50.

Another hint

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.

Info

On a real robot

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:

For teachers

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.

Students will be able to

Before class

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.

Running the lesson

  1. Predict: most say it stops before the box. A few pick 'crashes because it checks once': ask them to hold that thought for Mia's robot.
  2. Run: students must drag a box into the robot's path first. If the robot drives off, drag it back and face it at a box.
  3. Investigate: Mia's robot is the run-once mistake from micro:bit Foundations, in a place where it matters. Ask: when did Mia's program check the distance?
  4. Modify: 25 cm leaves a bigger gap. Ask why a real car's parking sensor starts beeping much farther away.
  5. Make: three answers from one number, so if / else if / else, checked in order. The order matters: 'less than 30' first would catch 5 cm too, and the robot would never stop.
  6. Real robot (optional): this is where tuning happens. Expect different stop distances on carpet and desks.

The hard part (on purpose)

The robot only knows the distance when the program reads it, many times a second. Ask where the sensor is read and how often.

When students get stuck

Stretch and support

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.

Where it leads

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.

What each step is for

Now you can
Next lesson4. Avoid and exploreTurn away when something is close, and keep exploring, choosing between two moves with if and else.