Skip to main content

Lesson 2 · Drive to the Edge and Stop

Module 2 · Line Tracking

Lesson 2 · Drive to the Edge and Stop

50–60 minwhile loopsSensor-driven control
👩‍🏫

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 while loop 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 while loop differs from a for loop

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)
Knowledge Check

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 while loop is checking, inside the loop.

Knowledge Check

A student's robot drives forward and never stops, even over the line. What did they most likely forget?

Knowledge Check

The threshold is 0.5 and the sensor reads 0.3. Is `left < threshold` True or False?

Activity · Try it and extend it

  1. Build the program with your threshold, start the robot inside the taped circle pointing at an edge, and watch it stop at the line.
  2. 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:

Home

Thermostats

The furnace runs while the room is below the set temperature, then stops — a while loop in hardware.

Software

Downloads

A download loops "grab the next chunk" while there's more file left to fetch.

Games

Game loops

A game repeats "update and draw" while the game is still running.

Wrap-up

  • When do you reach for while instead of for? (When you repeat until a condition changes, not a fixed count.)
  • What's the one line you must never forget in a sensor while loop? (Re-reading the sensor inside the loop.)
  • What does set_effort() do that straight() can't? (Runs the motors without blocking, so the loop can keep checking.)

Resources