by Tyler Bletsch (Tyler.Bletsch AT gmail.com)
SimpleSoftwareServo is an 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).
Based on SoftwareServo with pieces from Servo8Bit by CunningTurtle.
This is a replacement for SoftwareServo, which was great for ATtiny controllers, except for two problems:
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.
ai3KY5z6Kgc
#include <SimpleSoftwareServo.h> /* * 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(); }