How to write your own Library in Arduino?




Hello, Everyone! Well, I hope if you are here then either you are my friend or have a comfortable hand on Arduino and want to dive deeper or...you just got trapped by the google ads and search result 😉 Anyhow, once you are here I promise this post would be worth of your time.

SO, the big question is.. “ is it difficult?” “Will it take a lot of time?” “Do I need to know a lot of programming?”... “and which language?” .........and..

Wait.. I am just gonna stop you there because if, by any chance, you ran through my last blog then you would know how hard it is to start anything! But we won’t let that come in our way and start right now..ah! Yes after answering those questions first.
No,It’s not difficult. It will take 1 blog to make one of your own library ready and working. As for language you need to be a beginner in C++ or Just bear with me! It’s not on the language to stop your creativity, if you want it you can make it.

So, Let’s start..
The result of our effort would be a library which can give navigation commands to a bot. The first thing we do is to select our environment, any C++ I.D.E is sufficient but here is what you should know:-
  • Arduino commands are not executable by default on any IDE, so when you are sketching be conscious of your syntax.
  • Overlook all the errors given by IDE when it can’t understand the Arduino commands (ex- pinMode,Serial e.t.c)
  • What I will use here is Clion for C++ IDE on UBUNTU only because I know a way that the Arduino commands will be comprehensible to it, it’s beautiful and also free if you are a student. So why not?☺
  • So people who are using any other IDE please skip to HERE.
  • Now people who are with me first see this tutorial to install Clion.
    Done? Congrats! if you get a starting screen like this->







    Now to run an embedded system our favorite language Python made a wrapper called PlatformIO which has to be installed, for that I will guide:-

  • Install PlatformIO Core-
    use pip command-
    pip install -U platformio
    ?? Common Questions: what is pip? and where to write this?
    Pip command is a tool for installing and managing Python packages (or in other words) type this command to install any software written in Python.
    Where? In terminal (UBUNTU) or command prompt (Windows) and if you don’t have pip then follow this amazing tutorial and install it now!
    https://www.youtube.com/watch?v=jnpC_Ib_lbc&vl=en
  • Next,create new folder where you want to make your project, an empty folder is always better. (if you are a UBUNTU user like me cheers! This-> is the command: $cd /home/mylib)
  • Now Generate a project using PIO Core Project Generator or in other words type this: platformio init --ide clion --board uno
  • Now open your clion, go to import project , choose the newly created folder. Click on open project


 
......And we are good to go. In this newly created project environment we are gonna have a lot of fun.

HERE:
A library is made up of two files – a header file and a source file.

Header file:

A header file is one where we list all the function names , variables and define class constructors etc.

If this doesn’t make sense to you Awesome! You have less sh*t to deal with :) and don’t worry you still will be able to complete this project.

So uh..where were we? Right, we have to create a header file. In your IDE create a .h file named Move.h (please keep the first letter capital! --no reasons)
Here’s how to do it in Pycharm: - right click on the folder and select new, then select header. Now name it and save.

Source file:

A Source file is where you will actually write the program. Follow the same steps given above to create a source file BUT this time select .cpp type.

Now once you have these files ready, let’s move on and sketch the header file. Type these lines:
#ifndef Move_h
#define Move_h

#endif
Whatever we plan to write will be written inside this block . This literally means that
if you have not included the header file include it else no need to import again .
       This basically prevents the multiple import of a library.

Now let's make a class:-
class Move
{
};


Now in the class we will declare our constructor.

In simple words...... whenever we want to use the pins in an Arduino we have to initialize it using pinMode command,right?
 And sometimes we also want to print cool stuff like “bot starting...”! but then it is required that for every time our library is
 imported the initialization (here pin configuration) should be performed automatically.
When we want something to happen automatically at every start we put them in weird looking functions called constructors.
Now there's two things to keep in mind: 
  • The name of constructor should be same as the name of the class
  • The constructor has no data type. (not understood? Simple all function definition start with a data type like int , float ,
     void etc. But constructors have nothing to start with)
ex: >
 Move(uint8_t leftRed,uint8_t leftBlack, uint8_t rightRed,
uint8_t rightBlack, uint8_t leftSpeedPin,uint8_t rightSpeedPin);



Next we will declare all the functions that we are going to use. Fortunately for now we have only five functions ;)
  • void forward();
  • void backward();
  • void left();
  • void right();
  • void brake();


Now we know that we will use these functions as commands to our bot so we WILL use these functions. In cases where functions of a class are to be accessed by other programs we say that these functions are public: . So what we do is that we keep the definitions in the public area. Like:

public:
    //constructor--------
    Move(uint8_t leftRed,uint8_t leftBlack,uint8_t rightRed,uint8_t rightBlack,
 uint8_t leftSpeedPin,uint8_t rightSpeedPin);


    // navigation's function
    void forward();
    void backward();
    void left();
    void right();


