Skip to main content

Lesson 2-3 · Bounce Driving

Module 2 · Line Tracking

Lesson 2-3 · Bounce Driving

50–60 minif / elseDecisionsSlides (PowerPoint)
👩‍🏫

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, and write is_either_on_line() around the or
  • Write the bounce as a test function in robot.py, from a pseudocode outline

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.

Knowledge Check

What does `while True:` do?

Combining conditions — and the function that asks​

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, so get_left() > THRESHOLD or get_right() > THRESHOLD means at least one sensor sees the line.

The three logical operators: and (both must be true — you used it in Lesson 2-2), or (at least one true), not (flips true/false).

That or question — is either sensor on a line? — is going to be asked by the bounce, by next lesson's random bounce, and by the turns in Lesson 2-9. So it becomes the next function in your LINE SENSOR section. It returns True or False: a Boolean is a value like any other, and return can hand one back just as it handed back a number in Lesson 1-11. The name starts with is_ because it answers a yes/no question — a convention you'll see in every function like it from here on.

Knowledge Check

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

This lesson's additions to robot.py​

One function for the LINE SENSOR section — add it under get_right() — and one test to paste above RUN. The test is the exercise again: the pseudocode from above is inside it as comments.

def is_either_on_line():
# return True when AT LEAST ONE reading is above THRESHOLD
pass
def test_bounce():
# loop forever:
# if the left sensor sees the line:
# stop, print a message, turn around
# else:
# drive forward
pass

Activity · Build the bounce​

An animation, seen from above. The robot drives forward from the middle of a taped circle. While its sensor reads white the else branch runs and it keeps driving; when the sensor reaches the tape the if branch runs: it stops and turns 180 degrees, then drives back across the circle and bounces off the far side. A blue line traces its path, and every bounce lands on the same line, so it paces back and forth across one diameter. Beside it, the outline of test_bounce() highlights the line that is running.
What step 1 should look like. Every bounce is exactly 180 degrees, so the robot paces one line. Lesson 2-4 fixes that.

Step 1 · One sensor​

Turn the comments in test_bounce() into code, with get_left() > THRESHOLD as the condition, and set the run line to test_bounce(). Start the robot inside the circle, press the button, and watch it bounce back and forth. If it drives through the line, check which way your comparison points — the sensor reads high on the line.

Step 2 · Either sensor​

Write the body of is_either_on_line(). Check it before it drives anything: add " On line:", is_either_on_line() to the print in test_sensors(), run that, and slide the robot by hand — True with either sensor on tape, False with both off. Then change the condition in test_bounce() to call it, and watch the robot bounce whichever sensor hits first.

Step 3 · Challenge — count the bounces​

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​