The Arduino Inventor's Guide

The Arduino Inventor's Guide by Unknown Read Free Book Online

Book: The Arduino Inventor's Guide by Unknown Read Free Book Online
Authors: Unknown
considered
general-purpose input/output (GPIO) pins
. They can be used as either inputs or outputs, and pinMode() allows you to tell the Arduino how you plan to use a digital pin. You do this by passing two parameters. The first is the pin number, and it can range from 0 to 13. The second parameter is the pin configuration.
    For the pin reference, the Blink sketch uses a system constant called LED_BUILTIN to specify that you’re using the default LED on the device. On most Arduino devices, this is the same as pin 13. Notice that the value is in all caps and colored dark teal. This color indicates that LED_BUILTIN is a special keyword with a predefined value used in the IDE.
    The second parameter defines the pin configuration as an OUTPUT . Notice that the keyword OUTPUT is also dark teal because it is another constant used in Arduino. There are a few other choices here, which we’ll cover in detail in Projects 4 and 9 , but for now just note that Blink sets the pin as an OUTPUT for the LED.
    If you were to describe this line of code as a sentence, it would say, “Tell pin 13 to output from the Arduino.”
    NOTE
    The
pinMode()
function follows a capitalization convention called
camel case.
In camel case, the first letter is lowercase, and any later letters that start words are capitalized.
    The very last character in the pinMode() call is a semicolon ( ; ), which marks the end of a line of code. When you start writing your own sketches, always end a finished line of code with a semicolon. If you forget one, don’t worry; nearly everyone who’s ever programmed forgets a semicolon eventually, so the Arduino IDE will show a handy warning to help you figure out where to put the missing punctuation.
    WHERE’S THE MAIN() FUNCTION?
    If you know a bit of programming or are familiar with C or C++, you might wonder where the main() function is in Arduino sketches. When you click Verify/Compile or Upload, Arduino actually pulls together a lot of other files behind the scenes—including a file called
main.cpp
. Dig around the Arduino program folder, and you can find all the nitty-gritty details of what’s going on. Remember, it’s open source!
    Here’s a snippet of code from the
main.cpp
file:
      int main(void)
  {
    init();
    initVariant();
  #if defined(USBCON)
    USBDevice.attach();
  #endif
➊    setup();
    for (;;)
    {
➋    loop();
    if (serialEventRun) serialEventRun();
    }
    return 0;
  }
    See where the setup() function is called at ➊ ? And notice that the loop() function ➋ is inside a forever loop; Arduino implements a forever loop using an empty for(;;) . That’s how it runs continuously.
The loop() Function
    Now let’s look again at the loop() function, which executes each instruction from top to bottom and repeats itself forever. See Listing 1-3 .
    LISTING 1-3: The loop() code for the Blink sketch
    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
}
    The digitalWrite() function allows you to turn the Arduino pins on or off; this is called controlling a pin’s
state
. This function also uses two parameters. The first indicates the pin you want to control; in this case, we’re using the system constant LED_BUILTIN again. The second parameter is the state you want the pin to be in. To turn the light on, the Blink sketch passes in HIGH . To turn the light off, it passes in LOW .
    The second instruction is delay() , which delays your sketch by the number of milliseconds you pass as its parameter.

Similar Books

Private Melody

Altonya Washington

Home by Another Way

Robert Benson

The Big Finish

James W. Hall

Lead Me Not

A. Meredith Walters

Musings From A Demented Mind

Derek Ailes, James Coon

Birthnight

Michelle Sagara

A Feral Darkness

Doranna Durgin