Write Your Own Arduino Library

As we have seen while using Arduino, it never runs off by its library support! Whatever library you want at an instance, you can find that, by just using some relevant keyword while using the search engine. But through our experience we are telling, its not always that you will able to fine the library file you want to, use for your project. Or even if you found one, it will not be working!
Write Your Own Arduino Library
So in that case you have to write your own library. Like in our case, we were trying to interface the resistive touch scren with our Arduino MEGA board, we searched a lot over the internet, & got 2 versions of library for the same, but they were obsolete, & not working! So decided to write our own library get input from touchscreen to Arduino board
The same situation can happen with you also! So our recommendation is that you should  know "How to write Arduino Library?" 

So lets Start......
General Intro
Libraries are files written in either C or C++ (.c, .cpp) which provide your sketch/source code with extra or common functionality. They have a file extension of .h.
To use an pre-built/existing library in your Arduino Program simply go to the "Sketch" menu, choose "Import Library", and pick from the libraries available.
That will insert an #include statement at the top of the sketch/program along with the imported header file. These statements make the public functions and constants defined by the library available to your sketch. They also signal the ArduinoIDE  to link that library's code(Linking operation) with your program/code when it is compiled or uploaded.
Because libraries are uploaded on board with your sketch/program, they increase the amount of space used by the ATmega8 on the board. If you no longer need a particular library, simply delete its #include statements from the top of your code. This will stop the Arduino IDE from linking the library & decrease the amount of space used on the Arduino board. 

1st step: The basic sketch for the library is writen....
Its just like making boundaries before filling up the colors in the canvas! Later from this we will be making the required library.
So my code will look like this....

#include<xyz.h> //The custom made library as we want to name it 

xyz abc; //declaring a xyz class object abc

void setup()
{
   abc.initial(); //initializing with the option provided in the class definition
}
void loop()
{
  abc.proceed(); //things to at every loop iteration
}
 
So looking at above code, function initial() is for initializing the variables(ports etc.) and proceed() function for do some operational work at each looping cycle.
Now make a list what all going to need, all the input parameters you need, and also output params. also.....
Now its time to step up for writing our own Arduino Library for the above needed operation....
Arduino may be build up in JAVA, but its programing syntax is very similar to C++.

2nd step:
To build the library you are required to create two files, one with extension .h & other .cpp.
In the .h(Header file), you need to write as the following....
 
// ensure this library description is only included once
#ifndef Testing_h
#define Testing_h
// include types & constants of Wiring core API
#include "WConstants.h"
//Start of definition of class xyz
class xyz {
public:      //public declared variables & functions, accessible outside of the class
                      //the class constructor
xyz();              //Default constructor of class xyz, may contain anything for initializing object 
void initial();    //define your first function here
void proceed();//define your second function here
private:           //private declared variables & functions, accessible inside the class
int input_process(); //to process and draw conclusion from input
int callibrationdata(int n=0); //you might want to calibrate your device
};                   //end of class xyz definition 
#endif
//ending with end if
 
3rd step: 
Save the above code with an extension ".h". It will become your header file.
Next step is to write you own .cpp file, it will contain the main() function,
which is the driver function in a C++ program.
It will look like as...
#include<xyz.h>     //including the custom made header file
xyz::xyz()
{                            //calling the default constructor

}
void xyz::initial()
{                            //the body of initial() function
                              //setup your sensor here
}
void xyz::proceed()
{                            //the body of the proceed() function
  int input_data= input_process();
  int output=callibrationdata(input_data);
}
int xyz::input_process()
{
                             //analogRead or equivalent or whatever you need to do
}
int ExampleLib::fancyAlgorithm(int inputData)
{
                            //some complex stuff
}
 
Save it as .cpp extension.
Take both these files...(.cpp & .h) under one folder, and your own custom made library
is ready!
To install it, in your Arduino IDE go to arduino root folder in my case it is
"E:\arduino-1.0.1"

   

Open Libraries Folder and paste it, now you will be able to use your own custom made 
Arduino library by going to "Sketches" then selecting the "Import Library" option,
then selecting the library you hae just created.
HAPPY CODING :)