Project: parking sensor

A while loop, decisions and the buzzer: beep faster as the wall gets closer, and park.

F2.8Decisions and loopsGCSE25 min

Do this lesson in the simulator

A car's parking sensor beeps as you reverse towards a wall, faster and faster, until it holds one long note when you are nearly touching. You will build one on BugBot: a loop that reads the distance sensor, decisions about how close the wall is, and the piezo to tell you. It uses everything from F2 and most of F1.

The brief

The robot drives slowly towards the wall ahead. While it drives it beeps: slowly when the wall is far, faster as it gets close. It stops in the parking bay, the band just in front of the wall, without touching the wall. Then it plays one long note, turns the LED red, and prints how far it is from the wall and how many beeps it gave.

Plan

Inputs Processing Outputs
distance to the wall, read again every time round keep driving while the wall is further than the bay the drive
choose the gap between beeps from the distance the beeps
count the beeps stopped <cm> cm from the wall after <n> beeps

Which kind of loop? The robot cannot know how many times round it will take, so a while. Which kind of selection? Three or more bands of distance, so if, elif, else.

Step 1: drive to the bay

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

BAY = 20          # stop when the wall is this close, in cm

while distance() > BAY:
    forward(30)
    wait(0.1)
stop()
print("stopped", distance(), "cm from the wall")

Run this in the simulator

Run it and look where the robot stopped. The robot coasts a little after stop(), so it ends up slightly closer than BAY. Change BAY and the speed until it stops inside the green band every time.

Step 2: a beep that depends on the distance

Inside the loop, choose how long to wait between beeps:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

d = distance()
if d > 50:
    gap = 0.4
elif d > 30:
    gap = 0.25
else:
    gap = 0.1
print("wall in", d, "cm, beep every", gap, "s")
tone(880, 0.05)
wait(gap)

Run this in the simulator

A short beep, then a pause of gap. The pause is where the robot keeps driving, so the wait(gap) replaces the wait(0.1) from step 1.

Step 3: put them together

Put step 2 inside step 1's loop, add a counter for the beeps, and finish with the long note, the red LED and the report. Build it in the task below, running after each change.

Testing

Check each part of the brief, one at a time:

  • Does it beep more often near the wall? Watch the note appear under the robot, or listen.
  • Does it stop in the bay? Run it three times.
  • Does it touch the wall at any speed you might choose? Try speed 50.
  • Is the beep count printed at the end the same as the number of beeps you counted?

Task: parking sensor

Drive to the wall, beeping faster as it gets closer, and stop in the green bay without touching the wall. Then play a long note, turn the LED red and print one line in exactly this shape (your numbers will differ):

stopped 18.5 cm from the wall after 9 beeps
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

BAY = 20
beeps = 0

while distance() > BAY:
    forward(30)
    wait(0.1)
stop()

Challenges

  1. Make the note get higher as well as faster when the wall is close.
  2. Add a fourth band that holds a continuous note under 15 cm.
  3. Reverse away from the wall afterwards until it is 50 cm away, beeping the other way round.