- Find this text in the Blink code: void loop( ).
- Notice how the lines of code that are highlighted in the reference video correlate with changes in the LED.
- digitalWrite(LED_BUILTIN, HIGH); causes the Arduino to give the built-in LED, pin 13, high voltage, causing the LED to turn on.
- delay(1000); causes the Arduino to pause for 1000 milliseconds, 1 second, before reading the next line of code. Meanwhile, the LED stays on since that was the last instruction it received.
- digitalWrite(LED_BUILTIN, LOW); causes the Arduino to give the built-in LED, pin 13, low voltage, causing the LED to turn off.
- delay(1000); causes the Arduino to pause for 1000 milliseconds, 1 second, before reading the next line of code. Meanwhile, the LED stays off since that was the last instruction it received.
- Because these lines of code are written within the void loop, they repeat forever (until the power is disconnected or the Arduino board is reset) and causes the LED to blink on and off at a rate of 1 second.
- digitalWrite(LED_BUILTIN, HIGH); causes the Arduino to give the built-in LED, pin 13, high voltage, causing the LED to turn on.
At the Thinkabit Lab, we point out there are 4 lines of code in the loop and call attention to them for students to visualize what a “line of code” is by doing the following:
- Reiterate that the loop runs these 4 lines over and over and say it out loud, “1, 2, 3, 4”, and when it reaches the end go back to the top of the loop to “1, 2, 3, 4…”
- Explain that every line of code in the loop is responsible for some action. When programming, every step must be thought out and coded for. Even waiting before doing the next task is an action that needs to be coded for.
- One way to exemplify that each line is responsible for an action is to tell the class that you are going to pretend to be an Arduino and the class, one table at a time, will read 1 line of code within the curly brackets of the loop to you. To make it more fun, you can physically walk to each table and ask them to read a line. Explain to them that they need to read only the actual line of code, not the comments. Start at any table and ask them to read one line of code. Remember, you are the Arduino and you will react to each line.
- Students say: “digitalWrite(LED_BUILTIN, HIGH);” or “digitalWrite(13, HIGH);”
- Teacher: Say “This line means pin 13 will be high voltage which means the LED will turn on.”
Put your hands up to indicate the LED is on, then go to the next table.
- Students say: “delay(1000);”
- Teacher: Says “This line means I, the Arduino, will not read another line of code for 1000 milliseconds.”
Teacher asks: “how many seconds is 1000 millisecond?” - Students should respond: one second
- Teacher: Say "for one second, I can’t read another line, but I was never told to turn off.”
Wait for one second, then go to the next table.
- Students say: “digitalWrite(LED_BUILTIN, LOW);” or “digitalWrite(13, LOW);”
- Teacher: Say “This line means pin 13 will be low voltage which means the LED will turn off.”
Put your hands down to indicate the LED is off, then go to the next table.
- Students say: “delay(1000);
- Teacher: Say “This line means I, the Arduino, will not read another line of code for 1000 milliseconds.”
Wait for one second, then go to the next table. Repeat as desired, starting from line one.
- Another way to demonstrate to students that each line is responsible for an action is to use Simon says as an analogy. Ask for a student volunteer or have all students play along. Explain to students that they should think of the Arduino board as the “Simon”.
- When it runs “digitalWrite(LED_BUILTIN, HIGH);” that is like saying, “Simon says turn on.”
- When it runs “delay(1000);” that is like Simon waiting one second to give the next command.
- When it runs “digitalWrite(LED_BUILTIN, LOW);” that is like saying, “Simon says turn off.”
- And then another “delay(1000);” is like Simon waiting for one second again to give the next command.
If you are using the Thinkabit Lab Notebook:
Have students fill in their own comments on page 14 of their notebooks:
digitalWrite(LED_BUILTIN, HIGH); LED on
delay(1000); wait 1 second
digitalWrite(LED_BUILTIN, LOW); LED off
delay(1000); wait 1 second
The functions “digitalWrite(LED_BUILTIN, HIGH)” and “digitalWrite(LED_BUILTIN, LOW)” do not appear
-
In some versions of the Arduino software, pin 13 is turned on by using the code “pinMode(13, HIGH);” and pin 13 is turned off using the code “digitalWrite(13, LOW);”.