Hi there. This code was written for skid steering, but needs to be altered to achieve dynamic, two-wheel front drive steering (casters in the rear). Basically, I'd like the left-right toggle on the radio controller to decrease the voltage as the toggle is pushed all the way to each side until it reaches 0, once it returns to the center both motors would just resume matching speeds. Maybe this is obvious to you - I'm just trying to turn what I imagine into reality.
The guy who wrote it is unavailable, now, and it's fallen to me, a mechanics guy who is completely new to this world. I've been doing some research, but my head is spinning. Can anyone help me out?
#define MTR 5 //motor right
#define MTL 6 //motor left
#define DIR_R 7 //right direction
#define DIR_L 8 //left direction
#define FWD LOW
#define RVS HIGH
void setup() {
pinMode(10,INPUT); //Channel 1 (steer)
pinMode(11,INPUT); //Channel 3 (throttle)
pinMode(9,INPUT); //Channel 5 (direction)
pinMode(MTR,OUTPUT);
pinMode(MTL,OUTPUT);
pinMode(FWD,OUTPUT);
pinMode(RVS, OUTPUT);
Serial.begin(9600);
}
int steer; //steering number
int throttle; //throttle number
int pwm = 0; // pwm number
int fwd = 1 ;
int direc = analogRead(A0);
void speedFunct(){
//full power
if(pwm >= 250){
analogWrite(MTR,255);
analogWrite(MTL,255);
}
//no power
else if(pwm <= 0){
analogWrite(MTR,0);
analogWrite(MTL,0);
}
//percentage of power (duty cycle)
else{
analogWrite(MTR,pwm);
analogWrite(MTL,pwm);
}
}
void steerFunct(){
//drive straight
if(1450 < steer < 1550){
if(fwd == 1){
digitalWrite(DIR_R, FWD);
digitalWrite(DIR_L, FWD);
}
else{
digitalWrite(DIR_R, RVS);
digitalWrite(DIR_L, RVS);
}
speedFunct();
delay(5);
}
//steer right
if(steer >= 1550){
digitalWrite(DIR_R, RVS);
digitalWrite(DIR_L, FWD);
speedFunct();
delay(5);
}
//steer left
if(steer <= 1450){ // 1400
digitalWrite(DIR_R, FWD);
digitalWrite(DIR_L, RVS);
speedFunct();
delay(5);
}
}
void loop() {
//Pin setup
steer = pulseIn(10, HIGH, 250000);
throttle = pulseIn(11, HIGH, 250000);
direc = pulseIn(9, HIGH, 250000);
Serial.println(throttle);
Serial.println(steer);
/***Direction control***/
//Forward
if(direc <= 1000){
analogWrite(DIR_R, FWD);
analogWrite(DIR_L, FWD);
pwm = map(throttle, 990, 1983 ,0 ,255);
fwd = 1;
steerFunct();
delay(5);
Serial.print("Direction: ");
Serial.println("Forward");
Serial.println("\n");
}
//Reverse
if(direc >= 1900){
digitalWrite(DIR_R, RVS);
digitalWrite(DIR_L, RVS);
pwm = map(throttle, 990, 1983 ,0 ,255);
fwd = 0;
steerFunct();
delay(5);
Serial.print("Direction: ");
Serial.println("Reverse");
Serial.println("\n");
}
//Dead Zone(No movement)
if(1400 < direc && direc < 1500){
//pwm = 0;
//speedFunct();
digitalWrite(MTR, LOW);
digitalWrite(MTL,LOW);
delay(5);
Serial.print("Direction: ");
Serial.println("Stop");
Serial.println("\n");
}
delay(5);
}```