Create Library in C
Create Library in C
- 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.
*/
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 !!!
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
>> Keywords : Reserve words for compiler / All have special meaning in
program and in compiler.
Ex: if, else ,for ,int ,char etc.
public : - Class property is public : so you can access data from anywhere.
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.
#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
void setup()
{
// For flash fourtime write down mysetup.flash(); here
}
void loop()
{
mysetup.on(); // or use mysetup.off()
}
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 !!!
Dedicate to : SATI………
Author Details :
JAYDEEP SHAH (E.C ENGINEER)
AHMEDABAD – INDIA
CONTACT :
Email : radhey04ec@gmail.com
Website :
https://radhey04ec.blogspot.com
Facebook :
/shahjaydeep.jayshah