Skip to main content

Lesson 3 · Bounce Driving

Module 2 · Line Tracking

Lesson 3 · Bounce Driving

50–60 minif / elseDecisions
👩‍🏫

Teacher mode is on. Toggle it off (bottom-right) to preview the student view.

Stopping at the line is fine — but what if the robot should keep going, bouncing off each edge like a ball in a box? To do that, the robot has to make a decision every moment: if I see the line, turn around; otherwise, keep driving. That's the if/else statement.

Learning Objectives

By the end of this lesson you will be able to:

  • Write if/else statements that choose between two actions
  • Combine while True, if/else, and a sensor read for continuous decisions
  • Use the logical operators and, or, and not
  • Program the robot to bounce between the edges of the circle

Making a decision with if/else

An if/else runs one block when a condition is true and a different block when it's false — never both:

if condition:
# runs when the condition is True
else:
# runs when the condition is False

A non-robot example makes the shape clear:

temperature = 85
if temperature > 80:
print("It's hot outside!")
else:
print("It's comfortable.")
Knowledge Check

When an if/else runs, how many of the two blocks execute?

The bounce algorithm

The behavior in plain English:

loop forever:
read the sensor
if the sensor sees the line:
stop, then turn around
else:
keep driving forward

"Loop forever" is while True: — a loop whose condition is always true, so it runs until you power the robot off. That's completely normal in robotics; real robots run a loop like this the whole time they're on.

from XRPLib.reflectance import Reflectance
from XRPLib.differential_drive import DifferentialDrive
from XRPLib.board import Board

reflectance = Reflectance.get_default_reflectance()
drivetrain = DifferentialDrive.get_default_differential_drive()
board = Board.get_default_board()

threshold = 0.5

board.wait_for_button()

while True:
left = reflectance.get_left()

if left > threshold:
drivetrain.stop()
print("Line detected! Turning around...")
drivetrain.turn(180)
else:
drivetrain.set_effort(0.3, 0.3)
Knowledge Check

What does `while True:` do?

Combining conditions

One sensor can miss the line if the robot hits it at an angle. Checking both is safer — and that's where or comes in: it's true if either side is true.

if left > threshold or right > threshold:
# at least one sensor sees the line → bounce
drivetrain.turn(180)
else:
drivetrain.set_effort(0.3, 0.3)

The three logical operators: and (both must be true), or (at least one true), not (flips true/false).

Knowledge Check

left is 0.2 and right is 0.9 (threshold 0.5). Is `left > threshold or right > threshold` True?

Activity · Bounce and extend

  1. Run the single-sensor version and watch the robot bounce back and forth.
  2. Upgrade to the or version so it bounces off the line no matter which sensor hits first.
  3. Challenge: add a bounce_count variable that goes up by one each bounce and print it — you'll build on counters like this later.

Real-world connections

if/else decisions are the backbone of anything that reacts:

Driving

Self-driving cars

"If an obstacle is ahead, brake; otherwise keep going" — millions of these checks per second.

Safety

Smoke alarms

If smoke is over the threshold, sound the alarm; else stay quiet.

Apps

Form validation

If the password is too short, show an error; otherwise let you continue.

Wrap-up

  • How many branches of an if/else run each time? (Exactly one.)
  • What does while True mean, and how do you stop it? (Loop forever; stop the robot.)
  • What's the difference between and and or? (and needs both true; or needs at least one.)

Resources