Hardware Reference
In-Depth Information
And if you want to do math with decimals, you'll simply use a decimal point
to indicate that you'd like to do math with floating points so that you don't
lose the decimal like you do with integers:
Serial.println(11.0/4);
The line above would print 2.75 to the serial monitor.
long
When looking at Arduino code from the official examples or from other
projects, you may encounter the data type
long
. This type is mostly helpful
for Arduinos like the Uno, where integers use fewer bits (16 bits versus 32 on
the Galileo) and therefore can't count as high.
long
is a type meant to store
a wider range of numbers than
int
and use less precious memory. However,
on the Galileo, both
int
and
long
(along with their
unsigned
counterparts)
use the same number of bits and therefore have the same range.
While using an
int
will suffice in most cases, it's important to be aware of the
existence of
long
as some Arduino functions return this data type. For in-
stance, see
“millis()” on page 95
. It's also important to be aware of
long
if you
are writing a sketch that you may run on an Arduino Uno, Leonardo, or com-
patible board. On those boards, the maximum value for an
int
is 32,767.
boolean
A
boolean
variable is meant to store the values
true
or
false
. This data type
is frequently used to store “flags” to indicate a state or mode of your sketch.
You can then evaluate those variables in loops and
if
statements. For in-
stance:
#define START_BUTTON 3
boolean
gameStarted
=
false
;
//
void
setup
()
{
pinMode
(
START_BUTTON
,
INPUT
);
}
void
loop
()
{
if
(
digitalRead
(
START_BUTTON
))
{
gameStarted
=
true
;
//
}
if
(
gameStarted
)
{
//
// game play code here
}
}