This lesson reviews the code used in the example Blink sketch. Additionally, this lesson provides some tips such as making sure that line numbers are displayed in the Arduino IDE.
Now you know how to set up the Arduino board and upload code from the IDE. Right now, you just uploaded the starter Blink code. Now let’s look at that code and break down some of the key parts in order to develop an understanding of what each line does, and what can be changed.
Let’s start by examining the Blink sketch that you just uploaded:
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Code language: JavaScript (javascript)
The code comments (you’ll learn more about this important concept later in the camp) in the void setup() function have some important information about connecting the software and hardware of the Arduino:
Next, let’s examine some of the key code in the void loop() function:
The comments in the code, as well as the explanations above, shed some light on the importance of making sure that your code correlates and matches the physical connections with your board.
When troubleshooting Arduino projects, and you’ll likely have to do some troubleshooting in this camp, you have to check both the software and the hardware. If you build solid habits from the beginning, such as making sure that anything that references specific pins in the board has the correct number, you’ll have a much easier time isolating and fixing issues!
Here is a quick tip to help you when you are using the Arduino IDE. You want to be able to see the line numbers in the Arduino IDE to help track down bugs in error messages. To do so, just follow these steps:
First, launch the Arduino IDE. Once it opens, click on the File menu and select Preferences.
Next, click on the checkbox next to Display line numbers. This will now display line numbers in your code! Here’s a screenshot of where this is inside the Preferences menu:
Click the OK button to save your changes and close the window.
Without this option selected, the code in the IDE looks like this:
With the line numbers displayed, the code in the IDE looks like this:
Turning on the line numbers makes debugging and fixing your code much easier. Additionally, you’ll be able to check to see if you’re missing any code that you import since your line numbers won’t match.