Let’s see how this works and dissect the program’s source code piece by piece.
In the first two lines, we define two
unsigned int
constants using the
const
key-
word.
LED_PIN
refers to the number of the digital IO pin we’re using, and
PAUSE
defines the length of the blink period in milliseconds.
Every Arduino program needs a function named
setup
, and ours starts in line
4. A function definition always adheres to the following scheme:
<return value type> <function name> '(' <list of parameters> ')'
In our case the function’s name is
setup
, and its return value type is
void
: it
returns nothing.
setup
doesn’t expect any arguments, so we left the parameter
list empty. Before we continue with the dissection of our program, you should
learn more about the Arduino’s data types.
Arduino Data Types
Every piece of data you store in an Arduino program needs a type. Depending
on your needs, you can choose from the following:
•
boolean
values take up one byte of memory and can be
true
or
false
.
•
char
variables take up one byte of memory and store numbers from -128
to 127. These numbers usually represent characters encoded in ASCII;
that is, in the following example,
c1
and
c2
have the same value:
char c1 = 'A';
char c2 = 65;
Note that you have to use single quotes for
char
literals.
•
byte
variables use one byte and store values from 0 to 255.
• An
int
variable needs two bytes of memory; you can use it to store numbers
from -32,768 to 32,767. Its unsigned equivalent
unsigned int
also consumes
two bytes of memory but stores numbers from 0 to 65,535.
• For bigger numbers, use
long
. It consumes four bytes of memory and stores
values from -2,147,483,648 to 2,147,483,647. The unsigned variant
unsigned long
also needs four bytes but ranges from 0 to 4,294,967,295.
•
float
and
double
are the same at the moment on most Arduino boards, and
you can use these types for storing floating-point numbers. Both use four
bytes of memory and are able to store values from -3.4028235E+38 to
3.4028235E+38. On the Arduino Due,
double
values are more accurate
and occupy eight bytes of memory.
report erratum • discuss
Hello, World! • 17
www.it-ebooks.info