Lesson 3 · Bounce Driving
Lesson 3 · Bounce Driving
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/elsestatements that choose between two actions - Combine
while True,if/else, and a sensor read for continuous decisions - Use the logical operators
and,or, andnot - 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.")
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)
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).
left is 0.2 and right is 0.9 (threshold 0.5). Is `left > threshold or right > threshold` True?
Activity · Bounce and extend
- Run the single-sensor version and watch the robot bounce back and forth.
- Upgrade to the
orversion so it bounces off the line no matter which sensor hits first. - Challenge: add a
bounce_countvariable 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:
Self-driving cars
"If an obstacle is ahead, brake; otherwise keep going" — millions of these checks per second.
Smoke alarms
If smoke is over the threshold, sound the alarm; else stay quiet.
Form validation
If the password is too short, show an error; otherwise let you continue.
Wrap-up
- How many branches of an
if/elserun each time? (Exactly one.) - What does
while Truemean, and how do you stop it? (Loop forever; stop the robot.) - What's the difference between
andandor? (andneeds both true;orneeds at least one.)