Joystick controlling 2 continuous servo motors - change states/ignore X/Y??

Hello -

Using a SumoBot and programming it with Arduino to respond to one joystick. Having issues with the else if statements where the program cant ignore y inputs when its trying to read x inputs, and vice versa. I cant seem to figure out a way to make it so that each direction the joystick points, it moves the motor forward/backward/left/right without freezing up or getting stuck on one of the directions

We’re using a Leonardo, an OSEPP joystick and a SumoBot for the car itself.

here’s our code:

#include <Servo.h>
#include <SoftwareSerial.h>

Servo servoR;
Servo servoL;

int xPin = A1;
int yPin = A0;
int buttonPin = 13;

int x = 0;
int y = 0;
int buttonState = 0;

int prevVal;

void setup() {
  servoR.attach(5);
  servoL.attach(6);
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP); 
  Serial.begin(9600);
}

void forward() {
    servoL.write(180);
    servoR.write(0);
    delay(2000);
}

void reverse() {
    servoL.write(0);
    servoR.write(180);
    delay(2000);

}

void turnLeft() {
    servoL.write(0);
    servoR.write(0);
    delay(750);

}

void turnRight() {
    servoL.write(180);
    servoR.write(180);
    delay(750);
 
}
void Stop() {
    servoL.write(90);
    servoR.write(91);
}

void loop()
{
  x = analogRead(xPin);
  y = analogRead(yPin);
  buttonState = digitalRead(buttonPin);

  
  if(y>600 && y<=1024){
  forward();
  Serial.println("forward");
  }
  else if(y<400 && y>=0){
  reverse();
  Serial.println("reverse");
  }
  else if(x>600 && x<=1024){
  turnRight();
  Serial.println("right");
  }
  else if(x<400 && x>=0){
  turnLeft();
  Serial.println("left");
  }
  else{
  Stop( );
  Serial.println("stop");
  }

  if((x<569 && x>441) && (y<569 && y>441)){
    Stop();
  }

delay(100);

}

Any ideas? Thank you!

nicholas.m.bernstein@gmail.com: Having issues with the else if statements where the program cant ignore y inputs when its trying to read x inputs, and vice versa. I cant seem to figure out a way to make it so that each direction the joystick points, it moves the motor forward/backward/left/right without freezing up or getting stuck on one of the directions

Remake the "else if" statements (might need to even add a couple extra, too) where each of them include conditional statements for BOTH x and y data at the same time. Does that make sense? You'll need to rewrite the code yourself; should be really simple and quick.

nicholas.m.bernstein@gmail.com: void Stop() { servoL.write(90); servoR.write(91); }

You can calibrate continuous rotation servos (with a screwdriver) to both stop at an input of 90. Not necessary, but it would make the code and hardware setup more reliable.

Rather than doing everything at full speed (0 or 180) you can use your analog joysticks for proportional control. The 'Y' control (forward/backward) would control the base speed for both motors. The 'X' control (left/right) would control the offset added to one motor and subtracted from the other.

int X = (analogRead(xPin) - 512) / 6; // Value of +/-85
int Y =  (analogRead(yPin) - 512) / 6; // Value of +/-85

Add or subtract the +/-85 values to 90 and constrain to the allowed range.
int speedLeft  = constrain(90 + Y + X, 0, 180);
int speedRight = constrain(90 + Y - X, 0, 180);

(If it turns the wrong way, swap signs on X. If it goes the wrong direction, change the sign on Y.)