Click & Whirr needs a wider screen
Open it on a laptop or desktop (1280px or wider recommended). The Learn · Code · Watch workspace needs the room to breathe.
Lesson 02a
Follow the line
Now the robot has a sensormounted out front that reads how far off-center it is from the line. Write the steering logic yourself: when the bot drifts to one side, nudge the wheels to bring it back. It’ll follow the line, wobbling all the way. That wobble is the whole point; we’ll smooth it out next.
Checkpoints
- Catch the line (waiting for Run)
- Reach the end of the line (waiting for Run)
Press Run ▸ to start checking your code.
Edit the logic and watch it react instantly. Goal: keep it on the line all the way to the green flag.
Steps
- 1
React to which side you're off
Branch on the sign of
offset: the sensor tells you which way you drifted. - 2
Steer back toward the line
- 3
Ride it to the flag
▸API reference
- follow_line(offset)
- Called every tick. Return a (left, right) wheel-speed tuple. offset is the line sensor reading: > 0 = robot is right of the line, < 0 = left, 0 = centered.
- offset (float)
- Signed cross-track distance from the line, in pixels. Sign convention: positive means the robot has drifted to the RIGHT of the line.
- robot.set_wheels(left, right)
- Alternative to returning a tuple: set the wheel speeds directly. If you both call this and return a tuple, the returned tuple wins. Speeds are clamped to ±100.
- print(...)
- Prints to the console panel below the editor: handy for watching offset change as the robot drives.
space pause · r reset