The following code was posted in a giant thread called 3 axis auto stabilized platform, but it may be difficult to find in the hundreds of posts there. I will put the code in the playground with a more complete writeup when I can find the time, but for now here is the code. Please do post your comments and suggestons – as well as information on the projects you build using the code.
Have fun!
The library uses Timer2 to drive up to 8 servos using interrupts so no refresh activity is required from within the sketch.
The usage and method naming is similar to the Arduino software servo library Arduino Playground - Servo
except that pulse widths are in microseconds not degrees.
A servo is activated by creating an instance of the Servo class passing the desired pin to the attach() method.
The servo is pulsed in the background to the value most recently written using the write() method
Note that analogWrite of PWM on pins 3 and 11 is disabled when the first servo is attached
The methods are:
ServoTimer2 - Class for manipulating servo motors connected to Arduino pins.
attach(pin ) - Attaches a servo motor to an i/o pin.
attach(pin, min, max ) - Attaches to a pin setting min and max values in microseconds
default min is 544, max is 2400
write() - Sets the servo pulse width in microseconds.
read() - Gets the last written servo pulse width in microseconds.
attached() - Returns true if there is a servo attached.
detach() - Stops an attached servos from pulsing its i/o pin.
The library takes about 824 bytes of program memory and 32+(1*servos) bytes of SRAM.
The pulse width timing is accurate to within 1%
p.s. here is my list of potential enhancements:
- add method to read and write angle (in as an alternative to microseconds) axis
- A variant that uses timer1 – offering greater precision and the potential to drive more than eight servos.