The Code, Step by Step
At first, you might consider this kind of explanation too unnecessary, a bit like when I was in school and I had to study Dante's Divina Commedia (every Italian student has to go through that, as well as another book called I promessi sposi, or The Betrothed—oh, the nightmares). For each line of the poems, there were a hundred lines of commentary! However, the explanation will be much more useful here as you move on to writing your own programs.
// Example 01 :Blinking LED
A comment is a useful way for us to write little notes. The preceding title comment just reminds us that this program, Example 4-1, blinks an LED.
#define LED 13 // LED connected to// digital pin 13
#define is like an automatic search and replace for your code; in this case, it's telling Arduino to write the number 13 every time the word LED appears. The replacement is the first thing done when you click Verify or Upload to I/O Board (you never see the results of the replacement as it's done behind the scenes). We are using this command to specify that the LED we're blinking is connected to the Arduino pin 13.
voidsetup()This line tells Arduino that the next block of code will be called setup().
{ With this opening curly bracket, a block of code
begins.
pinMode(LED, OUTPUT); // sets the digital// pin as output
Finally, a really interesting instruction. pinMode tells Arduino how to configure a certain pin. Digital pins can be used either as INPUT or OUTPUT. In this case, we need an ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access