19
7.4 Math
The Arduino can do standard mathematical operations. While floating point (e.g. 23.2) numbers
are allowed if declared as floats, operations on floats are very slow so integer variables and
integer math is recommended. If you have byte variables, no number, nor the result of any math
operation can fall outside the range of 0 to 255. You can divide numbers, but the result will be
truncated (not rounded) to the nearest integer. Thus in integer arithmetic, 17/3 = 5, and not 5.666
and not 6. Math operations are performed strictly in a left-to-right order. You can add parenthesis
to group operations.
The table below shows some of the valid math operators. Full details of their use can be found in
the Arduino Language Reference.
modulus (division remainder)
8 The Simple Commands
This section covers the small set of commands you need to make the Arduino do something
useful. These commands appear in order of priority. You can make a great machine using only
digital read, digital write and delay commands. Learning all the commands here will take you to
the next level.
If you need more, consult the Arduino language reference page at h
http://arduino.cc/en/Reference/HomePage.
pinMode
This command, which goes in the setup() function, is used to set the direction of a digital I/O pin.
Set the pin to OUTPUT if the pin is driving and LED, motor or other device. Set the pin to
INPUT if the pin is reading a switch or other sensor. On power up or reset, all pins default to
inputs. This example sets pin 2 to an output and pin 3 to an input.
void setup()
{
pinMode(2,OUTPUT);
pinMode(3,INPUT);
}
void loop() {}
Serial.print