Inside the loop()

We're on a roll understanding statements and comments so lets go to to the next line - the first statement inside of loop(). You guessed it, its time for a quick quiz!

Quick Quiz #4!

What is this line of code:

digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)

(A) A statement
(B) A single line comment
(C) Part of a function definition
(D) Uh...both?

Answer: D! Yes it is a line that has both a statement and a comment (Click to reveal the answer!)

Really threw you for a loop, didn't it? (Ha, that was my little function name joke) But now you know, you can combine statments and comments. The comment is only the part after the //. The part before the // is the statement. As you can tell, if you have a really short comment, it can be convenient to double them up for compactness and clarity.

Another Statement!

If we seperate it out, the statement looks like this digitalWrite(13, HIGH); Which looks a lot like...A function call statement? (That's right!)

We've already been introduced to pinMode so now you will meet her friend, digitalWrite.

  • pinMode is what you request of the Arduino when you want to set the mode of pins.
  • digitalWrite is what you can request of the Arduino when you want to change the setting of the pins.

To think of it in terms of that front door we spoke of earlier, pinMode is like asking the Arduino to lock or unlock the door and digitalWrite is like asking it top open or close the door. You have to unlock the door before you open or close it!

digitalWrite

The nice thing about good comments is they really help you understand what the statement does.

For example, the comment next to this statement

digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)

Tells you what the code actually does: it turns on the LED so it's lit up and shining. Likewise, if you skip down two more statments, you'll see

  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW

Which, apparently, turns it off.

Notice that the second input to the digitalWrite function has changed from HIGH to LOW but that the first input, 13, is the same!

These two statements act like someone flipping a switch on and off. On (HIGH), off (LOW). Simple!

delay

In between the digitalWrite calls are two other lines of code. To our luck, they are identical:

delay(1000);              // wait for a second

No need for a quiz: you are an expert at this now! You are looking at a statement with a short one line comment. Moreover, the statement is a function call.

delay is the third function you are meeting today. delay is what you can request of the arduino when you want to hold on for a moment and do nothing.

Which might seem a little odd - until you realize that many of the function calls that you can request complete in thousandth's of a second! In order to really see the LED on and off, the Arduino needs to pause:

  • Turn on the light
  • Wait a second
  • Turn off the light
  • Wait a second
  • (and repeat!)

In particular, delay requires only one input - the amount of time to wait. That time is a number of milliseconds. Which are thousandths of a second (not whole seconds). So delay(1000); is not 1000 seconds, minutes or hours - it is 1000 * thousandths, a.k.a. one second.

Time for a Break!

You just did some serious reading, learning about comments and statements and you might be getting a little antsy. Now is a good time for a break where you will start putting what you learned into practice. Let's go to the next section where we will practice modifying a sketch.

This guide was first published on Sep 02, 2016. It was last updated on Sep 02, 2016. This page (Inside the loop()) was last updated on Apr 08, 2020.