user1095: 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?

user220: 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

user1136: 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.

1095: <p>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?</p>
 220: <p><strong>This is a simple solution.</strong> All you need to do is declare a <em>global variable</em> (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.</p>

<h2>Code you need:</h2>

<p><strong>Your library:</strong></p>

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

void bar() {
  //More code here!
}
</code></pre>

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

<p><strong>Your library header file (<code>name.h</code>):</strong></p>

<pre><code>extern Servo servoname;
</code></pre>

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

<p><em><a href="https://stackoverflow.com/questions/1045501/how-do-i-share-variables-between-different-c-files">Source</a></em></p>
 1136: <p>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.</p>

<p>Start a new Arduino sketch, name it as you like.</p>

<p>Go to the folder where you saved that file.</p>

<p>Add a new file called MyServoLib.h parallel to your main project file.</p>

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

<pre><code>#include &lt;Servo.h&gt;
#include "MyNewLib.h"
</code></pre>

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

<p>Copy the following simple stub code into your custom library file:</p>

<pre><code>#ifndef MY_SERVO_LIB_H__
#define  MY_SERVO_LIB_H__

#include &lt;Servo.h&gt;
#include &lt;Arduino.h&gt;

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
</code></pre>

<p>Your main file should look like this:</p>

<pre><code>#include &lt;Servo.h&gt;
#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

}
</code></pre>

<p>That's it!</p>

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