Hardware Reference
In-Depth Information
Preprocessing scans the sketch for functions and libraries that are included in your sketch. This process generates
a file that ends in the .h extension—the standard extension for a header file. Arduino does that for you, but libraries
require a header file and an implementation file. The header file is a list of all the function signatures, including the
return type, function name, and the function parameters. In certain cases, you will need to use in-line functions
in order to an optimizing goal. If you are defining your library as a C++ object, you should including the following
information in the header file: what the object inherits from, the object class name, when functions are members of
the class, and whether these functions are public or private.
â– 
Note
arduino ide forces the implementation file to have the *.cpp extension. if you use *.c , you will get errors.
One reason you may opt for a C function rather than a C++ object has to do with the potentiometer. In order
to read a potentiometer, you issue a function like analogRead(A0) . If all you are doing is reading values from the
potentiometer, you are already in good shape. Creating a single potentiometer as an object takes memory and can
quite easily overcomplicate a simple read from a device. However, if you are trying to avoid a huge number of global
variables, it makes sense to have a single object contain all of the information. If needed, you can create libraries just for
a single sketch. If your code starts to take multiple pages and you are writing many helper functions, you can transfer
that code into sketch-based libraries. Your project will load with all the library code together, and you'll see multiple
tabs. Eventually, you may want to use those libraries in more than one project. To do so, you will need to separate each
into their own library area and package them to install easily across the system. Header files can also be used to create
hardware profiles. The pins indicate what is used for custom shields, breadboard circuits, and even custom Arduino-
compatible boards. This would allow for portable code between hardware devices and configurations.
Figure 12-1 shows the #include "HardwareProfile.h" , which pulls in the header file HardwareProfiles.h . In
this file, you can define custom variables for a piece of hardware and set their default values.
Figure 12-1. Hardware Profile included into an Arduino Sketch
Listing 12-1. Example defines in HardwareProfile.h
#define motor1Dir 7
#define motor2Dir 8
#define motor1PWM 9
#define motor2PWM 10
#define motor1Enable 11
#define motor2Enable 12
Listing 12-1 shows a set of pins defined from the Motor Library example. This guarantees that you use the correct
pins every time you work with the same hardware. If you need to change from the default pins, you can redefine the
pin numbers in your sketch.
 
 
Search WWH ::




Custom Search