• No se han encontrado resultados

PROBLEMAS QUE ENFRENTA LA ESPECIE

COOPERACIÓN LOCAL

Correct any syntactical errors and click the verify button to ensure that the pro- gram is free of errors. A final step is to click the upload button to upload the compiled sketch to the Arduino. As with verifying a sketch, you should see a message titled “Done uploading” if the sketch uploaded to the Arduino board without any errors. Alternatively, an error message such as one of the following indicates a problem with the serial port or Arduino board respectively:

Serial port name_of_port already in use. Try quitting any programs that may be using it.

Problem uploading to board. avrdude: stk500_recv(): programmer is not responding

You will need to check that the correct Arduino board and serial port are se- lected if you receive such a message. (Also make sure that you have installed the appropriate serial driver, if necessary, for your platform.) Arduino Uno should be selected in the Tools→Board menu (assuming you are using an Uno). On my system, the serial port shown in Figure 2.3 is used to upload sketches to Arduino. Note that your settings will likely look different.

Once your sketch has been uploaded to Arduino, click the Serial Monitor icon (or click the Tools→Serial Monitor menu) to view the output of the sketch. Assuming the Serial Monitor is set to a baud rate of 9600, you should be greeted with the text “Hello, world!” Although this sketch is mundane, it represents an incredible potential: an autonomous microcontroller that, with appropriate pro- gramming and circuitry, can form the basis of a virtually limitless number of performance controllers, real-time synthesizers, algorithmic composers, and the like. To quote author Chris Anderson: “We are all designers now. It’s time to get

good at it.”1

The remainder of the chapter is devoted to a discussion of the primary commands and syntax associated with the C language. As with learning an in- strument, work through the basics slowly until you master them, and take time

Serial port.

19

to explore and experiment with each concept. This book is all about the creative application of technology, so you will get the most out of the book if you strive to use new concepts in creative ways. Exploration will also help to foster own- ership and retention of the material. To use another musical analogy: there has been some great music written with just three chords—don’t hesitate to get cre- ative after you have learned a few of the “chords” in this chapter.

“Hello, world!” in Detail

Let’s work through the “Hello, world!” sketch line by line. Although the sketch is rudimentary, it will provide a useful vehicle to explore a number of funda- mental programming concepts. The first thing to notice is that the sketch com- prises two functions. We will look at functions in detail later in the chapter, but for now, understand that a function represents a useful block of code. All Arduino sketches have the two functions listed below, and the following boiler- plate will form a foundation for all of your Arduino sketches:

void setup() { } void loop() { }

Figure 2.4 illustrates the name and function of each symbol in the sketch.

Functions

Note how an opening and closing brace forms the body of each function and that the name of the function is followed by parentheses. Although it is common

void setup() { } void loop() { }

function name empty () meansthe function does not take any parameters void keyword

means that the function does not return a value open bracket indicates the start of the

function block closed bracket indicates the end of the function block another function...this one is named loop() end of the loop() function

block Code description.

20

to provide additional information to a function in the form of parameters, num- bers or characters that are sent to the function between the parentheses, empty parentheses indicate that the function does not accept such parameters. Also, some functions perform calculations and return a number, text, or some other value but, in this case the void keyword indicates that a function does not return anything.

There is some behind-the-scenes magic associated the setup() and loop() functions in Arduino. The first is that the setup() function is called as soon as the program starts running. As the name implies, setup() is typically used to initialize variables or other housekeeping chores when a sketch first runs. Where setup() is called once and only once when a program first runs, the loop() function is called repeatedly for as long as the Arduino is running. As is prob- ably obvious, the loop() function is typically used to poll the state of buttons, output Music Instrument Digital Interface data, or perform any number of real- time activities. Although you can use creative names for your own functions, it is important to note that the setup() and loop() functions must be spelled exactly as in the previous example because the compiler looks for those specific func- tion names. Also note that C is case sensitive: the functions setUp() and setup() are different as far as the complier is concerned.

The interesting part of the “Hello, world!” application occurs in the body of the setup() function. The first thing to note is that the body contains two lines, each of which are followed by a semicolon:

void setup() {

    Serial.begin(9600);

    Serial.println("Hello, world!"); }

Semicolons follow all statements in C. In contrast, code that involves program structure and logic does not require a semicolon. For example, a semicolon is not used after the name or braces that denote a function block.

Documento similar