- Learnt28 min · sliders, tuning, marker pads, the timer
- Basic24 min · tune, stop, time
- Challenge 1–327 min · fastest settings, four trips, a three-lap race
- Extrabonus · careful on the bends
Review — where we got to 5 min
<color [#FF00D4] is touching [#23262D]?>asks about the pink dot only.- Pink sees the line: turn left. Sky blue: turn right. Neither: move.
- Changing
forevertorepeat until <>gives the robot somewhere to stop.
Quick-fire
Your robot is too slow. You change move (3) steps to 6. What did you have to do first?
Reveal the answer
Stop the project, find the block, type, and start again. Every single time. Today the numbers become sliders, so you tune while it runs.
Today’s Topic 3 min
(speed)and(turn)as variables- A variable shown as a slider on the Stage
- Marker pads: blue means stop, yellow means turn round
(timer)andreset timer— a lap timer
Learning outcome
By the end of this lesson you will be able to:
- Turn a number in a script into a variable you can change on the Stage.
- Tune two numbers to make a robot fast and safe.
- Time something with Scratch’s own clock.
Learnt — tune it, signal it, time it 28 min
1 · Numbers become variables
The follower has two numbers that matter: how far it moves, and how far it turns.
- In Variables, make
speedandturn, for all sprites. - Drop
(turn)into both turn blocks. - Drop
(speed)into the move block.
when flag clicked
go to x: (-46) y: (97)
point in direction (132)
forever
if <color [#FF00D4] is touching [#23262D]?> then
turn ccw (turn) degrees
else
if <color [#00E0FF] is touching [#23262D]?> then
turn cw (turn) degrees
else
move (speed) steps
end
end
end
set [speed v] to (3) under the flag. The slider owns the number now, and the flag must not undo it.2 · A slider on the Stage
- Tick the box next to each variable, so it shows on the Stage.
- Right-click the speed box on the Stage. Choose slider.
- Right-click it again. Choose change slider range. Type 1 and 30.
- Do the same for turn, from 1 to 30.
- Press the flag. Drag the sliders while the robot runs.
3 · Tuning
Tuning means changing numbers until something works well. Every real robot is tuned. Try these:
| You change | What happens on the Wave |
|---|---|
| speed 3, turn 5 | Safe and slow. |
| speed 12, turn 5 | Much faster, still safe. |
| speed 3, turn 30 | Swings past the line on a bend. Left, right, left — stuck. |
| speed 30, turn 5 | The dots jump over the line on a bend. It flies off. |
In this demo, the Stage moves the numbers for you, like a hand on each slider. Before each bad setting it sends the robot back to the start, so both meet the same bend.
when flag clicked
set [speed v] to (3)
set [turn v] to (5)
broadcast [go v]
wait (4) seconds
set [speed v] to (12)
wait (4) seconds
set [speed v] to (3)
set [turn v] to (30)
broadcast [go v]
wait (4) seconds
set [turn v] to (5)
set [speed v] to (30)
broadcast [go v]
when I receive [go v]
go to x: (-46) y: (97)
point in direction (132)
forever
if <color [#FF00D4] is touching [#23262D]?> then
turn ccw (turn) degrees
else
if <color [#00E0FF] is touching [#23262D]?> then
turn cw (turn) degrees
else
move (speed) steps
end
end
end
How to use this: Click the green flag and watch for about 15 seconds. Nothing to press — the Stage changes speed and turn for you.
4 · Marker pads
Real warehouse robots read marks on the floor. We add two.
- A blue stop line across the line: wait one second, then go on.
- A yellow pad over the end of the line: turn round.
The pink dot is at the front, so it finds a pad first. We ask about the pink dot, not the whole robot.
if <color [#FF00D4] is touching [#3B82F6]?> then
wait (1) seconds
repeat until <not <color [#FF00D4] is touching [#3B82F6]?>>
move (speed) steps
end
end
if <color [#FF00D4] is touching [#FACC15]?> then
turn cw (180) degrees
end
How to use this: Click the green flag to start it. Nothing else to press — the robot drives itself back and forth.
5 · A lap timer
Scratch has a clock that is always running: (timer). It counts seconds. reset timer puts it back to 0.
- Make a variable called
lap time. - Add a second script to the robot, with the blocks below.
- Tick
(timer)in Sensing to watch it count.
when flag clicked
reset timer
forever
wait until <touching color [#FFFFFF]?>
set [lap time v] to (timer)
reset timer
wait until <not <touching color [#FFFFFF]?>>
end
How to use this: Click the green flag to start it. Nothing else to press — watch lap time fill in each time the robot crosses the chequered strip.
Basic — tune, stop, time 24 min
Open your follower from last lesson.
Step 1 — two variables
- Make
speedandturn. - Put them in the move block and both turn blocks.
- Show them as sliders, from 1 to 30.
Step 2 — tune it
Drag the sliders while it runs. Find a speed that is fast and still safe.
Step 3 — a finish strip
Paint a short chequered strip of white and grey across your line. Move the robot’s start just past it.
Step 4 — the lap timer
Build the lap timer script. Check lap time changes once per lap.
Step 5 — a stop line
- Paint a short blue stripe across a straight part of your line.
- Add the blue stop if at the bottom of the loop.
- Pick the pink from your dot, and the blue from your stripe.
when flag clicked
go to x: (-17) y: (118)
point in direction (90)
forever
if <color [#FF00D4] is touching [#23262D]?> then
turn ccw (turn) degrees
else
if <color [#00E0FF] is touching [#23262D]?> then
turn cw (turn) degrees
else
move (speed) steps
end
end
if <color [#FF00D4] is touching [#3B82F6]?> then
wait (1) seconds
repeat until <not <color [#FF00D4] is touching [#3B82F6]?>>
move (speed) steps
end
end
end
My sliders go back to the same number every time I press the flag.
There is a set [speed v] to (3) under the flag. Take it out. The slider keeps its number between runs.
The robot stops at the blue line and never goes again.
The repeat until is missing, or it asks about a different colour. After the wait, it must drive until the pink dot is off the blue.
Lap time is always 0.
The robot starts on the finish strip, so the first crossing happens at once. Start it further along.
The fastest safe settings
Find the speed and turn that give the best lap time on a wavy track.
- The robot must finish three laps in a row without getting stuck or flying off.
- Write down your speed, your turn and your best lap time.
- Then try to beat your own time by changing one number.
How to use this: Click the green flag to start it. Nothing else to press — watch lap time.
Teacher note
Pupils usually push speed first and forget turn. A slightly bigger turn lets a faster robot get round bends it would otherwise fly off. Ask them to change one number at a time — that habit is the real lesson.
Four trips, then park
A shuttle robot carries things back and forth. Make it count its trips.
- Paint a line with two ends and a yellow pad on each end.
- Each turn at a yellow pad adds one to
trips. - After four trips, the robot stops and says “Four trips. Done!”.
How to use this: Click the green flag to start it. Nothing else to press — watch the trips box count to 4.
Teacher reveal
when flag clicked
go to x: (-114) y: (-20)
point in direction (55)
set [trips v] to (0)
repeat until <(trips) = (4)>
if <color [#FF00D4] is touching [#23262D]?> then
turn ccw (turn) degrees
else
if <color [#00E0FF] is touching [#23262D]?> then
turn cw (turn) degrees
else
move (speed) steps
end
end
if <color [#FF00D4] is touching [#3B82F6]?> then
wait (1) seconds
repeat until <not <color [#FF00D4] is touching [#3B82F6]?>>
move (speed) steps
end
end
if <color [#FF00D4] is touching [#FACC15]?> then
turn cw (180) degrees
change [trips v] by (1)
end
end
say [Four trips. Done!]
The forever becomes repeat until <(trips) = (4)>, as the route did last lesson. The count goes where the turn happens. The demo runs at speed 6.
A three-lap race
Turn the lap timer into a race.
- A variable
lapscounts each crossing of the finish strip. - After three laps, the robot stops.
- It says the total time, like “3 laps in 17.9”.
How to use this: Click the green flag to start it. Nothing else to press — watch it through, about 20 seconds.
Teacher reveal — no reset this time
when flag clicked
set [laps v] to (0)
reset timer
repeat until <(laps) = (3)>
wait until <touching color [#FFFFFF]?>
change [laps v] by (1)
wait until <not <touching color [#FFFFFF]?>>
end
say (join [3 laps in ] (timer))
stop [other scripts in sprite v]
The timer is reset once, at the start, so at the end it holds the whole race. stop [other scripts in sprite v] stops the follower, so the robot stands still with its bubble.
Careful on the bends
Speed 30 flies off the Wave. Make a robot that goes fast on straights and slow on bends.
- Every time the robot turns, it remembers: “I am on a bend”.
- For the next ten moves, it only moves 3 steps.
- After that, it moves at full speed again.
How to use this: Click the green flag to start it. Nothing else to press — watch the bend box jump to 10 on every turn.
Teacher reveal
when flag clicked
go to x: (-46) y: (97)
point in direction (132)
set [bend v] to (10)
forever
if <color [#FF00D4] is touching [#23262D]?> then
turn ccw (turn) degrees
set [bend v] to (10)
else
if <color [#00E0FF] is touching [#23262D]?> then
turn cw (turn) degrees
set [bend v] to (10)
else
if <(bend) > (0)> then
move (3) steps
change [bend v] by (-1)
else
move (speed) steps
end
end
end
end
The set [bend v] to (10) under the flag matters: without it, the robot starts at full speed and flies off the first bend before it has turned once.
Summary 5 min
- A number in a script can become a variable, and a variable can be a slider.
- Tuning: change one number, watch, change it again.
- Too much speed flies off. Too much turn wobbles and sticks.
- Marker pads give orders. The front sensor finds them first.
(timer)counts seconds by itself.reset timerstarts it again.
- Slider
- A variable box on the Stage you can drag to change its number.
- Tuning
- Changing numbers, one at a time, until something works well.
- Marker
- A mark on the floor that tells a robot to do something.
- Timer
- Scratch’s own clock. It counts seconds from the last reset.
Hand in
Save your tuned robot and write your best lap time in the project notes. Next lesson: three tracks, a menu, and a best lap for each.