Hardware Reference
In-Depth Information
In
Example 3-4
, the line
#include <Wire.h>
is a workaround to
get the servo library working with Galileo in Intel's version 0.7.5
of the software. It may not be required for later versions. Your
best bet is to try it with and without the line to see what works.
The Servo Object
When you're reviewing the lines of code that control the servo, you're en-
countering a new programming concept called the
object
.
Servo myservo;
In programming parlance, this line creates a
Servo
object called
myservo
. You
don't need to know the nitty gritty of what an object is or how it works, but
what's important with an object is that it lets you create multiple servos and
act on them independently with the servo functions. The best way to see this
in action is to look at an example with two servos. Let's say you have a camera
on a servo that controls its pan from left to right and another servo that con-
trols the camera's tilt from up to down. To do this, you would create two
Servo
objects:
Servo panServo;
Servo tiltServo;
You can then tell Galileo which pin each servo is connected to with the
at
tach()
function. Assuming the pan servo is on pin 9 and the tilt servo is on
pin 10, you'd put the following in your setup function:
panServo.attach(9);
tiltServo.attach(10);
Then to tell each servo where to go, you'll use the
write()
function, and input
the number of degrees between 0 and 180. If you wanted each servo to sit
right in the middle, it would be 90 degrees:
panServo.write(90);
tiltServo.write(90);
Those are the basics of how the servo library works and a quick crash course
in creating and acting on an object. Most other libraries that you'll use will
use the object paradigm and usually include examples to help you under-
stand them.
Looking at Linux
Up until this point in the topic, most of what you've learned has focused on
the Arduino-like capabilities of Galileo. As mentioned in
Chapter 1
, one of the