Friday, June 14, 2019

MAKING YOUR OWN LIBRARY FOR ARDUINO


How to Create Library for Arduino using C / C ++ ??

Click below link to download (.pdf):





Blog Type : Programming Language / Embedded 

Date : 14 June 2K19
Author : Jaydeep Shah (E.C Engineer)
Blog  : https://radhey04ec.blogspot.com                Email : radhey04ec@gmail.com



A Library typically consist of three main components :
1) Header File (.h) = This contain the Library definitions .
2) Source code (.cpp) = This is c++ file contain library’s code.
3) Keyword file (.txt) = This contain that are used in library.

-         First of all .ino (Arduino code file ) , .h Header file & .cpp Source code file must be in same folder.
I have used DevC++ Software to write down  the code , You can use any of C or C++ software or text editor like notepad+ (2.0) etc.

Let’s start step by step, I have covered two different examples in this book.
Example 1 :
I want to make program using my own Library. Just user need to enter pin number of LED and delay time between blinking of LED. No need to declaration as OUTPUT pin etc. Let’s start.
1) Header file:

#include “Arduino.h”     

/*  This declaration allows you to use Arduino functions like a  HIGH,digitalWrite etc.*/

void blinkLED(int pin,int delayTime);


   /* Function definition so you can call it  from source file & can use in Arduino code.  */










SaveAS above file as a “myLib.h”


2) Source Code:

#include “Arduino.h”

#include “myLib.h”  

// Calling to header file

void blinkLED(int pin,int delayTime)  

  // Create function body
{

pinMode(pin,OUTPUT);   // Declare pin as output pin

digitalWrite(pin,HIGH);    // Pin output HIGH

delay(delayTime);             // delay time as  per user input

digitalWrite(pin,LOW);    // Pin output LOW

delay(delayTime);
}










SaveAs  ‘myLib.cpp’ in same folder.




3) Making .ino Arduino file in Arduino IDE :

#include “myLib.h”       // Call to our library

void setup()

{
}

void loop()

{

blinkLED(13,100);  
   // call to our function – declaration of op pin and delay
}









Save above code and Exit from Arduino IDE. Make sure all the file mustbe inside of same folder location. Again open it and load the code into your Arduino. It will work !!!!




User can use  your blinkLED function , no extra activity require.
When you debug / load the program you can see your .h file and .cpp file with .ino file in Arduino IDE.If there will error then you can reed it directly from Arduino IDE and save it for further debigging process.
Good Luck !!!

For Download soft copy (.pdf) Click below Link :



In Example-1 , I have used simple method for declaring function .But in real time problems we have to use and deal with many complicated calculation and Function's call .































Example – 02 :

-         - 2 Leds connected with pin no 03 , 05
-         - Make Library for this program
-         - Turn All LEDs on & Turn All LEDs off
-         - Flash all LEDs 4 times

Let’s start :

1) Header File : Code :

#ifndef myFirstLib_h

#define myFirstLib_h

#include “Arduino.h”

class myFirstLib {

public :

myFirstLib(int pinOne,int PinTwo);

void on();

void off();

void flash(int delaytime);

private:

int _pinOne;

int _pinTwo;
};

#endif







































Lets understand step by step….







·        #ifndef  - This is combination of if and endif loop. ifndef statement check to see if myFirstLib.h is not already defined in library package. It stop double declaration of  any identifiers within same library. This  statement act as Guard / Security for your program.

    >> Keywords : Reserve words for compiler / All have special meaning in program and in compiler.
 Ex: if, else ,for ,int ,char etc.

    >> Identifiers  : Name given to Entity / Object into Program
Ex : int money  : Here money is Identifier (name of varaible / Array etc)

·       
#define myFirstLib_h – This statement defines this is header file, so you can use it in Source file and .ino file.


·        #include “Arduino.h” – Because of this statement you can use Arduino’s Library functions like delay, HIGH or LOW, digitalWrite , pinMode etc.


