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 ), 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.