Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

ServoTimeTimer1 Library.

The ServoTimeTimer1 Library drives servos on pins 9 and 10 by using the timer1 hardware. This library is based on the Servo Timer 1 Library but instead of controlling the angle with degrees ranging between 0 and 180, you send the actual pulse length in microseconds. Microseconds will give you more than 10 times the resolution that degrees,

Standard Methods

attach(int)
Turn a pin into a servo driver. Calls pinMode. Returns 0 on failure.
detach()
Release a pin from servo driving.
write(int)
Set the angle of the servo in microseconds 500 to 2500
read()
return the value set with the last write().
attached()
return 1 if the servo is currently attached.

Installation and Examples

You can find the an updated version of the code (for the Atmega168) here: Attach:servotimetimer1_168.zip

Unpack it into your arduino-xxxx/hardware/libraries folder (since Arduino 00010) to add the library.

Seems to have trouble compiling on Arduino IDEs 12 and up(works great on 11).

It works great on Arduino IDE 18, with this simple modification! In order to use the ServoTimeTimer1 library you have to move the line #include <wiring.h> from ServoTimeTimer1.h file and place it after the line #include <avr/interrupt.h> line in the ServoTimeTimer1.cpp file or you get the cryptic error: /usr/lib/gcc/avr/4.3.4/../../../avr/include/stdlib.h:111: error: expected `)' before ‘int’

You will find an example under File/Sketchbook/Examples/Library-ServoTimeTimer1 called tow_pots_controlling_two_servos

Connect on potentiometer to analog 0 and another to analog 1. Connect one servo to digital 9 and the other to digital 10. Compile. Turn the potentiometers to move the servos.

Here is the code:

//Example code for using ServoTimeTimer1 library
// hardware control of up to two servos, on Arduino pins 9 & 10 

#include <ServoTimeTimer1.h>

#define servoPin1 9 
#define servoPin2 10
#define potPin1 0
#define potPin2 1

ServoTimeTimer1 servo1;
ServoTimeTimer1 servo2;

int potVal1 = 0;
int potVal2 = 0;

void setup()
{
  servo1.attach(servoPin1);
  servo2.attach(servoPin2);

}

void loop()
{

    potVal1 = analogRead(potPin1);

    potVal2 = analogRead(potPin2);

    //Here we convert a 0-1023 range to a 500-2546 range
    //2546 is clipped to the maximum value of 2500 internally

    servo1.write((potVal1*2)+500); 
    servo2.write((potVal2*2)+500); 

}