The answersDownload the PDF
Worksheet

F2.8 Project: parking sensor

Decisions and loops · GCSE · about 25 min

BugBotLab
NameClassDate

What this lesson is about

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

Questions 4 marks in all

  1. [1 mark]Why does the parking sensor use a while loop rather than a for loop?

    1. AThe robot cannot know beforehand how many times round it will take
    2. Bfor loops cannot use the sensor
    3. Cwhile loops are faster
    4. Dfor loops cannot contain if
  2. [1 mark]What does this program print?

    d = 35
    if d > 50:
        gap = 0.4
    elif d > 30:
        gap = 0.25
    else:
        gap = 0.1
    print(gap)
  3. [1 mark]The robot stops slightly closer to the wall than BAY. Why?

    1. AIt coasts a little after stop()
    2. Bdistance() is always wrong
    3. CBAY is a string
    4. DThe loop runs one extra time on purpose
  4. [1 mark]Where must beeps = 0 go?

    1. ABefore the loop
    2. BInside the loop, before the beep
    3. CAfter the loop
    4. DIt is not needed

The 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):

# 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()

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/f2-8-project-parking-sensor/
The simulator checks it and tells you when it passes. Nothing to install, no account.

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.