Library Files

A library contains a .h header file and a .c or .cpp source code file as a minimum.  Additional files can be added as needed.  The files should used inside a directory typically given the library name.

Create a .h File


#ifndef MY_LIBRARY_H		//Do only once the first time this file is used
#define	MY_LIBRARY_H

#include "Arduino.h"

class MyLibraryName
{
  public:
    MyLibraryName(int pin);
    void SampleFunction();
  private:
    int InternalVariable;
};

#endif

Create a .c File


#include "Arduino.h"
#include "MyLibraryName.h"

//*********************************
//*********************************
//********** CONSTRUCTOR **********
//*********************************
//*********************************
MyLibraryName::MyLibraryName(int pin)
{
  InternalVariable = pin;
}

//*************************************
//*************************************
//********** SAMPLE FUNCTION **********
//*************************************
//*************************************
void MyLibraryName::SampleFunction()
{
  InternalVariable = 2;
}

Using It In A Project

Copy the library files to a directory off the libraries sub directory of your sketchbook directory, e.g. YourSketchbook\libraries\MyLibraryName\


#include <MyLibraryName.h>

//Create an instance of the class
MyLibraryname MyLibraryname1(3);

MyLibraryname1.SampleFunction();

Nice Extras To Add

See Keywords and an Example of usage here.

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *