Standard Servo : degree and speed

Hey everyone,

I am new to programming with the arduino (diecimila) and servo motors. I've done my fair share of searching these forums, but haven't had much luck finding what I'm looking for.

I would essentially like to control the degree and speed of a standard rotation parallax servo. I would be listing a degree for the servo to go to, pause for a certain amount of time, then go to the next degree. Simple!

I've found this basic sweep code to work, but it seems to move to the servos full speed.

#include <Servo.h>

Servo s;

void setup(){
   s.attach(9);
   s.write(0);
}

void loop(){
   for (int angle=1; angle<180;angle++){
      s.write(angle);
      delay(10);
   }

   for (int angle=180;angle>0;angle--){
      s.write(angle);
      delay(10);
   }
}

Since I am just telling the servo to go to specific degrees, the following seems to work (correct me if I'm wrong.. I'm not at home!):

#include <Servo.h>

Servo s;

void setup(){
   s.attach(9);
   s.write(0);
}

void loop(){
   s.write(angle);
   delay(10);

   s.write(angle);
   delay(10);

   s.write(angle);
   delay(10);
}

Is there a simple way to make the servo sweep at a slower speed from one degree to the next? The speed won't be changing throughout the program.. I would just like it to move slower.

I have also experienced a jittery effect when changing the delay time, but I have read that this might be caused by the servo being powered by the 5v port on the Arduino.

Thanks.

Imagine how you'd move the servo if it were still part of an r/c system.
You'd simply move the stick more slowly.
Same approach here.

Never power servos from an Arduino.

I'm sorry, but I don't really understand what you mean by this. There won't be any interactivity involved with this project. Also, with an r/c system, wouldn't you generally be using a continuous servo?

It seems like it is much easier to control speed using a continuous rotation servo, but I need the control of using degrees with a standard rotation servo.

Increase the angle s-l-o-w-l-y

Replace delay(10) with delay(200) and look what happens. With delay 10 you do 100 degrees per second, which will full speed for most servos.

Korman

Thanks Korman.

I thought that the delay might have something to do with it. If this is the case, though, what would control the 'delay' between movements? Say I wanted to move to a postion, delay 5 seconds, then move to the next. I thought this is what delay is used for in my example!

Sorry Octocore, I was talking about the delay() in the Sweep example. Your code has no relation to this. In your example, you move the servos at full speed.

If the sweep example isn't what you want, you should read these postings before rehashing the same material.

Korman

Korman,

I think the sweep example will work just fine. What I was confused about is the delay in the 'for' loop. I'm not used to seeing delay used outside of the "pause for this amount of time" scenario.

And thank you for the link. I hadn't seen the VarSpeedServo library before, and it looks like this will also work.

:slight_smile: