How do I create an Arduino library?

I need a simple example for the Arduino library.

I read this: https://www.arduino.cc/en/Hacking/LibraryTutorial

And it's a little tricky for me - I understand code examples better.

I need to build a library for Arduino. Inside the library, I need to have 2 functions, each of which receives 3 string values. The function then needs to bind the values ​​to one big string and send the value created withSerial.println(the new value);

I only need this for demonstration. So the values ​​are not important to me. It's just so I can learn from it. And I want to use it that way.

className.Function1("val1", "val2", val3");
className.Function2("val1", "val2", val3");

      

What extension should the files (.h.cpp) have? Of course I tried to use the Arduino library, but they are very complex.

If anyone has an example I am sure it will help me understand.

+3


source to share


1 answer


You don't need a class if all you need is a few functions, however, if you want to learn a method, I can point you in the right direction.

Here's a simple library. We will name it LED

.

In this example, we are recreating the blink example (included in the IDE) using the library. This is obviously more work than the example uses, however comparing the two should make it a little easier for you.

LED.h

#ifndef LED_h  //Prevents header contents from being added twice.
#define LED_h

class LED{
  public:
    void begin( char PinToUse );
    void on();
    void off();
  private:
    char pin;
};

#endif

      

The title contains what is called the inclusion of guards and is explained here: Why are #ifndef and #define used in C ++ header files?

LED.cpp

#include "Arduino.h"
#include "LED.h"

void LED::begin( char PinToUse ){  // LED:: indicates the function belongs to the LED class, and isn't a global function.
  pin = PinToUse;
  pinMode( pin, OUTPUT );
}

void LED::on(){
  digitalWrite( pin, HIGH );
}

void LED::off(){
  digitalWrite( pin, LOW );
}

      



You need to enable Arduino.h

it if you want to use any of the Arduino APIs in your library. Since I am using digitalWrite()

in a .cpp file, I will need to include it for this example. If you are using the API in the header, you need to enable it there.

Sketch

#include <LED.h>

LED led;

void setup() {
  led.begin(13);
}

void loop() {
  led.on();
  delay(500);
  led.off();
  delay(500);
}

      

As you can see, the library is split into a declaration (.h) and a definition (.cpp). Both the thumbnail and the .cpp file include the library header (.h).

Something that catches a lot of people happens when they want to use another library, such as an SPI class, in their own code. Unfortunately, the way the IDE is designed requires a sketch to include the internal libraries as well, whether or not that sketch is directly used. There is a more detailed explanation here .

Share your creation

Once your library is complete, you can post your library on the Arduino forums. Make sure you have some work examples to increase the chances of people trying to work. A good place for the library is here: Other Software Development .

If you're comfortable with GitHub ( Intro ), you can even add your library directly to the IDE's library manager, this will allow people to use it just by clicking install. Visit 1.5 Library Specification to see the add-on requirements.

+6


source







All Articles