Go Down

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

#### unb

##### Dec 18, 2010, 02:32 pm
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);          }}`

#### newman

#1
##### Dec 18, 2010, 02:46 pm
well just increase the delay until you find it slow enough but it can sometimes be jittery

#### unb

#2
##### Dec 18, 2010, 05:00 pm
That didnt seemed to work.
Any suggestions?

#### GrooveFlotilla

#3
##### Dec 18, 2010, 05:05 pm
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.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.

#### newman

#4
##### Dec 18, 2010, 05:06 pm
have a look at this
http://arduino.cc/en/Tutorial/Sweep

#### PaulS

#5
##### Dec 18, 2010, 11:19 pm
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.