You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/learn/08.contributions/03.arduino-creating-library-guide/arduino-creating-library-guide.md
+23-6Lines changed: 23 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -61,6 +61,7 @@ class Morse
61
61
{
62
62
public:
63
63
Morse(int pin);
64
+
void begin();
64
65
void dot();
65
66
void dash();
66
67
private:
@@ -107,6 +108,7 @@ class Morse
107
108
{
108
109
public:
109
110
Morse(int pin);
111
+
void begin();
110
112
void dot();
111
113
void dash();
112
114
private:
@@ -125,19 +127,27 @@ First comes a couple of #include statements. These give the rest of the code acc
125
127
#include "Morse.h"
126
128
```
127
129
128
-
Then comes the constructor. Again, this explains what should happen when someone creates an instance of your class. In this case, the user specifies which pin they would like to use. We configure the pin as an output save it into a private variable for use in the other functions:
130
+
Then comes the constructor. Again, this explains what should happen when someone creates an instance of your class. In this case, the user specifies which pin they would like to use. The constructor records that in a private variable for use in the other functions:
129
131
130
132
```arduino
131
133
Morse::Morse(int pin)
132
134
{
133
-
pinMode(pin, OUTPUT);
134
135
_pin = pin;
135
136
}
136
137
```
137
138
138
139
There are a couple of strange things in this code. First is the **Morse::** before the name of the function. This says that the function is part of the **Morse** class. You'll see this again in the other functions in the class. The second unusual thing is the underscore in the name of our private variable, `_pin`. This variable can actually have any name you want, as long as it matches the definition in the header file. Adding an underscore to the start of the name is a common convention to make it clear which variables are private, and also to distinguish the name from that of the argument to the function (**pin** in this case).
139
140
140
-
Next comes the actual code from the sketch that you're turning into a library (finally!). It looks pretty much the same, except with **Morse::** in front of the names of the functions, and `_pin` instead of **pin**:
141
+
Next, you'll create a `begin()` function to handle hardware configuration. This will be called from the `setup()` function of the sketch. Hardware configuration is done in a dedicated function instead of the constructor because the hardware has not yet been initialized at the time the constructor code is executed. In our library, we need to set the pin as an output:
142
+
143
+
```arduino
144
+
void Morse::begin()
145
+
{
146
+
pinMode(_pin, OUTPUT);
147
+
}
148
+
```
149
+
150
+
Then comes the actual code from the sketch that you're turning into a library (finally!). It looks pretty much the same, except with **Morse::** in front of the names of the functions, and `_pin` instead of **pin**:
141
151
142
152
```arduino
143
153
void Morse::dot()
@@ -163,6 +173,7 @@ Finally, it's typical to include the comment header at the top of the source fil
163
173
/*
164
174
Morse.cpp - Library for flashing Morse code.
165
175
Created by David A. Mellis, November 2, 2007.
176
+
Updated by Jason A. Cox, February 18, 2023.
166
177
Released into the public domain.
167
178
*/
168
179
@@ -171,10 +182,14 @@ Finally, it's typical to include the comment header at the top of the source fil
171
182
172
183
Morse::Morse(int pin)
173
184
{
174
-
pinMode(pin, OUTPUT);
175
185
_pin = pin;
176
186
}
177
187
188
+
void Morse::begin()
189
+
{
190
+
pinMode(_pin, OUTPUT);
191
+
}
192
+
178
193
void Morse::dot()
179
194
{
180
195
digitalWrite(_pin, HIGH);
@@ -194,7 +209,7 @@ void Morse::dash()
194
209
195
210
And that's all you need (there's some other nice optional stuff, but we'll talk about that later). Let's see how you use the library.
196
211
197
-
First, make a **Morse** directory inside of the **libraries** sub-directory of your sketchbook directory. Copy or move the Morse.h and Morse.cpp files into that directory. Now launch the Arduino environment. If you open the **Sketch > Import Library** menu, you should see Morse inside. The library will be compiled with sketches that use it. If the library doesn't seem to build, make sure that the files really end in .cpp and .h (with no extra .pde or .txt extension, for example).
212
+
First, make a **Morse** directory inside of the **libraries** sub-directory of your sketchbook directory. Copy or move the Morse.h and Morse.cpp files into that directory. Now launch the Arduino environment. If you open the **Sketch > Import Library** menu, you should see Morse inside. The library will be compiled with sketches that use it. If the library doesn't seem to build, make sure that the files really end in .cpp and .h (with no extra .ino, .pde or .txt extension, for example).
198
213
199
214
Let's see how we can replicate our old SOS sketch using the new library:
200
215
@@ -205,6 +220,7 @@ Morse morse(13);
205
220
206
221
void setup()
207
222
{
223
+
morse.begin();
208
224
}
209
225
210
226
void loop()
@@ -228,7 +244,7 @@ Morse morse(13);
228
244
229
245
When this line gets executed (which actually happens even before the `setup()` function), the constructor for the `Morse` class will be called, and passed the argument you've given here (in this case, just 13).
230
246
231
-
Notice that our `setup()`is now empty; that's because the call to `pinMode()`happens inside the library (when the instance is constructed).
247
+
Notice that our `setup()` now has a call to `morse.begin()`which configures the pin that was set in the constructor.
232
248
233
249
Finally, to call the `dot()` and `dash()` functions, we need to prefix them with **morse**. - the name of the instance we want to use. We could have multiple instances of the `Morse` class, each on their own pin stored in the _pin private variable of that instance. By calling a function on a particular instance, we specify which instance's variables should be used during that call to a function. That is, if we had both:
234
250
@@ -243,6 +259,7 @@ If you tried the new sketch, you probably noticed that nothing from our library
0 commit comments