Go Down

Topic: Slowing servos (Read 750 times) previous topic - next topic

GWS46

I am trying to slow servos. I have this code (some kind person has written) but I would like to alter the speed of the servos.
Is this possible?

Many thanks
P.S. I'm really new to the arduino


#include <Servo.h>

Servo servoLeft;              // Define left servo
Servo servoRight;             // Define right servo
int switchPin = 11;           // Set up to read the switch state from digital input 2
int currentSpeed = 1;         // Determines how fast we switch between lights.
int buttonWas = 0;            // The state of the switch (pushed = 1, not pushed = 0) last time we looked
int buttonIs = 0;             // Current state of the switch
unsigned long timer = 0;      // A timer to keep track of where we are.



void setup() {
pinMode(switchPin, INPUT);    // Tells the Arduino to treat the switchPin as INPUT
  servoLeft.attach(6);        // Set left servo to digital pin 10
  servoRight.attach(9);       // Set right servo to digital pin 9
 
}


void getButton() {                     // Let's see what the button is doing, shall we?
//buttonWas = buttonIs;                // Set the old state of the button to be the current state since we're creating a new current state.
buttonIs = digitalRead(switchPin);     // Read the button state
}


void loop() {                        // Loop through motion tests
getButton();
if(buttonIs = 1);




  forward();                         // Example: move forward
  delay(2000);                       // Wait 2000 milliseconds (2 seconds)
  reverse();
  delay(2000);
  turnRight();
  delay(2000);
  turnLeft();
  delay(2000);
  stopRobot();
  delay(2000);
}



// Motion routines for forward, reverse, turns, and stop
void forward() {
  servoLeft.write(30);
  servoRight.write(180);
}

void reverse() {
  servoLeft.write(180);
  servoRight.write(20);
}

void turnRight() {
  servoLeft.write(180);
  servoRight.write(180);
}
void turnLeft() {
  servoLeft.write(30);
  servoRight.write(20);
}

void stopRobot() {
  servoLeft.write(90);
  servoRight.write(90);
}



dxw00d

This thread might be helpful - http://arduino.cc/forum/index.php/topic,61586.0.html

PaulS

Quote
I have this code (some kind person has written) but I would like to alter the speed of the servos.
Is this possible?

Whether you can slow a servo, or not, depends on the type of servo being used. This code appears to be written for continuous rotation servos, which CAN be slowed down or sped up.

The usual approach for continuous rotation servos, which really aren't servos, is to use the writeMicroseconds() method to control them. Some minimum value, generally around 1000, results in maximum speed in one direction. As the value increases, the servo turns slower and slower, until a value is reached, generally around 1500, that causes the servo to stop. As the value continues to increase, the servo starts moving again, in the opposite direction. Continuing to increase the value causes the stepper to go faster, until some maximum value is reached, generally around 2000, above which the servo goes no faster.

http://arduino.cc/en/Reference/ServoWriteMicroseconds

GWS46

Thank you for the help, I'll give both methods a try.

GWS46

Can anyone tell what I have done wrong here? only mysecondservo seems to be moving!

Many thanks


#include "Servo.h"

Servo myservo;                          // create servo object to control a servo, a maximum of eight servo objects can be created
Servo mysecondservo;                    // servo 2

int pos1 = 90;                           // variable to store the servo position
int pos2 = 90;
void setup()
{
myservo.attach(9);                     // attaches the servo on pin 9 to the servo object
mysecondservo.attach(6);               // attaches the servo on pin 6 to the servo object
}


void loop()
{
for(pos1 = 90; pos1 <= 180; pos1 += 1)    // goes from 0 degrees to 90 degrees in steps of 1 degree
{
for(pos2 = 90; pos2 >= 30; pos2 -= 2)

{       
myservo.write(pos1);                      // tell servo to go to position in variable 'pos1'
mysecondservo.write(pos2);                // tell servo to go to position in variable 'pos2'
delay(40);   
}
}
}

PaulS

Code: [Select]
for(pos1 = 90; pos1 <= 180; pos1 += 1)    // goes from 0 degrees to 90 degrees in steps of 1 degree

If you are going to have comments that state the obvious, at least make them correct.

Quote
Can anyone tell what I have done wrong here?

Without knowing how you have powered the servos? No.

PeterH


Can anyone tell what I have done wrong here? only mysecondservo seems to be moving!


Yeah, you've put the loop to move the second servo inside the loop to move the first servo, so it will do a full sweep of the second servo and then move the first servo by one step, and then sweep the second servo again, and so on. I expect you'll find the first servo is moving, just e-x-t-r-e-m-e-l-y slowly.
I only provide help via the forum - please do not contact me for private consultancy.

GWS46

PaulS,

Apologies for being inept!! I'm new to this so forgot to change the comments after tinkering with the code

I'm powering the servos through the Arduino board (5v) and the board is powered from a bench top power supply


PeterH

Many thanks Peter, I'm trying to get them to both move together, i.e. at the same time but I'm obviously doing something silly. Any pointers would be much appreciated.

PeterH


Many thanks Peter, I'm trying to get them to both move together, i.e. at the same time but I'm obviously doing something silly. Any pointers would be much appreciated.


If you want them to move in lockstep, go back to your original code and just move the second servo every time you move the first servo.

If you want them to move independently you need to make some fundamental changes to the structure of your code.
I only provide help via the forum - please do not contact me for private consultancy.

GWS46

Independently is what I'm after which, as I feared, maybe a learning curve too far at this moment in time!

(I probably need some more studying)

PaulS

Quote
Independently is what I'm after which, as I feared, maybe a learning curve too far at this moment in time!

Think about how YOU would move the servos different amounts at different speeds. You'd keep track of the last time you moved a servo, and where you moved it to. Periodically, you'd see if it was time to move each servo again. If so, you'd adjust the position of a servo, and record when you adjusted its position.

"Periodically" on the Arduino means every pass through loop. Time is determined by the millis() function. The other stuff is simple variables and if statements.

Quote
I'm powering the servos through the Arduino board (5v) and the board is powered from a bench top power supply

Servos should be powered by a separate power supply. They draw a lot of current - often more than the Arduino can supply.

Arrch


Independently is what I'm after which, as I feared, maybe a learning curve too far at this moment in time!

(I probably need some more studying)


You should play a look at the Blink Without Delay example. Change things, tweak it, until you are completely comfortable with every aspect of it and doing things like moving servos independently will be relatively simple.

GWS46


Go Up