HOW TO WRITE YOUR OWN ARDUINO LIBRARY?

INTRODUCTION
In the previous tutorial, we’ve met with 7 segment displays. Among everything else, we’ve learned why libraries are useful. Now is the time to write our own. We will sort out this lesson in files that need to be made to make the library functional and transparent.
The thing we want, and will do, are functions that write numbers from 0 to 9, a function that will shut down all segments on the display and a function that will turn the point on and off. Let’s go…
HEADER
The header file is saved with an .h extension, it’s one of the two necessary files to make the library function. Bearing in mind that Arduino doesn’t do that by itself, here we will define everything that we use in a library. We create a class that we can call in later on in Arduino IDE with all the variables and functions that we want to make available to the user (public)  and those internal ones that we will use to make the library functional (private).
CLASS
What is a class? If you haven’t met with the term class by now, it’s basically a group of all functions and variables for a certain object. For example, let’s take a male human as an object. Every male has or hasn’t got certain characteristics like IQ, height, mass, height or moustaches. Looking from the programmers’ point of view, IQ is a whole number variable int, height and mass can be float, and moustaches which you either have or you don’t are going to be boolean.
With all that behind us, Â let’s get back to creating a library. Open your favorite text editor and let’s get into it:
// creating a class that we will call in Arduino IDE
class
SegmentÂ
{
 Â
// functions that user of the library works with
 Â
public
:
 Â
// here we will set up a connection with Croduino/Arduino and display
 Â
Segment(uint8_t pin1, uint8_t pin2, uint8_t pin3, uint8_t pin4, uint8_t pin5, uint8_t pin6, uint8_t pin7, uint8_t pin8);Â
 Â
void
Broj(
int
n);Â
// function "broj" will have one parameter, number "n" which we want to show up
 Â
void
Tocka();Â
// function "tocka" will turn the dot on and off
 Â
void
Ocisti();Â
// function "ocisti" turns off remaining segments
 Â
// internal variables
 Â
private
:
 Â
uint8_t _pin1;
 Â
uint8_t _pin2;
 Â
uint8_t _pin3;
 Â
uint8_t _pin4;
 Â
uint8_t _pin5;
 Â
uint8_t _pin6;
 Â
uint8_t _pin7;
 Â
uint8_t _pin8;
 Â
bool
b;
};
Don’t worry if you have some uncertainties, main part is located in the source file which we will get to in a second. Except for the above, the Header file has to have some more parts. It certainly is necessary to implement Arduino.h, which is automatically added when we compile the code, but not in the process of writing the library itself.
This part greatly depends on the version of Arduino IDE which the user has installed. So you need to pay much more attention to this part, however, this will be enough for now.
It’s a custom to add a small piece of code into the Header file that checks if the library has been added only once to the sketch. It is supposed to look like this:
#ifndef Segment_h
#define Segment_h
Â
// part for code
#endif
Finally, our Header looks like this:
SOURCE FILES
Source file is a part where we store all of our code/logic, and it’s written in C++ language. Even tho we’ve already explained the logic we want to write, feel free to take a peak in the 7 segment displays tutorial and remind yourself how to work with that. For starters we need to include Arduino.h and  Segment.h which we  have just created. Again, ofcourse we write in a text editor.
KEYWORDS
Keywords is a  .txt file that allows Arduino IDE to recognize keywords from our library and changes their color in the editor to make it easier to work with. It would look something like this:
ARDUINO IDE AND EXAMPLE OF A CODE
If we want the example of libraries used to appear in Arduino IDE File-Examples we need to create a subfolder called examples. This is how it will look like in Windows OS:
In subfolder examples, we will save examples of libraries’ usage. You can write it in Arduino IDE software, and as an example, we will write a countdown on the display.
Products used in this tutorial
No products found