away any long routines, and making hardware easy to use. This chapter looks
at both of these kinds of libraries.
When you import a library, you import an .h i le, the header i le. This is a text i le
that describes what is in a C++ i le (that ends with the .cpp extension). Header
i les are used in most C and C++ projects. They are not required for sketches but
are required for libraries. They are a simple way of telling the compiler what to
expect in the C++ i le and how to use it. It is also an excellent way for developers
to know what is contained in a library; everything is listed in just a few lines.
Function libraries are an easy entry into writing a library; they contain simple
functions similar to ones you might write in your main sketch. Don't worry, you'll
look at making some with advanced capabilities in the “Advanced Libraries”
section. For now, these contain only simple functions, and their header i le is
You can demonstrate the use of a potential library with a function call. You
can use an Arduino to calculate the answer to the Ultimate Question of Life, the
Universe, and Everything. Luckily, Douglas Adams has already answered this
question in The Hitchhiker's Guide to the Galaxy ; a super-computer calculated this
question for 7.5 million years before coming up with the answer: 42. Luckily, the
Arduino is a lot faster to come up with the answer, and the function looks simple:
Looks simple, doesn't it? The only difi culty is making this function usable
as a library. It requires a few things to set up before it is useable. First, you must
think of a name for the library, as well as the folder that will contain your i les.
The choice of the name is important because it will also be used for the name
displayed on the Import Library menu item. Try to think of a name that clearly
identii es the library you will create—either the component name, function, or
application. Users of your library will depend on this. For this example, use
Create a folder with this name on your desktop or anywhere you have easy
access to. Next, you need to create two i les: the source i le and the header i le.
The Arduino IDE cannot directly open or save C++ and .h i les. These can be
created with a standard text editor or with an IDE. Code::Blocks is a freeware