Lesson 2 · Drive to the Edge and Stop
Lesson 2 · Drive to the Edge and Stop
Teacher mode is on. Toggle it off (bottom-right) to preview the student view.
Last lesson the robot read the sensor but sat still. Now you'll make it drive and
watch at the same time — rolling forward until the sensor spots the line, then
stopping. The tool that makes this possible is the while loop.
Learning Objectives
By the end of this lesson you will be able to:
- Write a
whileloop that repeats until a condition changes - Use comparison operators (
<,>,==,!=,<=,>=) - Drive the robot with
set_effort()while checking a sensor each pass - Explain why the sensor must be re-read inside the loop
- Say how a
whileloop differs from aforloop
for vs. while
In Module 1 the for loop repeated a fixed number of times — for i in range(4) draws four sides. But "drive until you hit the line" has no fixed count;
you don't know how many steps it'll take. That's a job for while, which repeats as
long as a condition stays true:
while left < threshold:
# this runs over and over while the sensor is on white
# the moment left >= threshold, the loop stops
A while loop needs a condition — a test that's either True or False. We
build those with comparison operators:
0.3 < 0.5 # True (less than)
0.8 > 0.5 # True (greater than)
0.5 == 0.5 # True (equal to)
0.9 != 0.5 # True (not equal to)
Which loop fits the task 'drive until the robot detects a line'?
Drive until the line
Here's the whole program. Read the sensor before the loop, keep driving while you're on white, and stop once the line is detected:
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 # use YOUR value from Lesson 1
board.wait_for_button()
left = reflectance.get_left()
while left < threshold:
drivetrain.set_effort(0.3, 0.3)
left = reflectance.get_left() # ← read again every pass!
print("Line detected! Sensor value:", left)
set_effort(0.3, 0.3) is the key to driving inside a loop: unlike straight(),
it sets the motors and returns immediately, so the loop can keep checking the
sensor while the robot rolls.
The infinite-loop trap
Look at that program again. The line left = reflectance.get_left() inside the
loop is the most important one. Leave it out and left never changes — so the
condition never becomes false and the robot drives forever. That's an
infinite loop.
The rule: always update the variable your
whileloop is checking, inside the loop.
A student's robot drives forward and never stops, even over the line. What did they most likely forget?
The threshold is 0.5 and the sensor reads 0.3. Is `left < threshold` True or False?
Activity · Try it and extend it
- Build the program with your threshold, start the robot inside the taped circle pointing at an edge, and watch it stop at the line.
- Challenge: switch to the right sensor, then stop when either sensor hits the line:
while left < threshold and right < threshold:
drivetrain.set_effort(0.3, 0.3)
left = reflectance.get_left()
right = reflectance.get_right()
The and means "keep going only while both sensors are still on white." (More on
and / or next lesson.)
Real-world connections
"Keep going until a condition is met" runs countless real systems:
Thermostats
The furnace runs while the room is below the set temperature, then stops — a while loop in hardware.
Downloads
A download loops "grab the next chunk" while there's more file left to fetch.
Game loops
A game repeats "update and draw" while the game is still running.
Wrap-up
- When do you reach for
whileinstead offor? (When you repeat until a condition changes, not a fixed count.) - What's the one line you must never forget in a sensor
whileloop? (Re-reading the sensor inside the loop.) - What does
set_effort()do thatstraight()can't? (Runs the motors without blocking, so the loop can keep checking.)