Every sketch has two parts: setup() runs once when the board starts, then loop() runs again and again for ever. delay(ms) waits that many milliseconds, so the delays set the timing of a blink.
MakeCode had 'on start' and 'forever'. An Arduino sketch has the same two parts, written as C++. Which part runs when, and how many times?
MakeCode
Arduino C++
on start
void setup() { … }
forever
void loop() { … }
pause (ms)
delay(ms);
The lines inside { } belong to that part, and each statement ends with ;.
Predict: Read the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
Try: Try it on the board and compare the result with your prediction.
Investigate: Work out why it happens, one step at a time.
Modify: Change the problem on purpose and see what happens.
Practise: A few on your own, to check you have it.
Make: Make your own problem using what you have learned. Expect a few tries; that is normal.
WidthMore contexts, same idea.
Make your prediction first. Help opens once you've locked it in.
PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
How many times?
TryTry it on the board and compare the result with your prediction.
Try: watch the counters
Try it yourselfYour edits are saved in this browser.
Upload and run the blink. Watch the counters above the board for a few seconds.
Use the board. This ticks when you get there.
InvestigateWork out why it happens, one step at a time.
Worked example: time it
Worked exampleBefore each step, say what you think comes next.
setup(): pinMode(13, OUTPUT)Runs once, at 0 ms. Pin 13 is now an output.
loop(): digitalWrite(13, HIGH)LED on. Writing takes (almost) no time.
delay(500)Wait 500 ms with the LED on.
digitalWrite(13, LOW); delay(500)LED off for 500 ms.
end of loop()→start of loop()1000 ms per blink, for ever.
Fill in the board time after each line of loop(), starting at 0 ms.
line
LED
time (ms)
digitalWrite(13, HIGH);
on
0
delay(500);
on
digitalWrite(13, LOW);
off
delay(500);
off
ModifyChange the problem on purpose and see what happens.
Vary: change the timing
Try it yourselfYour edits are saved in this browser.
Make it blink twice as fast.
Use the board. This ticks when you get there.
Now a flash: on for 100 ms, off for 900 ms.
Use the board. This ticks when you get there.
Modify TypicalChange the problem on purpose and see what happens.
Build it in Tinkercad
PractiseA few on your own, to check you have it.
Timing
loop() is: HIGH, delay(300), LOW, delay(700). How many blinks in 10 seconds?
MakeMake your own problem using what you have learned. Expect a few tries; that is normal.
A lighthouse
Try it yourselfYour edits are saved in this browser.
Need a hint?
The light is on for 200 ms in every 2 seconds.
Another hint
2 s = 2000 ms: off for 2000 − 200 ms.
Another hint
delay(200) after HIGH and delay(1800) after LOW.
A lighthouse flashes for 0.2 s every 2 s. Change the delays to match.
Use the board. This ticks when you get there.
For teachers
Students watch setup() run once and loop() repeat, trace board time through a blink, see why blink code in setup() blinks only once, and change delays to set a blink's timing, including a lighthouse from its timing. About 40 minutes.