This code is kind of like the "Hello, World!" for an Arduino board - "Blink".
This code will make the built in LED which is usually connected to pin #13 turn on for one second, then turn off for a second. It loops infinitely.
"Blink" can also be found in the example codes in the IDE.
The code contains quite a bit of commenting:
/*
Blink
Turns an LED on for one second, then off for one second, repeatedly.
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your Arduino
model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products
modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
modified 8 Sep 2016
by Colby Newman
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void 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
}
You may have noticed that while a double slash (//) comments out only one line, whereas (/*) will comment out everything until you enter (*/). This is very useful for commenting large blocks of code and/or paragraphs of text.
This code is a good example of controlling pins, but it could be better. There is a second blink sketch which is better, but we will look at it later.
I prefer to write it like this:
const int ledPin = LED_BUILTIN;// the number of the LED pin
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(ledPin, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
This makes it much quicker and easier to change which pin number you want to connect an LED to.
However, we will break down the original "Blink" sketch to begin with, in order to understand some more very basic principals.
This is the setup function which prepares the LED for later commands:
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
LED_BUILTIN is the on board LED that is usually connected to pin #13. This name (LED_BUILTIN) is a default name, and is buried deep inside the default Arduino libraries. This isn't really able to be edited.
pinMode(LED_BUILTIN, OUTPUT);
This line is setting the pinMode of a specific pin. In this case, we are choosing the BUILTIN LED pin, which is pin #13, and setting it to an OUTPUT. pinMode can only be INPUT or OUTPUT, and the pin we are setting is either LED_BUILTIN, or a number between 0 and 13. Usually, pins 0 and 1 aren't used as these are serial pins and are usually reserved for communicating with the computer that you are programming with.
This is the loop function which enters an infinite loop as soon as the setup is finished.
void 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 the section of the code that is much more user friendly" and able to be edited to do different things.
digitalWrite(LED_BUILTIN, HIGH);
This line writes binary to the selected pin, either a 1 (HIGH), or a 0 (LOW).
digitalWrite is only capable of writing a binary 0 or 1. The other command which is available is analogWrite. This command is only able to be used on pins that have the ~ symbol next to them. See the PWM post by Max.
delay(1000);
This line freezes all progress in the loop for exactly 1000 milliseconds. This is known as a line of blocking code. In a future post, we will explore the differences between blocking and non-blocking code, and learn how to remove all delays.
the number inside the brackets is able to be changed to any number you want, however you cannot use commas in the brackets. So one million (1,000,000) milliseconds would be written as (1000000).
You may have noticed that both the setup and loop have these brackets around what is inside them {}. This is what contains the code, as anything outside of these are either an error or a global variable.
You may also have seen the semicolon at the end of every line of code. This is used to end a statement. Forgetting to end a line in a semicolon will result in a compiler error.
Comments