====== SimpleSoftwareServo ====== by Tyler Bletsch (Tyler.Bletsch AT gmail.com) SimpleSoftwareServo is an [[http://arduino.cc|Arduino]] library; it drives servos on any number of pins at once. Same API as the default servo library, except you need to call SimpleSoftwareServo::refresh() each time you want to send a pulse (at least every 20ms, preferably). ===== Download ===== [[http://dsss.be/f/dat/SimpleSoftwareServo.zip|Download SimpleSoftwareServo v0.1]] ===== Requirements ===== * Arduino-compatible microcontroller running at ≥ 8 MHz * If using an ATtiny, you need an Arduino specification file and programmer that can get to it ([[http://hlt.media.mit.edu/?p=1695|details]]). * The user must call refresh() each time a pulse needs to be generated ===== Installation ===== * Extract to the 'libraries' directory under your sketchbook directory. ===== Background ===== Based on [[http://playground.arduino.cc/ComponentLib/servo|SoftwareServo]] with pieces from [[http://www.cunningturtle.com/servo8bit-library-version-0-6-released/|Servo8Bit by CunningTurtle]]. This is a replacement for SoftwareServo, which was great for ATtiny controllers, except for two problems: - It didn't include "Arduino.h", so it didn't compile (?) - It played pretty loose with the inter-pulse time, waiting until AFTER 20ms to pulse again, so if you had a delay of 15ms before the refresh, you'd wait 15+15=30ms plus computation time between pulses, which is fine for servos, but might upset the motor controller I'm using. So I took it and rewrote the refresh method and a few other things to standardize the pulse-to-pulse time rather than the inter-pulse time. Now the output on my oscilloscope is gorgeous. It's nearly a drop-in replacement for SoftwareServo, but be aware: It will generate a pulse EVERY time refresh is called, unlike SoftwareServo, which would ignore calls that were too close together. This may change behavior in some cases. I prefer this behavior, because it avoids uneven pulse timing. ===== Links ===== * [[http://hlt.media.mit.edu/?p=1695|How to use ATtiny MCUs with Arduino]] * Note: instead of the capacitor they show, you can just jam a wire between RESET and 5V. * [[http://playground.arduino.cc/ComponentLib/servo|SoftwareServo]] * [[http://www.cunningturtle.com/servo8bit-library-version-0-6-released/|Servo8Bit by CunningTurtle]]. ===== Example usage ===== {{youtube>ai3KY5z6Kgc}} #include /* * Move a servo based on input from a knob. Also set brightness of an LED proportional to servo angle. */ SimpleSoftwareServo servo1; int servo1_pin = 2; int led_pin = 0; int pot_pin = 2; // analog input pin (different numbering scheme from digital pins) void setup() { pinMode(led_pin, OUTPUT); servo1.attach(servo1_pin); } void loop() { int val = analogRead(pot_pin); servo1.write(map(val,0,1023,0,180)); analogWrite(led_pin,map(val,0,1023,0,255)); delay(15); // optional, since refresh() will delay as needed for the next pulse SimpleSoftwareServo::refresh(); }