7 The Standard Arduino Library This library is where all the goodies live. You can only get so far with the core C
language; what you really need is a big collection of functions that you can use in your
sketches.
You have already met a fair few of these, such as
pinMode, digitalWrite , and
analogWrite . But actually, there are many more. There are functions that you can use for
doing math, making random numbers, manipulating bits, detecting pulses on an input pin,
and using something called interrupts.
The Arduino language is based on an earlier library called Wiring and it complements
another library called Processing. The Processing library is very similar to Wiring, but it is
based on the Java language rather than C and is used on your computer to link to Android
over USB. In fact, the Arduino application that you run on your computer is based on
Processing. If you find yourself wanting to write some fancy interface on your computer
to talk to an Arduino, then take a look at Processing (
www.processing.org
).
Random Numbers Despite the experience of anyone using a PC, computers are in actual fact very
predictable. Occasionally it is useful to be able to deliberately make your Arduino
unpredictable. For example, you might want to make a robot take a “random” path around
a room, heading for a random amount of time in one direction, turning a random number
of degrees, and then setting off again. Or, you might be contemplating making an
Arduino-based die that gives you a random number between one and six.
The Arduino standard library provides you with a feature to do just this. It is the
function called
random. random returns an
int and it can take either one argument or
two. If it just takes one argument, then it will return a random number between zero and
the argument minus one.
The two argument version produces a random number between the first argument
(inclusive) and the second argument minus one. Thus