Hardware Reference
In-Depth Information
the second line but again encounters a comment and ignores that, too. On the
third line, there is no comment; this is a real line of code.
It starts with the keyword int , short for integer. This is a variable declaration ;
it tells the compiler to reserve space for a variable , a named container that can
change its contents. Because the variable was declared as an integer, it can hold
only whole numbers between -32,768 and 32,767. This variable is named led . The
compiler will assign the value 13 to the variable. Finally, the line is i nished with
a semicolon. In C, a semicolon marks the end of an instruction.
Now for the next part:
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
The i rst line is a comment. It explains what the next portion of the code will do.
The next line is interesting. The keyword void means an empty data type.
The second word, setup , declares the name of a function. Because of the paren-
theses and curly brackets, you know that this is not a variable but a function.
Functions are portions of code that can be called inside a program; instead of
writing the same code dozens of times, it is possible to write it only once and
have the program call this function as required. It is also a way of separating
code for special needs.
Inside the parentheses, you would list any parameters for the function: these
are variables that can be passed to the function. Because there is nothing inside
the parentheses of setup() , there are no parameters. The function therefore does
not need any data to run. Because the function was declared as void, it will not
return any data either. When this function is called, it will do its job and then
return without any data. But what exactly does it do?
Everything included in the curly brackets is part of the function—in this case,
a single line of code. When the setup function is called, it executes one instruc-
tion, pinMode() . This instruction is not preceded with a data type, meaning that
it is not a variable declaration, and it is not a function declaration. Because it
has parentheses, it is a function, and unlike setup it requires two parameters:
led and OUTPUT . All the standard functions will be listed in Chapter 4, but just
to give you an idea, pinMode() is a function that tells the microcontroller how
a particular pin will be used. Before using a pin, the microcontroller needs to
know how it will be used; in this case, it will be sent as an output. The microcon-
troller can therefore set the output of a pin as HIGH or LOW and will not attempt
to read the status of the pin. The pin in question, identii ed as led , was dei ned
earlier in the code; it is pin number 13.
Search WWH ::




Custom Search