Slowing down servo's

Hi Guys
I am new to Arduino (5 Days in :D)

I have put together some code to control a four legged bot (QuadBot). I want to slow the movement down by slowing the servos.

Is this possible without having to re-write all the code? I want to use the Pot4 to control the speed (0 being the fastest and 450 the slowest) and then reverse it when going backwards(ie. 1021 the fastest and 650 the slowest) while still giving the "Forward" or "Backward" instruction throught the Pot4. As soon as the Pot4 returns a "Center" value the servo speed must be reset.

Below is the code I wrote (Only been coding for all of 5 days :grin:), could you please give an example as well.

(The code below does work at a fixed speed. Tested and confirmed.)

#include <Servo.h> 

  //Serov's// 

  Servo FRH;  //Front Right Hip//
  Servo FRK;  //Front Right Knee//              
  Servo BRH;  //Back Right Hip//
  Servo BRK;  //Back Right Knee//
  Servo FLH;  //Front Left Hip//
  Servo FLK;  //Front Left Knee//
  Servo BLH;  //Back Left Hip//
  Servo BLK;  //Back Left Knee//

  //Local Constants and Variables//

  const int Pot4 = A3; //Forwards Backwards - Left Pot//

  const int Pot4C = 546; //The center position of the pot.( 0 - 1021 Range)//

  int Speed=50;       //The delay between movements//
  int Swing=500;      //The amount of LATERAL movement//
  int Lift=300;       //The amount the VERTICAL movement//
  int Stretch=500;    //Knee movement for the stretch//
  int Val4;            //Used to store controller position//
  
  int FLHcenter = 1350;
  int FRHcenter = 1350;
  int BLHcenter = 1350;
  int BRHcenter = 1300;
  int FLKcenter = 1500;  //These are the servo centering positions read from Constants.h//
  int FRKcenter = 1350;
  int BLKcenter = 1150;
  int BRKcenter = 1200;
  
  
void setup() { 
  
  Serial.begin(9600);

  //Attach Seros to PWM Outputs - The MEGA//

  FLK.attach(2);   //FLK	RED//
  FRH.attach(3);   //FRH	PURPLE//
  BRH.attach(4);   //BRH	BLACK//
  BLH.attach(5);   //BLH	GREEN//
  FLH.attach(6);   //FLH	BLUE//
  FRK.attach(7);   //FRK	BROWN//
  BLK.attach(8);   //BLK	YELLOW//
  BRK.attach(9);   //BRK	WHITE//

  // Centering the Servo's//
  
  FLH.writeMicroseconds(FLHcenter);  //Front Left Hip//                    
  FRH.writeMicroseconds(FRHcenter);  //Front Right Hip//
  BLH.writeMicroseconds(BLHcenter);  //Back Left Hip//
  BRH.writeMicroseconds(BRHcenter);  //Back Right Hip//
  FLK.writeMicroseconds(FLKcenter);  //Front Left Knee//
  FRK.writeMicroseconds(FRKcenter);  //Front Right Knee//
  BLK.writeMicroseconds(BLKcenter);  //Back Left Knee//
  BRK.writeMicroseconds(BRKcenter);  //Back Right Knee//

  delay(1000);
}
void loop(){

  analogRead(Pot4); //A3 Left Thumb Frowards-Backwards//
  Val4 = analogRead(Pot4);

  if (Val4 < (Pot4C-2)){
    Forwards();
   }
  else if (Val4 > (Pot4C+20)){
    Backwards();
  }
  else {
    Center();
  }
   delay(10);
}

