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.