Servo library extension

High everyone,

I am a new arduino user. Received my first board yesterday, and to my surprise after 30 minutes I was able to control a servo motor with a potentiometer, which was exactly what I needed for my paragliding project :slight_smile: It was about a zillion times easier than I expected…

Anyway, I soon realized that a servo motor will move to your desired position at its full speed, and in order to have it move slower, you need to send multiple commands with delays between them. This can become a bit tricky if you want it to move nice and smooth, so I decided to create a Servo library extension.

example code would be as follows:

#include <Servo.h> 
#include <StepServo.h>

StepServo myservo;  
int potpin = 0;
int val; 
int servoSpeed = 100; // 100 degrees / sec
void setup() 
  myservo.write(0);   // Initialize servo so the position is known
void loop() 
  val = analogRead(potpin);            
  val = map(val, 0, 1023, 0, 179);     
  myservo.write(val, servoSpeed);  // Move to desired position at servoSpeed
  delay(15); // (optional with StepServo)

Comments from the header file:

Extension class for Servo.h
Provides a way to move the servo slower than normal
(by moving it in small steps and delaying between them)

To move the servo slowly:

  • servo.write(Position, Speed) // Speed = degrees / second
  • keep calling either update() or write(Position, Speed) in a loop

write (P, S) will not move servo to your desired position, but it will move
it CLOSER to that position by a calculated step according to time passed
since last call. That’s why you need to keep calling write(P, S) or
update() until the servo gets where you want.

If you try to update position more often than UpdateInterval ms, your call
will be ignored. This makes delay() optional in your loop.

*** NOTE ***
First write will be done at full speed, as there is no way to know
current position of the servo. You can initialize it to a certain value,
and keep moving slowly afterwards.

The Speed (degrees per second) setting is kind of relative, because
the step value is rounded to an integer. Imagine this situation:
If you iterate every 15ms, step can be rounded to 1 degree, while iterating
at 20 ms, step can be rounded to 2 degrees.

Also note: servo is first delayed, then moved, not the other way around.
So there will be a delay of UpdateInterval before first step is done.

Play with Speed, UpdateInterval and MaxStep vars to get it right.

Question is: how do I share it? Is there any official place to upload? Any comments?