EasyManua.ls Logo

Thames & Kosmos Code Gamer - PROJECT 9: Finger disco; THE PLAN; THE PROGRAM

Thames & Kosmos Code Gamer
66 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
You have already learned a little about numbers and
variables that can incorporate numbers. They aren’t
much use, however, if you can’t use them to calculate.
After all, you will normally be using your KosmoDuino
to read a numerical value from a sensor and then using
that to control something else. To do that, you have to
be able to calculate another numerical value from the
measurement.
In the Arduino programming language, all the basic
calculation types are available to you for this. For
adding and subtracting, you use the typical “+” and “-
symbols. For multiplication, use a star (“*”). For
division, use a simple forward slash (“/”).
By the way, you can also use the previous value of a
variable to calculate something new with it:
There is even an abbreviation for the frequently used
calculation step
i = i + 1;
:
CALCULATING IN THE PROGRAM:
// Addition (plus)
int x; // x is a whole number.
x = 1 + 4; // x has the value 5.
// Subtraction (minus)
x = 5 - 3; // x has the value 2.
// Multiplication (times)
x = 3 * 4; // x has the value 12.
// Division (divided by)
x = 12 / 6; // x has the value 2.
// Follow order of operations
x = 4 * 3 + 2; // x has the value 14.
int x = 4;
x = x + 2; // x has the value 6.
x = 2 * x; // x has the value 12.
int i = 0;
i++; // means x = x + 1.
++i; // also means x = x + 1.
KNOWLEDGE BASE

CodeGamer
CodeGamer manual inside english.indd 35 7/19/16 12:32 PM