Lesson 2-2 · Drive to the Edge and Stop
Lesson 2-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 - Write a drive-until-the-line test function from a comment outline, in your
robot.py - 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'?
Driving inside a loop: set_effort()
One more tool before you write the program. Every drive so far has been
straight() or turn(), and both block: they finish their distance before the
next line runs, so you can't watch a sensor while they're going. set_effort(left, right) is different — it sets the motor power and returns immediately. The robot
keeps rolling while your program goes on to the next line, which means a loop can
drive and check a sensor on every pass:
drivetrain.set_effort(0.3, 0.3) # both motors at 30%, and come straight back
The motors stay on until something changes them — drivetrain.stop() turns them off.
This lesson's additions to robot.py
Two things. First, the threshold you picked last lesson becomes a setting in the
LINE SENSOR section — add this line right under the section's comment, above
get_left(). Capitals mean this is a setting, not a value that changes while the
program runs; Python doesn't enforce that, readers rely on it.
THRESHOLD = 0.5 # use YOUR value from Lesson 2-1
Second, a test function to paste above RUN. This time the test is the exercise:
the outline is inside it, and you write the body.
def test_drive_to_line():
# read the left sensor into a variable
# while that reading says "white":
# drive forward
# read the sensor again
# stop the motors
# print the reading that ended the loop
pass
You know everything it needs: get_left() from last lesson, THRESHOLD, how to
write a while loop, and now how to drive inside one.
Activity · Drive until the line
Step 1 · Write it and run it
Turn the comments into code and set the run line to test_drive_to_line(). Start the
robot inside the taped circle pointing at an edge, press the button, and watch it stop
at the line. If it stops as soon as you press the button, the sensor was already
reading "black" — check your threshold. If it never stops, read on.
Step 2 · Either sensor
Switch to get_right() and make sure it still works. Then make the loop keep going
only while both sensors are still on white, so the robot stops when either one
hits the line — you'll need and between two conditions, and a second sensor read
inside the loop. (More on and and or next lesson.)
Step 3 · Tidy it
Once step 2 works, try reading the sensors in the condition itself:
while get_left() < THRESHOLD and get_right() < THRESHOLD:
Now there's no variable to remember to update — the sensors are read fresh every time the condition is checked, which is every pass. Your loop body shrinks to one line, and the two reads before the loop go away. Most sensor loops in this course are written this way.
The infinite-loop trap
Did your robot drive forever in step 1? Then your loop is missing its most important line. If the sensor is read once before the loop and never again, the variable in the condition never changes — so the condition never becomes false, and the robot drives until you catch it. That's an infinite loop, and it is the most common bug in every sensor program you'll ever write.
The rule: always update the variable your
whileloop is checking, inside the loop — or put the sensor call in the condition, so there's no variable to forget.
A student's robot drives forward and never stops, even over the line. What did they most likely forget?
THRESHOLD is 0.5 and the sensor reads 0.3. Is `left < THRESHOLD` True or False?
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.)