← Teacher guide

Reading the C++

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.

Arduino · A new board · about 40 minutes · Open the student lesson

Classroom resources

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.

Students will be able to

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

  1. Predict: link to MakeCode's on start.
  2. Worked example: ask what the LED is doing at 750 ms.
  3. 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 seeA question to askThe 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

  1. 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.
  2. 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.
  3. 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.
  4. 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().
  5. Twice as fast: Make it blink twice as fast.

    Halve both delays: 250 ms on, 250 ms off.
  6. 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.
  7. 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.
  8. Where it goes: Where does pinMode(13, OUTPUT); belong?
    In setup(): it only needs doing once
    Set up once in setup(); repeat in loop().
  9. 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

Where it leads

Next: Values and decisions (variables, analogRead and if).

Sources