Go Down

Topic: Adjusting servo speed (Read 5 times) previous topic - next topic

©UnB©

Hi!
i'm almost done with my little project  8-)
The only thing i need to figure out now, is that how i adjust the speed of a servo.

Help me with the following code:  ::)
Code: [Select]
int ledPin = 13;
int inputPin1 = 2;
int inputPin2 = 4;
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
               // a maximum of eight servo objects can be created
int pos = 0;    // variable to store the servo position

void setup() {
 // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);      
 // initialize the pushbutton pin as an input:
 pinMode(inputPin1, INPUT);    
 pinMode(inputPin2, INPUT);    
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop(){

 if (digitalRead(inputPin1) == LOW) {
     for(pos = 20; pos < 100; pos += 1)  // goes from 0 degrees to 180 degrees
                                   // in steps of 1 degree
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(30);              
   
 }else if  (digitalRead(inputPin2) == LOW){
   
    for(pos = 100; pos>=20; pos-=1)     // goes from 180 degrees to 0 degrees
                               
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(30);        
 }
}

jada

well just increase the delay until you find it slow enough but it can sometimes be jittery

©UnB©

That didnt seemed to work.
Any suggestions?

Groove

What exactly didn't 'seem to work'?
Decide how fast you want to turn, say 180 degrees in 5 seconds, then divide 5 by 180 to get the delay per degree step.
Don't forget to work in milliseconds.
Per Arduino ad Astra

jada

have a look at this
http://arduino.cc/en/Tutorial/Sweep

PaulS

Code: [Select]
     for(pos = 20; pos < 100; pos += 1)  // goes from 0 degrees to 180 degrees
                                   // in steps of 1 degree
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(30);

Without braces, the only command execute in the body of the for loop is the myservo.write() command. Since that is a non-blocking function, the for loop completes very quickly.

You want curly braces around the myservo.write() and delay() calls, so that the servo moves a little, and waits, then move some more, and waits, until it has moved enough.

jezuz

Just to add, you can also change the number of steps per time frame the servo makes, so in your for loop, change pos += 1 to pos += 5 or something. Of course, there's a limit to this, which is the max speed of the servo.

Go Up