Skip to main content

Lesson 2 · Driving Multiple Intersections

Module 3 · Grid Driving

Lesson 2 · Driving Multiple Intersections

40–50 minfor loops on the gridClearing the cross
👩‍🏫

Teacher mode is on. Toggle it off (bottom-right) to preview the student view.

Reaching the first intersection is easy. But call track_until_cross() again and the robot won't budge. This lesson explains why — and gives you the one trick you need to drive across a whole grid.

Learning Objectives

By the end of this lesson you will be able to:

  • Explain why the robot must "clear" an intersection before continuing
  • Drive past one intersection to reach the next
  • Use a for loop to drive a chosen number of intersections
  • Know why you skip the clear after the last intersection

Why the robot gets stuck

When track_until_cross() stops, the robot is sitting on the intersection — both sensors still read high. Call track_until_cross() again and its loop condition (is_at_cross()) is already true, so it thinks it has already arrived and does nothing.

The fix is to clear the cross: drive forward just a bit so the sensors move off the intersection and back onto a plain line, then look for the next one.

# Clear the intersection: nudge forward past it
tracker.drivetrain.set_effort(0.3, 0.3)
time.sleep(0.3)

So the pattern is: detect → clear → detect → clear → …

Knowledge Check

Why does a second track_until_cross() call right after the first do nothing?

Drive a set number with a for loop

Doing detect-and-clear by hand for each intersection is repetitive. A for loop repeats it a chosen number of times — with one subtlety: don't clear after the last one, because you want to stop there.

intersections = 3

for i in range(intersections):
print("Driving to intersection", i + 1)
tracker.track_until_cross()

# Clear — but not after the final intersection
if i < intersections - 1:
tracker.drivetrain.set_effort(0.3, 0.3)
time.sleep(0.3)

print("Done! Passed", intersections, "intersections.")

The if i < intersections - 1: guard is what leaves the robot parked on the last intersection, ready to turn.

Knowledge Check

In a loop that drives 3 intersections, why is the clear step skipped on the last iteration?

Activity · Package it as a helper

Since you'll drive segments constantly, wrap the pattern in a reusable function:

def drive_intersections(tracker, count):
for i in range(count):
tracker.track_until_cross()
if i < count - 1:
tracker.drivetrain.set_effort(0.3, 0.3)
time.sleep(0.3)

Now drive_intersections(tracker, 4) drives four segments in one clean line — a building block you'll lean on in the next two lessons.

Knowledge Check

A student's loop clears after EVERY intersection, including the last. What goes wrong?

Real-world connections

"Do this a set number of times, then stop" is the rhythm of a lot of automation:

Elevators

Floor counting

An elevator counts floor sensors as it rises and stops on the Nth — same detect-and-count idea.

Manufacturing

Indexing lines

Assembly lines advance a fixed number of steps between stations, clearing each position.

Transit

Counting stops

Automated shuttles track how many stops they've passed to know when to pull over.

Wrap-up

  • Why must the robot clear an intersection before continuing? (It's still on the cross, so it would "detect" the same one.)
  • What loop drives a set number of segments? (A for loop over range(count).)
  • Why skip the clear on the last iteration? (To stop on the final intersection.)

Resources