← Arduino

Reading the C++

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 ;.

WidthMore contexts, same idea.
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?

The blink sketch runs for 10 seconds. How many times does setup() run?

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.
  1. setup(): pinMode(13, OUTPUT)\text{setup(): pinMode(13, OUTPUT)}Runs once, at 0 ms. Pin 13 is now an output.

Now fill in the times.

void setup() {
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
}

void loop() {
}

Fill in the board time after each line of loop(), starting at 0 ms.

lineLEDtime (ms)
digitalWrite(13, HIGH);on0
delay(500);on
digitalWrite(13, LOW);off
delay(500);off

Sam put the blink lines in setup() and left loop() empty (below). What does the LED do?

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?

Where does pinMode(13, OUTPUT); belong?

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.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

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

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.

Where it leads

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

Sources

What each step is for

Now you can
Next lesson3. Values and decisionsA variable is declared with its type, int level = 0;. analogRead(A0) gives 0 to 1023, and if (level > 500) { … } else { … } decides. == compares; a single = sets the variable, even inside an if. Every statement ends with ;, or the sketch won't upload.