Pages: [1]   Go Down
Author Topic: Adjusting servo speed  (Read 4691 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:  :smiley
Code:
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);        
  }
}
Logged

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That didnt seemed to work.
Any suggestions?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Per Arduino ad Astra

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
     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.
Logged

New York
Offline Offline
Edison Member
*
Karma: 1
Posts: 1023
E != m*c^2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: