Lesson 7 · Detecting Intersections
Lesson 7 · Detecting Intersections
Teacher mode is on. Toggle it off (bottom-right) to preview the student view.
Add a taped cross to your circle and your line follower has a new problem: when the robot reaches the crossing, both sensors hit black at once. This lesson turns that into a feature — the robot detects the intersection, reacts to it, and keeps following. It's where everything from Lessons 1–6 comes together.
Learning Objectives
By the end of this lesson you will be able to:
- Explain what an intersection looks like to the two sensors
- Write a condition with
andthat fires when both sensors are on the line - Combine intersection detection with line following in one loop
- Program a response to the cross (stop, turn around, continue)
What a cross looks like to the robot
During normal following, one sensor is darker than the other — the readings differ. At a crossing line, both sensors sit on black at the same time. That's the tell:
if left > threshold and right > threshold → we're at a cross
and means both conditions must be true (you met its partner or back in Lesson
3). Here, only both-sensors-on-black should count as an intersection.
Which condition detects an intersection (both sensors on the line)?
Detect, then follow
The structure is one if/else inside the control loop: check for the cross
first; if it's not a cross, do normal two-sensor following.
from XRPLib.reflectance import Reflectance
from XRPLib.differential_drive import DifferentialDrive
from XRPLib.board import Board
import time
reflectance = Reflectance.get_default_reflectance()
drivetrain = DifferentialDrive.get_default_differential_drive()
board = Board.get_default_board()
threshold = 0.5
Kp = 0.5
base_effort = 0.3
board.wait_for_button()
while True:
left = reflectance.get_left()
right = reflectance.get_right()
if left > threshold and right > threshold:
# Intersection! Stop and turn around
drivetrain.stop()
print("Cross detected! Turning around...")
drivetrain.turn(180)
time.sleep(0.3) # roll past the cross before checking again
else:
# Normal two-sensor line following
error = left - right
correction = error * Kp
drivetrain.arcade(base_effort, -correction)
Why is there a time.sleep(0.3) right after the robot turns at the cross?
Activity · Follow, reverse, and count
- Run the follow-and-reverse program: the robot laps the circle, hits the cross, turns 180°, and heads back.
- Challenge: count the crossings and stop after four — a direct preview of the final project:
cross_count = 0
max_crosses = 4
while cross_count < max_crosses:
left = reflectance.get_left()
right = reflectance.get_right()
if left > threshold and right > threshold:
cross_count = cross_count + 1
drivetrain.stop()
print("Cross #", cross_count)
drivetrain.turn(180)
time.sleep(0.3)
else:
error = left - right
drivetrain.arcade(base_effort, -error * Kp)
print("Done! Crossed", cross_count, "times.")
During normal line following (not at a cross), which branch of the if/else runs?
Real-world connections
Watching a continuous stream for a specific event is everywhere in automation:
Rail junctions
Trains detect track sensors at switches to decide which way to route.
Conveyor triggers
A sensor spots each item passing a point and triggers a sort, stamp, or count.
Step & tap detection
Your watch watches a constant sensor stream for the pattern that means "a step" or "a tap."
Wrap-up
- What does the robot see at an intersection? (Both sensors on black at once.)
- Why
andand notor? (Both must be true;orwould fire during normal following.) - Why the pause after turning? (So it clears the cross instead of re-detecting it.)