Table of Contents

SimpleSoftwareServo

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).

Download

Download SimpleSoftwareServo v0.1

Requirements

Installation

Background

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:

  1. It didn't include "Arduino.h", so it didn't compile (?)
  2. 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.

Example usage

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();
}