-3

I'm trying to make my first library for Arduino and want to use a servo object in my library. How can I do this?

  • Welcome to Arduino SE! First of all, getting frustrated isn't going to get you anywhere. I understand it can be painful coding sometimes with problems, but it's easier to get help with a calm tone. Secondly, do you mean like creating an object like Servo myservohere; within your library and using that object within your main code? – Anonymous Penguin Apr 18 '14 at 0:01
  • Yes! Is that possible? – Zack Frost Apr 18 '14 at 0:03
  • Important point: do you intend to create a C++ library i.e. with one -or more- class, which would then encapsulate a servo object? – jfpoilpret Apr 18 '14 at 8:31
  • 2
    It would be good to post some code to your question, showing what you have already done so far, and more importantly what part of the current code you would want to put into your own library. – jfpoilpret Apr 18 '14 at 8:34
  • Considering the fact you are a young hobbyist, I will first ask the question "do you absolutely need to write a library?". Writing libraries for Arduino generally comes in a second stage after writing several sketches, and it requires a good understanding of the language used by Arduino (C++). – jfpoilpret May 3 '14 at 5:51
1

This is a simple solution. All you need to do is declare a global variable (i.e. make it accessible everywhere in your sketch), and then add some additional code to "share" the servo between the different parts of the code.

Code you need:

Your library:

Servo servoname;
void foo() {
  //Some code here!
}

void bar() {
  //More code here!
}

The methods foo() and bar() are irreverent to the code, you just need to declare it.

Your library header file (name.h):

extern Servo servoname;

All you need to do then is import your new library and use servoname. You will have to still call servoname.attach([pin]); before you can use the servo, but then you just need to use the servo.

Source

  • when I do that I get an error: "Servo does not name a type" – Zack Frost Apr 18 '14 at 16:27
  • @Zack Have you imported the servo library into your library? I can't help you without any code... – Anonymous Penguin Apr 18 '14 at 20:36
1

This should give you an idea of how to create a custom library which you can use in your project. I actually compiled it with Arduino IDE 1.0.5 on Mac OsX. I did not run it on an Arduino board, but have similar code in a production project, that works just fine.

Start a new Arduino sketch, name it as you like.

Go to the folder where you saved that file.

Add a new file called MyServoLib.h parallel to your main project file.

In your main file, include the servo library from Arduino and your custom library like so:

#include <Servo.h>
#include "MyNewLib.h"

If you close and reopen your project, it the Arduino IDE will automatically load your custom library file as well.

Copy the following simple stub code into your custom library file:

#ifndef MY_SERVO_LIB_H__
#define  MY_SERVO_LIB_H__

#include <Servo.h>
#include <Arduino.h>

class MyServoLib
{
public:

  MyServoLib()
    : _servoPin(9)
    , _attached(false)
  {}

  void attach()
  {
    if (_attached)
      return;      

    _attached = true;
    _servo.attach(_servoPin);
  }

  void detach()
  {
      if (! _attached)
          return;

      _attached = false;
      _servo.detach();
  }    

  void setPin(int pin){
    _servoPin = pin;
  }


private:

  bool _attached;
  Servo _servo;
  int _servoPin;

};


#endif

Your main file should look like this:

#include <Servo.h>
#include "MyNewLib.h"

// use your custom lib here
MyServoLib myServo;
int MyServoPin = 5;

void setup(){

  pinMode(MyServoPin, OUTPUT);
  myServo.setPin(MyServoPin);  
  myServo.attach();

  // your additional setup code here
}

void loop(){

  // your main code here

}

That's it!

Upload it to an Arduino board and give it a try. The servo won't move, but it should get powered on.

0

If your question is about including library A inside library B and then including library B in your main program, then you need to be aware of a limitation of Arduino IDE (discussed in this answer to a similar question).

Arduino IDE (at least 1.0.x, I have not checked with 1.5) for an obscure reason will not include libraries included by other libraries.

The only way to work around this issue is to include ALL libraries (direct and indirect) in your main program file (.ino file):

// Needed by library "B"
#include "A.h"
// Needed by your program
#include "B.h"

void setup() {
    // You can use B stuff here
}

void loop() {
    // You can use B stuff here
}

Another workaround is to trash Arduino IDE and use a better (I mean a real) IDE: many good options have been discussed in this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.