·        class myFirstLib { - This is class declaration where all varaibles ,functions stored .


·        public : - Class property is public : so you can access data from anywhere.


·        myFirstLib(int pinOne,int Pin Two) :- This is the first statement of the class known as Constructor .

Constructor : This is special member function of class that is eexecuted whenever we create new objectof class.
-         Constructor always have same name as class name.
-         It doesn’t have any return type at all even void.


·        void on(), void off() ,void flash(int delayTime) : These three are Function declaration , all  these functions available for user call. Definition body of these functions store into source file.


·        private : Declaration of private class , access within class  itself.

·        }; - end of the class

·        #endif   - Wrap all above task within #ifndef function : End of Guard


2) Source File declaration :

#include “Arduino.h”

#include “myFirstLib.h”

myFirstLib :: myFirstLib(int pinOne, int pinTwo)

{

pinMode(pinOne,OUTPUT);


pinMode(pinTwo,OUTPUT);

_pinOne=pinOne;                                

_pinTwo=pinTwo;

}

void myFirstLib :: on() {

digitalWrite(_pinOne,HIGH);

digitalWrite(_pinTwo,HIGH);
}

void myFirstLib :: off() {

digitalWrite(_pinOne,LOW);

digitalWrite(_pinTwo,LOW);

}
void myFirstLib :: flash(int delayTime) {

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

{

digitalWrite(pinOne,HIGH);

digitalWrite(pinTwo,HIGH);

delay(delayTime);

digitalWrite(pinOne,LOW);

digitalWrite(pinTwo,LOW);

delay(delayTime);

}
}
















We have to undersatnd this code stap by step, Let’s start ….
·       
#include “myFirstLib.h” : Calling our header file

·        myFirstLib :: myFirstLib(int pinOne, int pinTwo) : First word myFirstLib is name of class. ::  sign indicate that you can access and use all functions and constructor. Second word myFirstLib(int pinOne, int pinTwo) indicate that we are dealing with constructor.


·        Body of Constructor when call it :
{
pinMode(pinOne,OUTPUT);
pinMode(pinTwo,OUTPUT);
_pinOne=pinOne;                            
_pinTwo=pinTwo;
}
Everytime you need to create object /  entity before using any functions. we will see it in Arduino code.

·        Body of Second Function :
void myFirstLib :: on() {
digitalWrite(_pinOne,HIGH);
digitalWrite(_pinTwo,HIGH);
}
Here class name = myFirstLib
Function name = on()
::  Indicate that you function and class relation. Same for remaining functions.


3) Arduino  .ino file

#include “myFirstLib.h”

myFirstLib mysetup(13,12);  // Create object mysetup  from class

void setup()
{

// For flash fourtime write down mysetup.flash(); here

}
void loop()
{

mysetup.on();     // or use mysetup.off()

}








·       





#include “myFirstLib.h”  : Call to our Library header file .h


·        myFirstLib mysetup(13,12);  : Creating object (instance) mysetup  and providing pin number information 13 and 12 . This object is from class myFirstLib.
This is part of constructor declaration in cpp source file. Further we will use mysetup (instance) entity to call any functions.


·        mysetup.on();    :  call the function on() using entity mysetup
mysetup.on() is use for calling function.


Same way we can use and call other functions.Before calling function we will have to create instance first then we can call function using instance.


After all completing this process , you need to save .h header file and .cpp source code file in same folder and then .zip it.
Now go to Arduino IDE.
Sketch >> Include Library  >> Include zip file
Select your file and load the program into your Arduino board.

Enjoy !!!






                                                                             This book  Dedicate to : SATI………

Author Details :
 JAYDEEP SHAH (E.C ENGINEER)
 AHMEDABAD – INDIA

CONTACT :

Website :

Facebook :
/shahjaydeep.jayshah


Feedback maters :: Comments or Email us.

Thanks...............................