As with spoken languages, there are many varieties of programming languages. Some of the most common languages include various dialects of Basic, C, C++,
15
C#, Objective-C, Java, and Python, among others. At the lowest level, assembly programmers write code that is only one level up from the sets of instructions that are executed by a computer or microcontroller. On the other end of the programming spectrum, interpreted languages like Basic can, to an extent, shield a programmer from some low-level details such as memory management and pointers. Somewhere in the middle (or perhaps closer to the assembly level) is C, the language that is used to program an Arduino. (If you already have a background in programming you may notice some differences between stand- ard C and what might be called “Arduino C.” However, for the purposes of this book the differences are largely inconsequential.)
There are many benefits to learning the C language: The language is “lean and mean” in that a relatively small number of native commands and data types can form the basis of programs that are incredibly powerful. In literary terms, C might be compared to the terse power of an e.e. cummings poem or a Hemingway novel.
Although there are a few aspects of C programming that can be chal- lenging to neophytes, the language is beautiful in its simplicity and power, which is probably one of the reasons that C and C++ seem to be at the core of most college programming curricula. In a word, C is relatively easy to learn, will open the door to a virtually limitless creative potential, and provides a useful founda- tion for learning other programming languages.
Getting Started
A necessary first step is to download and install the Arduino programming en- vironment from www.arduino.cc. Downloads are available for PC, Mac, and Linux. Although the process is similar for each platform, be sure to read and follow the most up-to-date instructions at the Arduino website. Also note that PC users will need to install drivers so that the development computer will be able to communicate with the Arduino. Although a computer is required to pro- gram an Arduino, a computer is not required once you have programmed the unit. Any programs that are uploaded to an Arduino will run autonomously when an appropriate power supply or batteries are connected to the unit.
After downloading and installing the Arduino development environment and drivers, connect your Arduino to the host computer via a USB cable and run the Arduino software. You will be greeted with the screen shown in Figure 2.1. It will be helpful to familiarize yourself with the development environment prior to writing a first program: the leftmost button, the checkmark, is used to verify if a given program (or sketch as they are called in Arduino parlance) is free of errors. Should an error be found, an error message and explanatory text will be provided in the bottom two panels of the development environment as shown in Figure 2.2. Once you have verified that a sketch is free of errors, use the upload button (the right-facing arrow) to upload the sketch to the Arduino.
16
The other three buttons are used to create a new sketch or to save and load sketches.
You will frequently use the rightmost button (an icon of a looking glass) to monitor the serial output of an Arduino during the development process. Given that the Arduino has no visual interface, the Serial Monitor provides a convenient way to communicate with the unit while it is running. Most of the sample pro- grams in this chapter use the Serial Monitor window to provide such feedback.
Writing Your First Sketch
Traditions can be a good thing, and there is no shortage of tradition and con- vention in the field of computer science. When learning a new computer lan- guage or setting up a new development environment, it is customary to begin
Arduino home screen.
FIGure 2.1
Error message in Arduino environment.
17
with what is known as a “Hello, world!” application. The goal is to write a mini- malistic application that outputs the words “Hello, world!”
Enter the text in Listing 2.1 into the main area of the Arduino program- ming environment and save the file with the name hello_world (or something similar). The series of instructions will be compiled into an executable program that will run on the Arduino. At this point, don’t worry about what all of the instructions mean. Simply connect your Arduino via a USB cable, enter the code, and click the verify button to ensure that your program is free of errors. Note that the compiler is not picky about whitespace: in most cases, tabs, spaces, and carriage returns are ignored by the compiler, but the compiler will complain if you enter unexpected characters.
Listing 2.1 “Hello, world!” sketch
void setup() { Serial.begin(9600); Serial.println("Hello, world!"); } void loop() { //Nothing to do here }
Error Messages
One of two things should happen after clicking the verify button: 1. You will see a “Done compiling” message at the bottom of the screen. 2. You will see an error message such as the following:
hello_world.ino: In function 'void setup()':
hello_world:5: error: expected ';' before '}' token
One of the most challenging aspects of C programming is learning to translate cryptic compiler error messages into something meaningful. Don’t despair, a terse error message does provide some useful information: First, it is evident that there is some sort of error in the function named setup (this is the chunk of code between braces after the word setup()). Second, the compiler noticed the error on line five of a sketch titled “hello_world.” Last, the compiler expected a semicolon but found a closing brace. This is enough information to identify that there is a missing semicolon after the closing parenthesis in line four. Although the com- piler complained about line five, the problem was actually caused by an error on the previous line. This is common in C and C++ programming—it is often helpful to look at the line just prior to the line of code indicated by an error message.
Assuming you entered the sketch without error, it would be helpful to pur- posely enter a few errors to get a sense of how syntactic errors translate into compiler messages. For example, omit the closing parenthesis on the third line and note the similar message that is generated:
18
hello_world.ino: In function 'void setup()':
hello_world:3: error: expected ')' before ';' token
With practice and experience you will be able to quickly identify and correct most errors.