void Center(){
    
    FLH.writeMicroseconds(FLHcenter);                      
    FRH.writeMicroseconds(FRHcenter);  
    BLH.writeMicroseconds(BLHcenter);  
    BRH.writeMicroseconds(BRHcenter);  
    FLK.writeMicroseconds(FLKcenter);  
    FRK.writeMicroseconds(FRKcenter);  
    BLK.writeMicroseconds(BLKcenter);  
    BRK.writeMicroseconds(BRKcenter);
  }
  
  void Forwards(){
  
    FLK.writeMicroseconds(FLKcenter-Lift);
    BRK.writeMicroseconds(BRKcenter-Lift);
    delay(Speed);
    FLH.writeMicroseconds(FLHcenter+Swing);
    BRH.writeMicroseconds(BRHcenter-Swing);
    delay(Speed);
    FLK.writeMicroseconds(FLKcenter);
    BRK.writeMicroseconds(BRKcenter);
    delay(Speed);
    FLH.writeMicroseconds(FLHcenter);
    BRH.writeMicroseconds(BRHcenter);
    delay(Speed);
    FRK.writeMicroseconds(FRKcenter-Lift);
    BLK.writeMicroseconds(BLKcenter-Lift);
    delay(Speed);
    FRH.writeMicroseconds(FRHcenter-Swing);
    BLH.writeMicroseconds(BLHcenter+Swing);
    delay(Speed);
    FRK.writeMicroseconds(FRKcenter);
    BLK.writeMicroseconds(BLKcenter);
    delay(Speed);
    FRH.writeMicroseconds(FRHcenter);
    BLH.writeMicroseconds(BLHcenter);
    delay(Speed);
  }
  
  void Backwards(){
    
    FLK.writeMicroseconds(FLKcenter-Lift);
    BRK.writeMicroseconds(BRKcenter-Lift);
    delay(Speed);
    FLH.writeMicroseconds(FLHcenter-Swing);
    BRH.writeMicroseconds(BRHcenter+Swing);
    delay(Speed);
    FLK.writeMicroseconds(FLKcenter);
    BRK.writeMicroseconds(BRKcenter);
    delay(Speed);
    FLH.writeMicroseconds(FLHcenter);
    BRH.writeMicroseconds(BRHcenter);
    delay(Speed);
    FRK.writeMicroseconds(FRKcenter-Lift);
    BLK.writeMicroseconds(BLKcenter-Lift);
    delay(Speed);
    FRH.writeMicroseconds(FRHcenter+Swing);
    BLH.writeMicroseconds(BLHcenter-Swing);
    delay(Speed);
    FRK.writeMicroseconds(FRKcenter);
    BLK.writeMicroseconds(BLKcenter);
    delay(Speed);
    FRH.writeMicroseconds(FRHcenter);
    BLH.writeMicroseconds(BLHcenter);
    delay(Speed);
    
  }

Thanks, any help would be appreciated.

Have you tried adjusting the 'Speed' variable based on the value of the Pot? Basically decrease it as you move away from the center?

It seems to me you'd want the speed connected to the pot value, so use a formula to calculate the delay time rather than a fixed value - and the forward speed would seem to be reversed from what you posted (0 = fastest, and speed can be an integer defined as you have it so you can calibrate the slowest speed). I say that because it seems the pot would be centered for zero speed (512), and as you move away from center, speed should get faster with the fastest being at the 0 and 1023 ends.

replace "delay(Speed);" with

in forward function:
delay((Val4+1)/Speed);

in reverse function:
delay((Val4-512)/Speed);

Change Speed = 10 or lower depending on how slow you want it. This gives you a delay range of ~ 0 to 50 ms.

You can change the formula to suit your objective.

P.S., why is center 546? You might need to adjust the formula if that is where center actually ends up. You might also have to add a few ms if fastest is too fast.

Hi mirith & C2

Assigning the "Speed" variable isn't going to slow the servo's down, sorry for the misunderstanding. What I wish to achieve would be increasing the time it takes the servo to move form the center position to the position after the movement and returning to the center.

Code Below

FLK.writeMicroseconds(FLKcenter); //The time it takes to move form here-//
FLK.writeMicroseconds(FLKcenter-Lift); //To here.//

So slower individual servo movements.

Thanks

The only way to slow servos down is to mathematically interpolate the values in between
the initial and final positions, and then send a new pulse width value every update period
or so [ie, 20-msec].

The below might be worth a look.

http://forum.arduino.cc/index.php/topic,61586.30.html