combining obstacle-avoidance robot with joystick-controlled robot

hello. i am trying to make my robot car, essentially, change modes using 2 HC-05 Bluetooth modules. I have code that will make the car an obstacle-avoidance bot and code that can be controlled with a joystick. I got both codes to work separately; my idea was to use the digital pin sensor on the joystick to control the mode. So, for example, the bot will start as the joystick-controlled bot. When I press down on the joystick from my other arduino, a counter will keep track of how many times I pressed the joystick. That counter number would be sent to the bot, and will switch to the code that makes it into an obstacle-avoidance bot. Here is all the code:

MASTER CODE:

int xAxis, yAxis;
int flag;
const int pin = 2;
void setup() {
  pinMode(pin, INPUT);
  digitalWrite(pin, HIGH);
  Serial.begin(38400); 
}
void loop() {
  xAxis = analogRead(A0); // Read Joysticks X-axis
  yAxis = analogRead(A1); // Read Joysticks Y-axis
  if(digitalRead(pin) % 2 == 0){
    if(flag == 9){
      flag = 1;
    }
    flag = flag + 1;
  }
  delay(200);
  // Send the values via the serial port to the slave HC-05 Bluetooth device
  Serial.write(xAxis/4); 
  Serial.write(yAxis/4);
  delay(200);
  Serial.write((int)flag);
  delay(10);
}

SLAVE/ROBOT CODE:

#define echopin  13 // echo pin
#define trigpin 12 // Trigger pin
#define enA 5
#define in1 7
#define in2 8
#define enB 6
#define in3 9
#define in4 10

int maximumRange = 30;
long duration, distance;

int xAxis, yAxis;
unsigned int  x = 0;
unsigned int  y = 0;
int motorSpeedA = 0;
int motorSpeedB = 0;
int new_flag;


void setup() {
  pinMode (trigpin, OUTPUT);
  pinMode (echopin, INPUT );
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  Serial.begin(38400); 
void loop() {
  
    // Read the incoming data 
    while (Serial.available()) {
      x = Serial.read();
      delay(10);
      y = Serial.read();
      delay(10);
      new_flag = Serial.read();
    }
    delay(10);


    if(new_flag % 2 == 0){ // CODE FOR WHEN BUTTON WAS NOT PRESSED, JOYSTICK-CONTROLLED BOT
      xAxis = x*4;
      yAxis = y*4;
      // Y-axis used for forward and backward control
      if (yAxis < 470) {
        // Set Motor A backward
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
        // Set Motor B backward
        digitalWrite(in3, HIGH);
        digitalWrite(in4, LOW);
        
        motorSpeedA = map(yAxis, 470, 0, 0, 255);
        motorSpeedB = map(yAxis, 470, 0, 0, 255);
      }
      else if (yAxis > 550) {
        // Set Motor A forward
        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
        // Set Motor B forward
        digitalWrite(in3, LOW);
        digitalWrite(in4, HIGH);
        
        motorSpeedA = map(yAxis, 550, 1023, 0, 255);
        motorSpeedB = map(yAxis, 550, 1023, 0, 255);
      }
      // If joystick stays in middle the motors are not moving
      else {
        motorSpeedA = 0;
        motorSpeedB = 0;
      }
      // X-axis used for left and right control
      if (xAxis < 470) {
        
        int xMapped = map(xAxis, 470, 0, 0, 255);
        // Move to left - decrease left motor speed, increase right motor speed
        motorSpeedA = motorSpeedA - xMapped;
        motorSpeedB = motorSpeedB + xMapped;
        // Confine the range from 0 to 255
        if (motorSpeedA < 0) {
          motorSpeedA = 0;
        }
        if (motorSpeedB > 255) {
          motorSpeedB = 255;
        }
      }
      if (xAxis > 550) {
       
        int xMapped = map(xAxis, 550, 1023, 0, 255);
        // Move right - decrease right motor speed, increase left motor speed
        motorSpeedA = motorSpeedA + xMapped;
        motorSpeedB = motorSpeedB - xMapped;
        // Confine the range from 0 to 255
        if (motorSpeedA > 255) {
          motorSpeedA = 255;
        }
        if (motorSpeedB < 0) {
          motorSpeedB = 0;
        }
      }
      // 
      if (motorSpeedA < 70) {
        motorSpeedA = 0;
      }
      if (motorSpeedB < 70) {
        motorSpeedB = 0;
      }
      analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
      analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
    }

  else{ //CODE FOR WHEN BUTTON IS PRESSED, OBSTACLE-AVOIDANCE BOT
    {
      digitalWrite(trigpin,LOW);
      delayMicroseconds(2);
      
      digitalWrite(trigpin,HIGH);
      delayMicroseconds(10);
      
      duration=pulseIn (echopin,HIGH);
      
      distance= duration/58.2;
      delay (50);
      Serial.println(distance);
  
 
  analogWrite(enA, 240);
  analogWrite(enB, 255);
  if (distance <= 35 ){
    digitalWrite (in1, LOW);
    digitalWrite (in2, LOW);
    digitalWrite (in3,LOW);
    digitalWrite (in4,LOW);
    delay (200);
    digitalWrite(in1,LOW);
    digitalWrite(in3,HIGH);
    digitalWrite(in2,HIGH);
    digitalWrite(in4,LOW);
    delay (500);
  }
  else{
   digitalWrite(in1,HIGH);
   digitalWrite(in3,HIGH);
   digitalWrite(in2,LOW);
   digitalWrite(in4,LOW);
   delay (10);
  }
 }
  }  
}

It looks like from my tests that the joystick works fine, but the flag value is not being sent. any help would be appreciated. thank you

Why

does

the

master

code

delay

between

writes

to

the

serial

port?

You are sending, eventually, after all the f*king around, 3 bytes. Then, as soon as one arrives, you start reading all three.

That is just NEVER going to work, no matter how long you wait between reads.

Read Robin2's serial input basics thread, and send and receive the data properly.
http://forum.arduino.cc/index.php?topic=396450.0

OK, thank you. Based on the info in that thread, I want to try using Example 5, but I need to convert my integer values. Should I convert them as string or use an array and just add the numbers into that array? Thank you.

Should I convert them as string

That's what example 5 expects, so that seems like a good idea.