Hardware Reference
In-Depth Information
Advanced Libraries
The previous example used only simple functions, but Arduino libraries are
capable of much, much more. You have seen how to initialize external hardware
with the Arduino, usually by specifying some hardware pins. For example,
when using the Servo library, the user must specify which pin is connected to
the servo. Afterward, functions are available to control the servo, but the user
does not have to tell the driver which pin to use. The reason is simple: the driver
has stored that data in memory, so the user does not need to specify it every
time. How? C++ classes.
C++ development is oriented around objects. What is an object? It can be many
things, but mainly, it is a collection of variables and functions, all rolled into a
C++ class. A class provides blueprints; it does not dei ne any data, but dei nes
data types. An object is then created by using the class blueprint.
Imagine a trafi c light. It has three lights: red, yellow, and green. Physically,
three lights are connected to a microcontroller, and the microcontroller issues
instructions to each output pin; turn on the red light, and turn off the yellow
light. The trafi c light is physically an object. If you make a second trafi c light,
it is a copy of the i rst; it does exactly the same thing, has the same hardware,
and will be used for the same applications as the i rst trafi c light, but it operates
independently of the i rst. This is similar to the concept of a software object. In
software, an object is a structure in memory that contains the data and func-
tionality all wrapped up in one package. In this case, imagine an object called
trafficLight . It will have several functions to allow it to work and several
variables to help it keep track of its state. If you create a trafi c light and connect
it to an Arduino, you could create a trafficLight object. Connect a second one,
and you could create a second trafficLight object, and so on.
An object is dei ned by a C++ class. A class is a structure of code that contains
functions, variables, and a constructor. Here's an example.
A trafi c light requires three pins to work: one to activate the red light, one
for the yellow, and one for the green. Under normal conditions, only one light
should ever be on at the same time. This is easy to accomplish, but it requires
you to do two things; turn off the previous light, and turn on the new light. This
is easy enough with one trafi c light, but with multiple lights, it would become
increasingly difi cult to manage all the pins and variables to keep track of their
states. To make things easier, you could make an object.
To create an object, you need several things. First, you need a way to coni gure
the object; telling it which pins to use. Then, it requires at least three functions
for manipulating the lights. Naming them after the color they control can make
it more intuitive: red() , amber() , and green() . When creating this library, start
with the header i le, and “describe” the object before building the different
parts. This is what the object in the header i le TrafficLight.h might look like:
 
Search WWH ::




Custom Search