3

I need a simple example for an Arduino library.

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

And it's a bit complicated to me - I understand better from code examples.

I need to build a library for an Arduino. Inside the library I need to have 2 functions that receive 3 string values each. Then the function needs to connect the values to one big string and send the value created using Serial.println(the new value);

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

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

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

If anyone has an example, I'm sure it will help me understand.

halfer
  • 18,701
  • 13
  • 79
  • 158
Dany Maor
  • 2,223
  • 2
  • 15
  • 22

1 Answers1

6

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

Here is a simple library. We'll call it LED.

In this example we will recreate the blink example (included in IDE) using a library. It is obviously more work than the example uses, however comparing the two should allow you to follow a little easier.

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 header contains what is known as include guards and are 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 include Arduino.h when you want to use any of the Arduino API in your library. As I use digitalWrite() in the .cpp file, I'll need to include it for this example. If you use the API in the header, you'll need to include it there instead.

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 separated into a declaration (.h) and a definition (.cpp). Both the sketch and the .cpp file include the library header (.h).

Something which catches many people out happens when they want to use another library like the SPI class in their own code. Unfortunately, the way the IDE has been designed requires the sketch to include the internally used libraries also, regardless of whether the sketch uses it directly. There is a more in depth explanation here.

Share your creation

Once your library is complete, you can post your library in the Arduino forums. Ensure you have some working examples to increase the chances of people trying your work. A good location for the library is here: Other Software Development.

If you are comfortable with GitHub (Intro), you can even add your library directly to the IDE library manager, this will allow people to use it by simply clicking install. Visit the 1.5 Library specification to see the requirements for addition.

Community
  • 1
  • 1
Chris A
  • 1,415
  • 2
  • 13
  • 16