← Teacher guide

Avoid and explore

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.

Robotics challenges · Sense · about 45 minutes · Open the student lesson

Classroom resources

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.

Students will be able to

Timing

Predict and Run 8 min · Investigate 8 min · Modify 12 min · Make 12 min · Real robot 5 min (optional)

Before class

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.

Running the lesson

  1. Predict: most predict it turns away. Ask what makes it drive forward again afterwards.
  2. Run: drag boxes into its path. Mention the walls before anyone asks.
  3. Investigate: Zara's robot is the < and > mix-up from micro:bit Foundations; it is very visible here.
  4. Modify: −50 −50 backs up. Colours: robot set color, click the swatch.
  5. Make: an if … else inside an if is new for some. Draw the decision as a tree on the board: close? then coin toss: left or right.
  6. Real robot (optional): book walls work well. Soft objects are hard for ultrasonic sensors.

The hard part (on purpose)

if … else chooses a move every time round, with no plan. Ask them to read the question aloud and say when it is true.

When students get stuck

What you will seeA question to askThe 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.

Answers to the checks

  1. Predict: The robot drives towards a box. What happens?
    It turns away and keeps exploring. When the box is closer than 15 cm, it turns for half a second; once the way is clear, else drives it forward again.
  2. Back to forward: After turning away, how does the robot know to drive forward again?
    forever runs the if again: the box is no longer close, so else drives forward
    forever checks again straight after the turn. The way is clear now, so the question is false and else runs: drive forward.
  3. Zara's robot: Zara's robot spins on the spot in open space, and drives straight into boxes. Which part is wrong?
    The > should be <: her robot turns when things are far away and drives when they are close
    'distance > 15' is true when nothing is near, so Zara's robot turns in open space. When a box is close, the question is false and else drives straight at it.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › Back up first

The program as blocks
Show as code
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)
    }
})

Modify › Indicator lights

The program as blocks
Show as code
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)
    }
})

Make

The program as blocks
Show as code
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 and support

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.

Exit ticket

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.

What each step is for

Where it leads

Next: follow a line instead of exploring at random. Real-world link: early robot vacuums worked like this; newer ones map the room.