Hardware Reference
In-Depth Information
Not even before your code is completed in this format, it is good practice to make it a version-controlled project
and enable issue tracking. this is explained in detail in Chapter 2.
Installing Arduino Libraries
The libraries are normally installed in the user sketch folder under libraries . Complete libraries are stored there for
use in any Arduino sketch. Installation typically consists of extracting the library into the Arduino libraries folder.
If you have placed your code in a version control system like GitHub, users can click the download option and
extract or clone the project into the default libraries folder. This is a very efficient way to distribute the code and make
it communally available.
Using Arduino Libraries
Once a library is extracted and placed, it is ready for use. The code that references the libraries will need to be updated
in one of the following formats.
To look for a system-level library:
#include <Motor.h>
To look in the project directory for the library:
#include "Motor.h"
The #include with caret bracketing ( < > ) indicates a system library, and it will search the library area for your
code. This step can be easily overlooked, so be careful and check that you have the correct symbols.
Arduino Objects and Library Conventions
C libraries do not use constructors and destructors. The library simply provides a set of previously created functions
and variables. However, in order to use a library, there may be some set-up configuration necessary. This initialization
would be in a begin() function, where all necessary elements of the library are configured. However, C++ supports
objects that typically have a constructor, which is invoked when the object is created. Conversely, a destructor is
invoked when the object is removed from memory, which means begin() is not always needed.
A destructor would usually be defined as a way to clean up the object on delete. However, delete in the AVR
environment is not always available. You can free up pins and clean up after the object is finished, or you can use an
end() function activated in the void setup() portion of an Arduino sketch.
Not
destructors are not typically used. the examples have the destructors removed.
The setup() function should include all the starting maintenance and object initialization in addition to the
constructor. One key reason to use the begin() function is that variables, objects, or communications may not be
initialized yet. For instance, if you want to use the Wire (I2C) library in a library or object, the Wire.begin() function
must be enabled before your object can use the Wire library. At some point, the user may want to end the use of an
object so that the end() function can be accessed, which takes care of any necessary cleanup. The recommended
Arduino best practice for writing libraries that have some kind of sequential data includes using “read” and “write”
functions instead of “send” and “receive”.
 
 
Search WWH ::




Custom Search