Programming Arduino Getting Started with Sketches



Yüklə 5,42 Kb.
Pdf görüntüsü
səhifə72/78
tarix22.12.2023
ölçüsü5,42 Kb.
#189286
1   ...   68   69   70   71   72   73   74   75   ...   78
Arduino SimonMonk 2011

Built-in Library Example
The LCD library has been used in the two previous chapters, so let’s look more closely
and see what is going on in a little more detail.
Referring back to sketch 9-01 (open this in your Arduino IDE), you can see that the
include
command includes the file LiquidCrystal.h:


This file is the header file for the class called 
LiquidCrystal
. This file tells the Arduino
sketch what it needs to know to be able to use the library. You can actually retrieve this
file if you go to your Arduino installation folder and file and find the file
libraries/LiquidCrystal. You will need to open the file in a text editor. If you are using a
Mac, then right-click on the Arduino app itself and select the menu option Show Package
Contents. Then navigate to Contents/Resources/Java/libraries/LiquidCrystal.
The file LiquidCrystal.h contains lots of code, as this is a fairly large library class. The
code for the actual class itself, where the nuts and bolts of displaying a message actually
reside, are in the file LiquidCrystal.cpp.
In the next section, a simple example library will be created that should put the concepts
behind a library into context.
Writing Libraries
Creating an Arduino library might seem like the kind of thing that only a seasoned
Arduino veteran should attempt, but actually it is pretty straight-forward to make a library.
For example, you can convert into a library the 
flash
function from 
Chapter 4
that causes
an LED to flash for a specified number of times.
To create the C++ files that are needed to do this, you will need a text editor for your
computer—something like TextPad on Windows or Text-Mate on Mac.
The Header File
Start by creating a folder to contain all the library files. You should create this folder
inside the libraries folder of your Arduino documents folder. In Windows, your libraries
folder will be in My Documents\Arduino. On the Mac, you will find it in your home
directory, Documents/Arduino/, and on Linux, it will be in the sketchbook directory of
your home directory. If there is no libraries folder in your Arduino, then create one.
This libraries folder is where any libraries you write yourself, or any “unofficial”
contributed libraries, must be installed.
Call the folder Flasher. Start the text editor and type the following into it:


Save this file in the Flasher folder with the name Flasher.h. This is the header file for
the library class. This file specifies the different parts of the class. As you can see, it is
divided into public and private parts.
The public part contains what looks like the start of two functions. These are called
methods and differ from functions only insofar as they are associated with a class. They
can be used only as part of the class. Unlike functions, they cannot be used on their own.
The first method, 
Flasher
, begins with an uppercase letter, which is something you
would not use with a function name. It also has the same name as the class. This method is
called a 
constructor
, which you can apply to create a new 

Yüklə 5,42 Kb.

Dostları ilə paylaş:
1   ...   68   69   70   71   72   73   74   75   ...   78




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin