Hardware Reference
In-Depth Information
The loop() function does exactly what its name implies; it loops continu-
ously, as long as power is applied to the Arduino. In the example, loop() set
the output of a pin HIGH, waited for 1 second, set the output of the same pin
to LOW, and then waited for another second. After this was done, the function
ran again. This is also the reason why coni guration should not be done inside
the loop() function; the same code will be run over and over again. If you had
put any coni guration here, variables could have been overwritten, and setting
pin coni gurations might have slowed down the application.
These two functions are required for any sketch, though you are free to add
your own functions as required.
Programming Basics
As said previously, programming is the art of writing something that is read-
able by humans and that can be converted to be understood by computers. The
problem is that computers, despite what people try to tell you, aren't intelligent
at all. They need to be told exactly what to do, and require exact instructions.
Source code has to be laid out in a precise way.
Variables and Data Types
In your sketches, most of the time you will want to store data and perform some
type of calculation. Counting the number of times a button is pushed, storing
the voltage on an analog pin, or performing a complex mathematical calcula-
tion with vectors: require data to be calculated and stored. This data is saved
in a variable , a memory location that can be changed as required. By declaring
a variable, you are asking the compiler to allocate a specii c amount of memory,
depending on the data type.
There are different types of data, and you must i rst tell the compiler exactly
what sort of data you want to store. If you dei ne a variable as capable of holding
integers, you cannot use the same variable to store l oating-point data, or even
a string of text. The different data types are listed in Table 2-1.
Table 2-1: Diff erent Data Types
DATA TYPE
CONTENTS
void
No data type
boolean
True or false
char
One character, stored as an ASCII number ('A', 'B', 'C'...)
unsigned
char
Decimal numbers, from 0 to 255
 
Search WWH ::




Custom Search