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 ;.
How this lesson works: Predict, Try, Investigate, Modify, Practise, Make
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.
Extra examplesYour teacher may tell you which.
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.
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.