The Arduino Inventor's Guide

Read The Arduino Inventor's Guide for Free Online Page B

Book: Read The Arduino Inventor's Guide for Free Online
Authors: Unknown


     modified 8 Sep 2016
     by Colby Newman
   */

   //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
                                        //(voltage level is HIGH)
       delay (1000);                      //wait for a second
       digitalWrite ( LED_BUILTIN , LOW );   //turn the LED off
                                        //(voltage level is LOW)
       delay (1000);                      //wait for a second
  }
    When writing sketches in Arduino, you need to be very specific with the words, punctuation, and capitalization you use. These elements are part of a programming language’s
syntax
. For the IDE tocompile your sketch properly, you must use words that it recognizes. These are called
keywords
, and you’ll notice them when they change to a different color, such as orange, teal, or green. Now, let’s look at some of the features used in this first sketch in detail.
Key Sketch Elements
    At the top of the sketch, you declare a new
global namespace
➊ . This is a space that describes what the sketch does and often includes other information such as variable initializations and library statements. Nearly every sketch will include a namespace. This sketch’s namespace has comments written to help human readers understand what the sketch does. In the Arduino IDE, comments are gray. Every comment either starts with the characters // or is bounded by the symbols /* and */ if the comment is longer than a few lines. Notice that not all comments come between lines of code; some appear on the same line as the code they clarify. This doesn’t affect the sketch, because comments are ignored by the IDE. Unlike with code, you can write anything you want in the comments using regular words, spelling, or punctuation.
    The skeleton of any sketch consists of two main function definitions, setup() ➋ and loop() ➌ . A
function
is simply a way of grouping multiple instructions or lines of code together. Each function has a data type, a name, and a group of instructions. The word before the function indicates the type of data the function will return. Both setup() and loop() have the type void because they do not return any values.
    The name of every function includes a set of parentheses. These parentheses are where you pass
parameters
to the function. Parameters are values that a function needs to do its job. Neither setup() nor loop() needs parameters, but you’ll use some functions in later projects that do need them. Finally, the lines of code that make up the function are grouped by an opening curly bracket, { , and closing curly bracket, } .
    The setup() and loop() functions are required for every Arduino sketch; when the Arduino is turned on for the first time or is reset, the setup() code runs once and only once, and loop() code repeats continuously over and over. It’s like baking cookies: instructions in setup() get out all of your tools and ingredients, and loop() bakes batches over and over until you turn off the oven (that is, the Arduino).
    Now, let’s figure out what each line of code in setup() and loop() is actually doing.
The setup() Function
    First, let’s take a closer look at the Blink sketch’s setup() function; see Listing 1-2 .
    LISTING 1-2: The setup() code for our Blink example
    void setup () {
   //initialize digital pin LED_BUILTIN as an output
   pinMode ( LED_BUILTIN , OUTPUT );
}
    The only line of code inside the setup() function is a call to the pinMode() function. Pins 0–13 on the Arduino are

Similar Books

The Pirate's Witch

Candace Smith

The Luminist

David Rocklin

Timeless Love

Karyn Gerrard

Nothing But Trouble

Trish Jensen