SPIKE Prime Robotics Camp — Day 4
Give your robot eyes
Each day upgrades our robot.
Give your robot eyes:
The robot reacts to the world, not just to your code.
Bugs are sensor data.
The robot did exactly what the code said.
What did the code actually say?
Keep watching the signal and react.
Green = move, red = freeze.
That's a sensor loop: check, decide, do — over and over.
Make sure your robot still drives and turns.
Then plug in and mount the distance sensor.
Same robot, same ideas. Pybricks shows the Python next to your blocks.
The ultrasonic sensor measures how far away things are.
from pybricks.pupdevices import UltrasonicSensor
eyes = UltrasonicSensor(Port.C) # use YOUR port
Plug it in and use the real port.
print(eyes.distance()) # millimeters
Move your hand closer — watch the number drop.
Nothing in front? It reads 2000 mm.
If something's close, do one thing. Otherwise, do another.
if eyes.distance() < 200:
robot.straight(-100) # back up
robot.turn(90) # turn away
else:
robot.drive(150, 0) # forward, no turn
200 mm is a guess — measure and adjust.
whileCheck the sensor again and again, forever.
while True:
if eyes.distance() < 200:
robot.straight(-100)
robot.turn(90)
else:
robot.drive(150, 0)
wait(10)
while True: runs foreverIt won't stop on its own — that's expected.
Get up, move, refuel.
Keep food away from the kits and laptops!
React to obstacles on the course.
Measure on the real course.
Don't fall off the table!
Sense the edge (or wall) and back away in time.
distance() (mm)if / else makes decisionswhile True: keeps checking — Stop button ends itLine Following + Showcase
We'll teach the robot to follow a line with the color sensor —
steering in proportion to how far off it is —
then show off everything from the week.