Выбрать главу

You should now be ready to upload the sketch to the Arduino, so press the Upload button. Messages should appear in Log area, and then the TX and RX LEDs on the Arduino should flicker as the program is uploaded onto the board.

When the upload is complete, you should see a message like the one shown in Figure C-4.

Figure C-4: A successful upload

The Done uploading message tells you that the sketch has uploaded, and the last line in the console tells you that you’ve used 1,084 bytes of the 32,256 bytes available on your Arduino.

Once the sketch is uploaded, the built-in L LED on the Arduino should blink slowly on and off, which is just what the Blink program is expected to do.

INSTALLING THE ANTIZOMBIE SKETCHES

All the sketches for the book are available via the book’s website (http://www.nostarch.com/zombies/). Click on the Download Code link to download a ZIP file called zombies-master.zip. Make sure to do this before the apocalypse begins, because your broadband is likely to be a low priority once the infection has begun to spread. This folder will contain all the Arduino and Raspberry Pi programs for the projects in this book.

Install the Arduino sketches so that you can use them directly from your Arduino IDE by copying the subfolders from the Arduino folder into Documents/Arduino folder for Mac and Linux users and My Documents\Arduino for Windows users. Exit and reopen the Arduino IDE. Now when you view FileSketchbook, you should find all the book’s sketches listed.

ARDUINO PROGRAMMING BASICS

This section contains an overview of the main Arduino programming commands to help you understand the sketches used to do with zombies. If you’re interested in learning the Arduino C programming language, consider getting a copy of my book Programming Arduino: Getting Started with Sketches (Tab Books, 2012). The technical reviewer for the book you’re reading now (Jeremy Blum) has also written a very good book on Arduino and has produced a superb series of video tutorials. You can find links to all this from his website (http://www.jeremyblum.com/).

STRUCTURE OF AN ARDUINO SKETCH

All Arduino sketches must have two basic functions (units of program code that perform a task): setup and loop. To see how they work, let’s dissect the Blink example that we looked at earlier.

int led = 13;

// the setup routine runs once when you press reset

void setup() {

  // initialize the digital pin as an output

  pinMode(led, OUTPUT);

}

// the loop routine runs over and over again forever

void loop() {

  digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)

  delay(1000);             // wait for a second

  digitalWrite(led, LOW);  // turn the LED off by making the voltage LOW

  delay(1000);             // wait for a second

}

Your Blink sketch might be slightly different if you have a newer version of the Arduino IDE, so for the purposes of this discussion, refer to the sketch printed here rather than the one loaded in your IDE.

The text preceded by a double slash (//) is called a comment. It’s not executable program code but rather a description of what’s happening at that point in the sketch.

Just after the words setup() and loop(), we have a { symbol. (Sometimes this is put on the same line as the preceding word and sometimes on the next line. Where it goes is just a matter of personal preference and has no effect on the running of the code.) The { symbol marks the start of a block of code, which ends with a corresponding } symbol. You’ll use curly brackets to group together all lines of code that belong to a particular function or other control structure.

The lines of code inside the setup function run just once, when power is applied to the Arduino or the Reset button is pressed. You use setup to perform all the tasks that need doing just once when the program starts. In Blink, the code inside the setup function just sets the LED pin as an output.

The commands inside the loop function will be run over and over again; in other words, when the last line inside loop has run, the first line will start again.

Now, let’s parse this sketch, starting from the top line.

CREATING VARIABLES AND CONSTANTS

Variables are a way of giving names to values; for example, the first line of Blink labels pin 13 led:

int led = 13;

This defines an int variable called led and gives it an initial value of 13, because 13 is the number of the Arduino pin that the L LED is connected to. The word int is short for integer and means that this variable returns a whole number without decimals.

In some of the book’s other sketches, variables like this, that define a specific pin to be used, are preceded by a const keyword:

const int led = 13;

The const keyword tells the Arduino IDE that the value of led is never going to change from 13, making it a constant. Assigning values this way results in slightly smaller and quicker sketches and is generally considered a good habit.

CONFIGURING DIGITAL OUTPUTS

The Blink sketch also shows a good example of a setting a pin up to be a digital output. Pin 13, having been defined as led, is configured as an output in the setup function by this line:

 pinMode(led, OUTPUT);

As this only needs to be done once, it is placed inside the setup function. Once the pin is set as an output, it will stay an output until we tell it to be something else.

For it to blink, the LED needs to turn on and off repeatedly, so the code for this goes inside loop:

digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000);             // wait for a second

digitalWrite(led, LOW);  // turn the LED off by making the voltage LOW

delay(1000);             // wait for a second

The command digitalWrite takes two parameters (pieces of data that the function needs to run), which are passed to the function inside parentheses and separated by a comma. The first parameter defines which Arduino pin to write to (in this case, pin 13, as specified by led), and the second parameter gives the value to be written to the pin. A value of HIGH sets the output to 5V, turning the LED on, and a value of LOW sets the pin to 0V, turning the LED off.

The delay function holds the parameter that defines how long the Arduino should continue with its current function. In this case, a value of 1000 delays the program for one second before changing the state of the LED.

CONFIGURING DIGITAL INPUTS

Digital pins can also be set as input pins using the pinMode command. The Blink sketch doesn’t do this, so here’s an example:

pinMode(7, INPUT)

This pinMode function sets pin 7 as an input. Just as with an output, you’ll rarely need to change the mode of a pin, so define input pins in the setup function.