- Learnt28 min · two dots, colour is touching colour, the steering rule
- Basic24 min · paint the dots, build the follower
- Challenge 1–327 min · say it, a new track, a line with an end
- Extrabonus · two robots, one line
Review — where we got to 5 min
<touching color [#23262D]?>asks if any part of the robot is on that colour.- Inside a forever loop, the question is asked every frame.
- Green pads, red holes and the finish all worked the same way.
Quick-fire
Last lesson’s autopilot drove straight past the first bend. Why?
Reveal the answer
It only knew “on the line” or “off the line”. It never knew which side the line went. To steer, you need to know left from right.
Today’s Topic 3 min
- Why one sensor cannot steer
- Two sensor dots, painted on the robot
<color [#FF00D4] is touching [#23262D]?>- The steering rule, written as if / else
Learning outcome
By the end of this lesson you will be able to:
- Ask whether one part of a sprite touches a colour.
- Put an if / else inside another if / else.
- Make a robot follow a line with no keys pressed.
Learnt — a robot that steers 28 min
1 · One sensor is not enough
Look at these two robots. Both are touching the line. Touching colour says “yes” to both.
But one must turn right and one must turn left. “Yes” does not say which. We need to know where the line is.
2 · Two dots on the nose
Real line robots have two small light sensors at the front. Ours are two painted dots.
- The pink dot is on the robot’s left.
- The sky-blue dot is on the robot’s right.
- The line should fit between them, with a little gap each side.
The nose points right, so the robot’s left is the top of the drawing. Pink goes at the top. Sky blue goes at the bottom.
3 · A question about one part
Sensing has a block with two colour swatches:
<color [#FF00D4] is touching [#23262D]?>
It asks: is my pink part touching the line? The rest of the robot does not count. Only the pink dot.
- Drag the block into the script area.
- Click the first swatch. Use the eyedropper to click the pink dot on your robot.
- Click the second swatch. Use the eyedropper to click the line.
- Click the block. It shows true or false.
This demo only reports what the dots see. You still drive.
when flag clicked
go to x: (-17) y: (118)
point in direction (90)
forever
if <key [left arrow v] pressed?> then
turn ccw (6) degrees
end
if <key [right arrow v] pressed?> then
turn cw (6) degrees
end
if <key [up arrow v] pressed?> then
move (3) steps
end
end
when flag clicked
forever
if <color [#FF00D4] is touching [#23262D]?> then
say [Left sees the line]
else
if <color [#00E0FF] is touching [#23262D]?> then
say [Right sees the line]
else
say []
end
end
end
How to use this: Click the green flag, then click the stage once. Hold ↑ to drive the robot, and ← or → to turn it.
4 · The steering rule
| What the dots see | What the robot does |
|---|---|
| The pink dot is on the line | turn ccw (5) degrees — left, towards the line |
| The sky-blue dot is on the line | turn cw (5) degrees — right, towards the line |
| Neither dot is on the line | move (3) steps — the line is between them |
5 · The rule, in blocks
Three answers need two questions. An if / else goes inside the else of another one.
when flag clicked
go to x: (-17) y: (118)
point in direction (90)
forever
if <color [#FF00D4] is touching [#23262D]?> then
turn ccw (5) degrees
else
if <color [#00E0FF] is touching [#23262D]?> then
turn cw (5) degrees
else
move (3) steps
end
end
end
How to use this: Click the green flag to start it. Nothing else to press — the robot drives itself. Use ↺ Reset to run it again.
Basic — paint the dots, build the follower 24 min
Step 1 — a loop to follow
- Add a new backdrop. Fill it pale, like last lesson.
- Draw a thick dark loop with the Line tool, or the Circle tool with no fill.
- Keep every bend gentle and round.
Step 2 — the dots
- Open your robot’s costume.
- With the Circle tool, paint a pink dot at the front, on the top side.
- Paint a sky-blue dot at the front, on the bottom side.
- Check the line fits between them, with a gap each side.
Step 3 — the follower
- Remove the arrow-key script. Keep the start blocks.
- Add
foreverand anif / elseinside it. - Put
<color [#FF00D4] is touching [#23262D]?>in the hexagon. Pick both colours with the eyedropper. - In the top half, put
turn ccw (5) degrees. - In the
else, put a secondif / elsefor the sky-blue dot. - Its top half turns right. Its bottom half is
move (3) steps.
Step 4 — test it
Put the robot on the line and press the flag. Let it do three laps.
The robot spins round and round on the spot.
Both dots are on the line, or the turn goes the wrong way. Check pink turns ccw (left) and sky blue turns cw (right).
The robot drives straight off the line.
The dots never say true. Pick both colours again with the eyedropper — first on the dot, then on the line.
The robot wobbles left, right, left at one corner and stops.
The corner is too sharp. Make that bend rounder, or move the dots a little closer to the costume’s centre.
Say what you are doing
Make the robot tell you what it is doing, as it does it.
- It says “left” while it turns left.
- It says “right” while it turns right.
- It says “straight” while it drives forward.
How to use this: Click the green flag to start it. Nothing else to press — the robot drives itself.
Teacher note
One say [] in each of the three places — above the turn or move block. Pupils who put all three says in one place have found out that the order of blocks inside an if matters less than which half they are in.
A new track
Paint a second track with a wave in it. Your robot must follow it too.
- The new track has at least one wave or S-bend.
- You change only the start position. The steering code stays the same.
- The robot does two whole laps without getting stuck.
How to use this: Click the green flag to start it. Nothing else to press — the robot drives itself.
Teacher note — when it gets stuck
A pupil whose robot wobbles at one bend has drawn that bend too sharp. The fix is in the paint editor, not the code. Let them find it by watching where it stalls.
A line with an end
Delivery robots do not go round in circles. They follow a route and stop at the end.
- Paint a line with two ends. Put a yellow pad over the far end.
- The robot follows the line by itself.
- On the yellow pad, it stops and says “Arrived!”.
How to use this: Click the green flag to start it. Nothing else to press — watch it through, then use ↺ Reset to run it again.
Teacher reveal — forever becomes repeat until
when flag clicked
go to x: (-114) y: (-20)
point in direction (55)
repeat until <touching color [#FACC15]?>
if <color [#FF00D4] is touching [#23262D]?> then
turn ccw (5) degrees
else
if <color [#00E0FF] is touching [#23262D]?> then
turn cw (5) degrees
else
move (3) steps
end
end
end
say [Arrived!]
The steering is unchanged. Only the loop around it changed: a question to stop on, and a block after it. Two sensors steer; a third question stops.
Two robots, one line
Put a second, slower robot on the same loop. The fast one must never crash into it.
- The blue robot moves 5 steps. The orange robot moves 2.
- They start on opposite sides of the loop.
- When blue gets close to orange, it waits and says “Waiting...”.
- When orange has moved on, blue carries on.
How to use this: Click the green flag to start it. Nothing else to press — watch the blue robot catch up and wait.
Teacher reveal — the blue robot
when flag clicked
go to x: (-17) y: (118)
point in direction (90)
forever
if <(distance to [Orange robot v]) < (90)> then
say [Waiting...]
else
say []
if <color [#FF00D4] is touching [#23262D]?> then
turn ccw (5) degrees
else
if <color [#00E0FF] is touching [#23262D]?> then
turn cw (5) degrees
else
move (5) steps
end
end
end
end
The orange robot is the plain follower with move (2) steps, starting at go to x: (176) y: (-37). Neither robot is confused by the other: no robot has the line’s colour on it.
Summary 5 min
- One sensor knows “on the line”. It cannot know which way the line went.
<color [#FF00D4] is touching [#23262D]?>asks about one coloured part of a sprite.- Pink sees the line: turn left. Sky blue sees it: turn right. Neither: go.
- An if / else inside an else gives three choices.
- Round bends are easy to follow. Sharp corners trap the robot.
- Line follower
- A robot that steers itself along a line using sensors.
- Colour is touching colour
- A question about one part of a sprite, not the whole sprite.
- Nested if
- An if / else inside another one. Three answers from two questions.
- Costume centre
- The point a sprite turns around.
Hand in
Save your follower. Next lesson its speed and turn become numbers you can tune, and it gets a lap timer.