Arduino programming
Arduino programming
Blink a LED, the Arduino 'hello world'.
4. Deconstructing Blink
4.4. Inside loop()
First, let's take a look at what statements are inside the loop():
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
This is essentially the part of the program that describes blinking. We see that blinking is actually turning a LED on, waiting for a while, turning it off, waiting for a while again and then repeating from the top.
Let's take the statements one by one:
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
In an Arduino sketch, write means to set a value. digitalWrite() will set a value of HIGH or LOW. For the digital output pins of the Arduino, HIGH and LOW means turning them ON and OFF.
Notice that the way we use digitalWrite() looks similar to pinMode() that we saw earlier.
delay(1000); // wait for a second
The delay() will pause the program, preventing the Arduino from executing the next command. The number in the parentheses is the amount of time it will wait in milliseconds.
1000 msec = 1 sec
The next statement is very similar to the first digitalWrite() in the program, however this time it sets the pin to LOW, turning the LED OFF.