Go Down

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


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'
 }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'


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


That didnt seemed to work.
Any suggestions?


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


have a look at this


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'

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.
The art of getting good answers lies in asking good questions.


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

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131