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.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
SlidesDownload Present from the browser, or export to PowerPoint or PDF.
WorksheetDownload Print the student copy; the Teacher edition has the answers.
Students will be able to
I can say what setup() and loop() each do.
I can work out a blink's timing from its delays.
I can choose delays for a timing.
Timing
Predict and Try 5 min · Worked example 10 min · Vary 6 min · Tinkercad 10 min · Practise and Make 9 min
Before class
The board counts setup() and loop() runs and uses simulated time, so delays are exact (o and f in the checks are the last on and off times in ms).
MakeCode's on start and forever are the same two parts; say so.
Running the lesson
Predict: link to MakeCode's on start.
Worked example: ask what the LED is doing at 750 ms.
Vary: ask which delay is the on time.
The hard part (on purpose)
Students forget that writing a pin takes no time; only delay does. Trace it with a stopwatch in hand.
When students get stuck
What you will see
A question to ask
The fix
A student changes only one delay for 'twice as fast'.
How long is one blink now?
Both delays set the length of a blink.
Answers to the checks
how many times?: The blink sketch runs for 10 seconds. How many times does setup() run? Once, when the board starts. Then loop() runs again and again: 10 times in 10 seconds here, as each loop takes 1 s.
Try: watch the counters: Upload and run the blink. Watch the counters above the board for a few seconds.
setup() ran once; loop() runs once a second (500 ms on, 500 ms off) and keeps going.
Board time: Fill in the board time after each line of loop(), starting at 0 ms. 500, 500, 1000 Only delay takes time: the LED is on from 0 to 500 ms and off from 500 to 1000 ms, then loop() starts again.
Sam's blink: Sam put the blink lines in setup() and left loop() empty (below). What does the LED do? Blinks once, then stays off setup() runs once, so the LED blinks once. Anything that should repeat goes in loop().
Twice as fast: Make it blink twice as fast.
Halve both delays: 250 ms on, 250 ms off.
A flash: Now a flash: on for 100 ms, off for 900 ms.
The first delay is the on time and the second the off time; the blink still takes 1 s.
Count the blinks: loop() is: HIGH, delay(300), LOW, delay(700). How many blinks in 10 seconds? 10 Each loop takes 1000 ms, so 10 blinks in 10 s.
Where it goes: Where does pinMode(13, OUTPUT); belong? In setup(): it only needs doing once Set up once in setup(); repeat in loop().
a lighthouse: A lighthouse flashes for 0.2 s every 2 s. Change the delays to match.
200 ms on and 1800 ms off makes one flash every 2000 ms.
Solutions
One possible solution for each task. Other correct programs are fine.
Vary: change the timing › Twice as fast
Open task: any program that does what the step asks.
Vary: change the timing › A flash
Open task: any program that does what the step asks.
a lighthouse
Open task: any program that does what the step asks.
Stretch and support
Stretch: Make a heartbeat: two quick flashes, then a pause. (Four delays in loop().)
Support: A timeline strip from 0 to 2000 ms to shade on and off.
Exit ticket
What is the difference between setup() and loop()?
setup() runs once when the board starts; loop() runs again and again after it.
What each step is for
how many times?: Committing to how often setup() runs tests the new move before the counters show it.
Try: watch the counters: The counters settle the prediction: setup() stays at 1 while loop() climbs, one per second.
Worked example: time it: Tracing board time line by line models how delay sets timing; Sam's blink in setup() is planted after the method.
Vary: change the timing: Changing one delay at a time shows each delay sets one part of the blink.
Build it in Tinkercad: In Tinkercad's Text view students edit the C++ directly for the first time and check the timing on a simulated board.
timing: Reading timings from delays, and where code belongs.
a lighthouse: Working from a timing back to the delays reverses the process.
Where it leads
Next: Values and decisions (variables, analogRead and if).