The answersDownload the PDF
Worksheet

5.3 Timers

Behaviours · Robot club · about 15 min

BugBotLab
NameClassDate

What this lesson is about

Two things at once, remembered ticks, timeouts.

Questions 7 marks in all

  1. [1 mark]Why can a program not blink the LED with wait(0.5) while it drives to the zone?

    1. Await(0.5) holds up the whole loop, so the driving stops being updated
    2. Bwait turns the LED off
    3. CThe LED cannot change while the robot moves
    4. Dwait only accepts whole numbers
  2. [1 mark]What does this program print?

    on = False
    changes = 0
    for ticks in range(20):
        if ticks % 5 == 0:
            on = not on
            changes += 1
    print(changes, on)
  3. [1 mark]The loop waits 0.1 s a tick. To toggle the LED every half second, every how many ticks should it toggle?

  4. [1 mark]What does this program print?

    started = None
    for ticks in range(40):
        if started is None and ticks == 20:
            started = ticks
            print("start", ticks)
        elif started is not None and ticks - started >= 10:
            print("stop", ticks)
            started = None
  5. [1 mark]In started_turning = None, what does None mean?

    1. ANo timer is running
    2. BThe timer has run for 0 ticks
    3. CThe robot has stopped
    4. DThe turn failed
  6. [1 mark]A search spins while ticks < 30 and gives up if nothing is found. Why is this timeout important?

    1. AWithout it, a search for a tag that is not there would spin forever
    2. BThe camera stops working after 3 seconds
    3. CIt makes the tag easier to see
    4. DIt saves the list of tags
  7. [1 mark]What is the advantage of clock() - started_at >= 1.0 over counting ticks?

    1. AIt stays right even if a tick takes longer than planned
    2. BIt works without a loop
    3. CIt makes each tick shorter
    4. DIt stops the robot automatically

The task: blink and drive

Drive into the green zone while the LED blinks green and off every half second, at least eight changes.

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

# the LED: green
led("green")
# drive forward at 60 for 70 cm, then stop
forward(60, distance=70)
# the LED: off
led("off")

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

QR code
Do it on the robot
www.bugbotlab.com/learn/5-3-timers/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Blink faster as the robot gets closer to the zone.
  2. Drive for two seconds, stop for one, repeat, using one timer variable.
  3. Print the total run time in seconds at the end from the tick count. Check it against the telemetry.