How to Write a Library the Easy Way

by verdelj in Circuits > Arduino

4320 Views, 43 Favorites, 0 Comments

How to Write a Library the Easy Way

sample.JPG

First thing is first I am not a programmer or have a lot of experience programming I am just a beginner. there are a lot of ways of doing things this is my way maybe is the wrong way but it works for me. This is for beginners like my self, if you have any questions about anything just let me know and i will try to help. it is hard for me to explain some things with out going in to detail but ill try my best to answer. with that being said lets get started...

The Intro

I have been working in a project that uses a Pt2322 6 channel audio processor that uses i2c protocol, there is only one library written by oddwires and its a bit hard to use. I was having some problems understanding all the functions so I started to study the library and after some research it all made sense, so I decided to write my own blink library .

This library consist of 3 files

*.cpp

*.h

keywords.txt

The Header File

header.JPG

the code:

#include "Arduino.h"

class Blinker //we define our class

{

public: //public declared variables & functions, accessible outside of the class

Blinker (int pin, int duration); //default constructor of Blinker class

void blink (int times); //we define our function

private : //private declared variables & functions, accessible inside the class

int _pin;

int _d;

}; //end of class definition

looking at the above code we can see how we have created a class object named Blinker, and we see our function

blink we have set up are variables public and private. this is our header file or *.h now our.....

Our Cpp File

cpp.JPG

#include "Arduino.h"

#include "Diylibrary.h" //include our header file

Blinker::Blinker (int pin, int duration) //calling constructor

{ pinMode(pin, OUTPUT); // make pin an output

_pin = pin; //pin

_d = duration / 2; //wait halft of the wanted period

}

void Blinker::blink(int times) //we define our blink fuction

{

for (int i = 0; i< times; i++)

{

digitalWrite(_pin, HIGH);

delay(_d);

digitalWrite(_pin, LOW);

} }

if you look at the code you see that this is our main function first we include our header file in this case "diylibrary.h"

all the lines are well commented so i wont go into much detail. the constructor in this case Blinker assigns all the parameters to the variables. the blink fucntions does all our work for us.

Our Keywords File

keywords.JPG

with out the keywords file nothing from our library would be recognized by the environment and highlighted in color. Each line has the name of the keyword, followed by a tab followed by the kind of keyword. Classes should be KEYWORD1 and are colored orange; functions should be KEYWORD2 and will be brown.

so we add these to our keywords file ans save it as a *.txt

Blinker KEYWORD1
Blink KEYWORD2

Using the Library

sample.JPG

#include <Diylibrary> // we include our library

int ledPin =11; // green led pin

int ledPin1 = 12; //white led pin

int Duration=500; //our duration

Blinker BlinkWhite (ledPin, Duration); //our new created object will blink pin 11 for the value on our duration variable

Blinker BlinkGreen (ledPin1, Duration); //another new created object will blink pin 12 for value on our duration variable

void setup(){ }

void loop()

{

BlinkWhite.blink (3); // our class is blink it takes a single argument of the number of times to flash

delay(2000);

BlinkGreen.blink(3); // blink our white led the wait 2 seconds blink our green led and wait 2 seconds repeat (3) times delay(2000);

}

really simple code we define our pins and the duration. we create two objects BlinkWhite and BlinkGreen we add this to our loop and declare the number of times to flash with only 3 lines of codes we can keep adding more and more leds.

add the library to the arduino library folder and to make it work just connect two leds one on pin 11 and the other one on pin 12 and run the sketch

Downloads