Today’s mission 5 min
Make the robot drive until it sees a line, then choose what to do from what its sensors read.
You have used every SPIKE sensor in blocks. Today you read them in Python, print their real numbers and turn the numbers into decisions with if, elif and else.
The idea — a reading, a question, an answer 10 min
Each sensor gets a name, like a motor does:
eye = ColorSensor(Port.E)—eye.reflection()gives 0 to 100 (dark to bright).eye.color()gives a colour such asColor.RED.sonar = UltrasonicSensor(Port.D)—sonar.distance()gives millimetres. It gives 2000 when it sees nothing at all.button = ForceSensor(Port.C)—button.pressed()givesTrueorFalse.button.force()gives newtons.
A decision is a question with a True or False answer:
==means “is equal to”. One=stores; two==compare.elifmeans “else, if”. It checks the next question only when the earlier ones were false.elsecatches everything left over.
Blocks vs Python 15 min
SPIKE word blocks
when program starts :: events hat set movement motors to (A+B v) :: movement start moving (straight: 0) :: movement wait until <(E v) reflection [< v] (30) %? :: sensors> stop moving :: movement
Pybricks Python
from pybricks.pupdevices import Motor, ColorSensor from pybricks.parameters import Port, Direction from pybricks.robotics import DriveBase from pybricks.tools import wait left = Motor(Port.A, Direction.COUNTERCLOCKWISE) right = Motor(Port.B) robot = DriveBase(left, right, wheel_diameter=56, axle_track=112) eye = ColorSensor(Port.E) robot.drive(200, 0) while eye.reflection() > 30: wait(10) robot.brake()
| Block | Python | Watch out |
|---|---|---|
| start moving (straight: 0) | robot.drive(200, 0) | 200 mm per second, 0 degrees per second of turning. |
| wait until reflection < 30 | while eye.reflection() > 30: wait(10) | Python waits WHILE the opposite is true. Note the flip: < became >. |
| stop moving | robot.brake() | stop() coasts; brake() stops sharply. Choose brake() at a line. |
The wait(10) inside the loop gives the hub a rest between readings. Without it the loop still works, but it reads the sensor far more often than it needs to.
SPIKE word blocks
when program starts :: events hat if <(E v) is color [red v]? :: sensors> then set Power Button light to [red v] :: light else if <(E v) is color [blue v]? :: sensors> then set Power Button light to [blue v] :: light else play beep (60) for (0.2) seconds :: sound end end
Pybricks Python
seen = eye.color() if seen == Color.RED: hub.light.on(Color.RED) elif seen == Color.BLUE: hub.light.on(Color.BLUE) else: hub.speaker.beep(262, 200) print("saw", seen)
| Block | Python | Watch out |
|---|---|---|
| is color red? | seen == Color.RED | Read the colour once into a variable; test the variable. |
| if-else inside else | elif | No nesting needed. Ten colours stay ten flat lines. |
| else | else: | Ends with a colon, like every line that opens a block. |
SPIKE word blocks
when program starts :: events hat forever write ((D v) distance in [cm v] :: sensors) :: display wait (0.2) seconds end
Pybricks Python
sonar = UltrasonicSensor(Port.D) while True: mm = sonar.distance() print(mm) wait(200)
| Block | Python | Watch out |
|---|---|---|
| distance in cm | sonar.distance() | Millimetres. 15 cm = 150. |
| (no reading) | 2000 | 2000 means nothing was seen. Treat it as "far", never as a real wall. |
Write it from scratch 20 min
New file: sensors. Copy only your calibrated drive-base lines from lesson 4. Type the rest.
- Print first. A loop that prints
eye.reflection()every 200 ms. Hold the robot over white mat, black line and the line’s edge. Write all three numbers down. - Choose your threshold. Halfway between black and white. If black is 8 and white is 92, the threshold is 50 — not the 30 on this page.
- Drive until the line with your own threshold.
- Then decide. After stopping, read the right sensor (F). If it also sees black, the robot is square to the line: beep high. Otherwise beep low.
Both sensors, one decision
BLACK_LINE = 50 # your threshold, from step 2 left_eye = ColorSensor(Port.E) right_eye = ColorSensor(Port.F) robot.drive(200, 0) while left_eye.reflection() > BLACK_LINE: wait(10) robot.brake() if right_eye.reflection() < BLACK_LINE: print("square to the line") hub.speaker.beep(1000, 200) else: print("crooked: right sensor still on white") hub.speaker.beep(200, 200)
Test and measure 15 min
- Run the line stop at speeds 100, 300 and 500. Measure how far past the line the sensor ends up each time. Why does speed matter?
- Swap
robot.brake()forrobot.stop(). How much further does it roll? - Hold coloured bricks under the sensor and print
eye.color(). Which colours does it confuse? Write them down — lesson 15 fixes this. - Point the Distance Sensor at a wall from 50 mm to 1000 mm. Where does it stop being trustworthy? What does it print when you point it at the ceiling?
Break it, fix it 10 min
One mistake in each. Two give errors; one gives a wrong robot.
Bug 1
seen = eye.color()
if seen = Color.RED:
hub.light.on(Color.RED)Bug 2
robot.drive(200, 0)
while eye.reflection > 30:
wait(10)
robot.brake()Bug 3 — no error; the robot crashes into the wall
sonar = UltrasonicSensor(Port.D)
robot.drive(200, 0)
# stop 10 cm from the wall
while sonar.distance() > 10:
wait(10)
robot.brake()Bug 2’s message says TypeError. Without brackets, eye.reflection is the method itself, not a reading. A method cannot be compared with a number. Brackets () mean “do it now”.
Solo challenge — no coach 10 min
No coach, no help. At WRO your coach may not touch the robot or the code. Close this page. You may use your own earlier programs and the Pybricks documentation, nothing else.
Line, then look. Put a black line across the floor. Beyond it, sometimes put a box 200 mm away, sometimes not. Write look, which:
- drives until the left sensor finds the line, and brakes;
- reverses 50 mm;
- if the Distance Sensor sees something nearer than 300 mm, turns round and drives 300 mm back;
- otherwise drives on 300 mm past the line;
- prints the distance it measured and the choice it made.
Done means: it chooses correctly five times out of five, with the box moved by another team.
Engineering journal 5 min
- your reflection numbers for white, black and the edge
- your threshold, and how you worked it out
- how far past the line the robot stopped at each speed
- the colours the sensor confused
- the Distance Sensor’s trustworthy range, in mm