I believe you are trying to reason out why we included constructor in public.
 No reason at all,In my programming style I tend to use and change the pin defining
 function for a better optimized code.

Now we need the Variables! To declare the pins. But Just pause and think for a second,
 do we want others to “accidentally” change our pin numbers? No,right? So we say that these 
variables  are private to the class and hence only the function defined in the class can
 access and use them.

So, here we go...............
private:
 // defining pins
 uint8_t  _leftRed, _leftBlack, _rightRed, _rightBlack,_leftSpeedPin,_rightSpeedPin;


Source File: - In the .cpp file first type these:

#include "Arduino.h"
#include "Move.h"



You guessed right! Here we are including the previously created header file and the Arduino header file.
Now as mentioned earlier we will configure the pins in the constructor and then define the functions. Here’s an example:




Move::Move(uint8_t  leftRed, uint8_t  leftBlack, uint8_t rightRed, 
 uint8_t rightBlack,uint8_t leftSpeedPin, uint8_t rightSpeedPin)
{

    pinMode(leftRed, OUTPUT);
    pinMode(leftBlack, OUTPUT);
    pinMode(rightRed, OUTPUT);
    pinMode(rightBlack, OUTPUT);
    pinMode(leftSpeedPin,OUTPUT);
    pinMode(rightSpeedPin,OUTPUT);
    _leftRed = leftRed;
    _leftBlack = leftBlack;
    _rightRed = rightRed;
    _rightBlack = rightBlack;
    _leftSpeedPin = leftSpeedPin;
    _rightSpeedPin = rightSpeedPin;
    delay(500);
    
}

//Now we define the functions:-

void Move::forward()
{
    Serial.println("moving forward");
    analogWrite(_leftSpeedPin,150);
    analogWrite(_rightSpeedPin,150);
    digitalWrite(_leftRed, HIGH);
    digitalWrite(_leftBlack,LOW);
    digitalWrite(_rightRed, HIGH);
    digitalWrite(_rightBlack, LOW);
    delay(10);
}
void Move::backward()
{
    Serial.println("moving backward");
    analogWrite(_leftSpeedPin,150);
    analogWrite(_rightSpeedPin,150);
    digitalWrite(_leftRed, LOW);
    digitalWrite(_leftBlack, HIGH);
    digitalWrite(_rightRed, LOW);
    digitalWrite(_rightBlack, HIGH);
    delay(10);
}
void Move::left()
{
    Serial.println("moving LEFT");
analogWrite(_leftSpeedPin,150);
    analogWrite(_rightSpeedPin,150);
    digitalWrite(_leftRed, HIGH);
    digitalWrite(_leftBlack, LOW);
    digitalWrite(_rightRed, LOW);
    digitalWrite(_rightBlack, LOW);
    delay(10);
}
void Move::right()
{
    analogWrite(_leftSpeedPin,150);
    analogWrite(_rightSpeedPin,150);
    Serial.println("moving RIGHT");
    digitalWrite(_leftRed, LOW);
    digitalWrite(_leftBlack, LOW);
    digitalWrite(_rightRed, HIGH);
    digitalWrite(_rightBlack, LOW);
    delay(10);
}


void Move::brake()
{
    
    Serial.println("Stopped");

    digitalWrite(_leftRed, LOW);
    digitalWrite(_leftBlack, LOW);
    digitalWrite(_rightRed, LOW);
    digitalWrite(_rightBlack, LOW);
    delay(10);

}




Got it? We are setting our variables that we declared in move.h to the input to be
 recieved through the object of the class. And then we are defining the functions for
our robot.
 


Done!!
We made our first library! Wasn’t it easy? 
Now before we get our hopes up, let’s see how to include it in Arduino IDE.

  1. Create a folder with the same name as of the class.
  2. Paste the header and the source files in the folder.
  3. Compress the folder.
  4. Open Arduino IDE.
  5. Go to Sketch,include library,add zip library.
  6. Find your zipped folder move.zip

    Here...help your eyes:

     
Tada!! You got your first library installed. Want to Use? Here’s an Arduino Example Code:

 


#include <Move.h>

Move robot(2,3,4,5,6,7);
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
robot.forward();
delay(1000);
robot.backward();
delay(1000);
robot.left();
delay(1000);
robot.brake();
delay(500);
}

Save the file and upload it,Enjoy! If you need the full code please visit my github post: Navigation-master.
 

Hello Again At the End! Was the post helpful? Is there room for improvement? I would love your suggestions. Feel free to comment or e-mail me and I would definitely get back to you.
Thanks for reading............will be back next week with another exciting post!

Comments

Popular posts from this blog

Let's Begin Now!!

Does English have a role in our personality?