Merged code doesn't work properly

Hi, I have merged 3 simple codes which all worked by themselves but when combined the distance sensor values are all false and are likely random (though some stay consistent).
I'm quite new at arduino so there is likely stuff to do when combining codes that I don't know about,

1- sweeps with 2 servos
2-turns of and on a relay when object is close
3- measuring 4 distances with DYP L04 underwater sensors

Here is the code :


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

//--------------------------------Initialization Block--------------------------------------------
// Initialization for distance measurement functions-----
#define COM1 0x55
#define COM2 0x56
#define COM3 0x57
#define COM4 0x58

int distance1 = 0; 
int distance2 = 0;
int distance3 = 0;
int distance4 = 0;
unsigned char buffer_RTT[4];
uint8_t CS;
//------------------------------------------------------------------------

// Initialization for servo motors-----------------------------------
Servo myServo1; // Creates a servo object for controlling the servo motor
Servo myServo2; // Creates a servo object for controlling the servo motor
//------------------------------------------------------------------------



void setup() {

  Serial.begin(115200);

  myServo1.attach(4); // Defines the horizontal servo motor connected to pin 4
  myServo2.attach(5);  // Defines the vertical servo motor connected to pin 5

  pinMode(3, OUTPUT); // Relay connected to pin 3
}






// --------------------------------------Main Loop-----------------------------------------------------------------

void loop() {


  for (int i = 0; i < 160; i=i+3) { // Loop for horizontal movement
    myServo1.write(i); // Sends the control pulse to the motor to set its angle


    for (int j = 40; j <= 160; j=j+3){ // Loop to raise the servo motor 
      myServo2.write(j);
      delay(30);
      obstacle_detection(); // Calls the obstacle detection function during the upward movement of the sensor block (from left to right)
    }
  i=i+3; // Rotates the servo to the right
  myServo1.write(i);

    for (int j = 160; j > 40; j=j-3){ // Loop to lower the servo motor
      myServo2.write(j);
      delay(30);
      obstacle_detection(); // Calls the obstacle detection function during the downward movement of the sensor block       
    }
  }


  for (int i = 160; i > 0; i=i-3) { //Loop to mirror what has been done above
    myServo1.write(i); 

    for (int j = 40; j <= 160; j=j+3){ // Loop to raise the servo motor 
      myServo2.write(j);
      delay(30);
      obstacle_detection(); // Calls the obstacle detection function during the upward movement of the sensor block (from right to left)
    }
  
  i=i-3; // Rotates the servo to the left
  myServo1.write(i);

  for (int j = 160; j > 40; j=j-3){ // Loop to lower the servo motor
      myServo2.write(j);
      delay(30);
      obstacle_detection(); // Calls the obstacle detection function during the upward movement of the sensor block (from right to left)      
    }

  }
  

}
//---------------------------------------------------------------------------------------------------------------------------




//----------------- Obstacle Detection Function-------------------------
void obstacle_detection() {


  //distance retrieval 
  int distance1bis = readSensor1();
    Serial.print("Distance from Sensor 1: ");
    Serial.print(distance1bis);
    Serial.println(" mm");
  int distance2bis = readSensor2();
    Serial.print("Distance from Sensor 2: ");
    Serial.print(distance2bis);
    Serial.println(" mm");
  int distance3bis = readSensor3();
    Serial.print("Distance from Sensor 3: ");
    Serial.print(distance3bis);
    Serial.println(" mm");
  int distance4bis = readSensor4();
    Serial.print("Distance from Sensor 4: ");
    Serial.print(distance4bis);
    Serial.println(" mm");
  
  //Action based on distance

  if(distance1bis < 200 || distance2bis < 200 || distance3bis < 200||  distance4bis  < 200 ){

    if(distance1bis < 50 || distance2bis < 50 || distance3bis < 50||  distance4bis  < 50 ){ // If we are at a critical distance
      digitalWrite(3, HIGH); // Turns on the relay, cutting power to motors
      delay(3000); // Wait a bit
      digitalWrite(3, LOW); // Turns the motors back on
 
    }
    else { // If we are at a close but not critical distance, notify the user via vibration
      for (int i = 0; i <= 200; i++){
        digitalWrite(3, HIGH);
        delay(50);
        digitalWrite(3, LOW);
        delay(50);
      }
    }

  }




}

//------------------------------------------------------------------------






//------------------------ Distance Functions (BLOCK C) -----------------------

int readSensor1() {
  SoftwareSerial mySerial1(53,52 );
  mySerial1.begin(115200);
  mySerial1.write(COM1);
  delay(50);
  if (mySerial1.available() > 0) {
    delay(10);
    if (mySerial1.read() == 0xff) {
      buffer_RTT[0] = 0xff;
      for (int i = 1; i < 4; i++) {
        buffer_RTT[i] = mySerial1.read();
      }
      CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
      if (buffer_RTT[3] == CS) {
        distance1 = (buffer_RTT[1] << 8) + buffer_RTT[2];
        
        return distance1;
      }
    }
  }
}

int readSensor2() {
  SoftwareSerial mySerial2(49,48);
  mySerial2.begin(115200);
  mySerial2.write(COM2);
  delay(50);
  if (mySerial2.available() > 0) {
    delay(10);
    if (mySerial2.read() == 0xff) {
      buffer_RTT[0] = 0xff;
      for (int i = 1; i < 4; i++) {
        buffer_RTT[i] = mySerial2.read();
      }
      CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
      if (buffer_RTT[3] == CS) {
        distance2 = (buffer_RTT[1] << 8) + buffer_RTT[2];
        
        return distance2;
      }
    }
  }
}

int readSensor3() {
  SoftwareSerial mySerial3(45,44);
  mySerial3.begin(115200);
  mySerial3.write(COM3);
  delay(50);
  if (mySerial3.available() > 0) {
    delay(10);
    if (mySerial3.read() == 0xff) {
      buffer_RTT[0] = 0xff;
      for (int i = 1; i < 4; i++) {
        buffer_RTT[i] = mySerial3.read();
      }
      CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
      if (buffer_RTT[3] == CS) {
        distance3 = (buffer_RTT[1] << 8) + buffer_RTT[2];
        
        return distance3;
      }
    }
  }
}

int readSensor4() {
  SoftwareSerial mySerial4(39,38);
  mySerial4.begin(115200);
  mySerial4.write(COM4);
  delay(50);
  if (mySerial4.available() > 0) {
    delay(10);
    if (mySerial4.read() == 0xff) {
      buffer_RTT[0] = 0xff;
      for (int i = 1; i < 4; i++) {
        buffer_RTT[i] = mySerial4.read();
      }
      CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
      if (buffer_RTT[3] == CS) {
        distance4 = (buffer_RTT[1] << 8) + buffer_RTT[2];
        
        return distance4;
      }
    }
  }
}
//------------------------------------------------------------------------

Can you help me get the code to work please ?

Should I also put the 3 seperate codes ?

Thanks ahead for your help.

what's your distance sensor?
can you share the circuit as it stands? (and powers)

I'm using DYP-L04 sensors using UART

The circuit is :
2 servos plugged in pwm on the pwm ports of the arduino
4 (3x 3m and 1x 6 meter range) distance sensors plugged into the Digitial ports
1 relay plugged in a digital port.

All power is provided through the arduino (directly plugged in through a breadboard)
I wasn't able to find power consumption of the sensors on datasheet.

With strictly nothing plugged into the arduino I get random distance values (I usually should get 0 when they aren't plugged in)

You don't need to use a PWM port to drive a servo, any pin is capable of driving one.

The Arduino is not capable of providing the current for one, let alone two servos.

Servos == external supply.

So we need to see a schematic of how it is wired, a link to the data sheet of your sensor.

You do not appear to be setting the software serial pins in your setup function.

Please provide a circuit diagram and the datasheet for the DYP L04.

Which Arduino do you have?

I suspect that Servo and SoftwareSerial are fighting for the same timer.

i've always had problems using Software Serial.

  • doubt begin() should be called multiple times. call it in setup()
  • believe it has a limited speed, 115200 may be too high
  • see example

Here is th datasheet : L04 Datasheet.pdf (4,7 Mo)

Here are the pins :

This is how I plugged the 4 in (I switched from the mega back to a UNO as it stopped working altogether on the mega) :

Couldn't find the proper sensors in tinkercad :

I have removed the multiple begins and only kept the one in the top but that doesn't work.

This is the code for the distance sensors that works :

#include <SoftwareSerial.h>

#define COM1 0x55
#define COM2 0x56
#define COM3 0x57
#define COM4 0x58

int distance1 = 0;
int distance2 = 0;
int distance3 = 0;
int distance4 = 0;
unsigned char buffer_RTT[4];
uint8_t CS;

void setup() {
  Serial.begin(115200);
}

void loop() {
  //Serial.println(" mm");
  readSensor1();
  readSensor2();
  readSensor3();
  readSensor4();
}

void readSensor1() {
  SoftwareSerial mySerial1(6,7); // Fils blanc Ă  gauche (sur connecteur 8)
  //mySerial1.begin(115200);
  mySerial1.write(COM1);
  delay(50);
  if (mySerial1.available() > 0) {
    delay(10);
    if (mySerial1.read() == 0xff) {
      buffer_RTT[0] = 0xff;
      for (int i = 1; i < 4; i++) {
        buffer_RTT[i] = mySerial1.read();
      }
      CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
      if (buffer_RTT[3] == CS) {
        distance1 = (buffer_RTT[1] << 8) + buffer_RTT[2];
        Serial.print("Distance from Sensor 1: ");
        Serial.print(distance1);
        Serial.println(" mm");
      }
    }
  }
}

void readSensor2() {
  SoftwareSerial mySerial2(8,9); // fils blanc sur 10
  //mySerial2.begin(115200);
  mySerial2.write(COM2);
  delay(50);
  if (mySerial2.available() > 0) {
    delay(10);
    if (mySerial2.read() == 0xff) {
      buffer_RTT[0] = 0xff;
      for (int i = 1; i < 4; i++) {
        buffer_RTT[i] = mySerial2.read();
      }
      CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
      if (buffer_RTT[3] == CS) {
        distance2 = (buffer_RTT[1] << 8) + buffer_RTT[2];
        Serial.print("Distance from Sensor 2: ");
        Serial.print(distance2);
        Serial.println(" mm");
      }
    }
  }
}

void readSensor3() {
  SoftwareSerial mySerial3(10,11);
  //mySerial3.begin(115200);
  mySerial3.write(COM3);
  delay(50);
  if (mySerial3.available() > 0) {
    delay(10);
    if (mySerial3.read() == 0xff) {
      buffer_RTT[0] = 0xff;
      for (int i = 1; i < 4; i++) {
        buffer_RTT[i] = mySerial3.read();
      }
      CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
      if (buffer_RTT[3] == CS) {
        distance3 = (buffer_RTT[1] << 8) + buffer_RTT[2];
        Serial.print("Distance from Sensor 3: ");
        Serial.print(distance3);
        Serial.println(" mm");
      }
    }
  }
}

void readSensor4() {
  SoftwareSerial mySerial4(12,13);
  //mySerial4.begin(115200);
  mySerial4.write(COM4);
  delay(50);
  if (mySerial4.available() > 0) {
    delay(10);
    if (mySerial4.read() == 0xff) {
      buffer_RTT[0] = 0xff;
      for (int i = 1; i < 4; i++) {
        buffer_RTT[i] = mySerial4.read();
      }
      CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
      if (buffer_RTT[3] == CS) {
        distance4 = (buffer_RTT[1] << 8) + buffer_RTT[2];
        Serial.print("Distance from Sensor 4: ");
        Serial.print(distance4);
        Serial.println(" mm");
      }
    }
  }
}

The baud rate of 115200 is on the datasheet

I was using an uno then switched to a mega.

Both worked fine for the 4 distance sensors but both don't work for the combination of codes.

Now the mega doesn't work with the distance only code (the week prior it did) so I switched back to the uno for now.

did you just read one sensor in the code that worked? or did you also read 4?

presumably you also moved the definition of the serial interface to be global and not dynamic within the function

Several disjoint observations:

  • If the Mega worked before, doesn't now, download the blink.ino test code to make sure you didn't damage it with the two servos as loads
  • have you attached an external power supply for the servos, and if not, why not? This is an essential step
  • if you have added a power supply, make sure it's (-)(GND) side is connected to the Arduino's GND. This is necessary as the pulse outputs of the Arduino need to be referenced to the GND of both the Arduino, and the Servos.
  • If your Mega is working, I strongly suggest you focus on getting this working with the Mega, as there is no need for fussing around with Software Serial there - just use Serial1, Serial2, or Serial3, with appropriate pin selection of course.
    Hope that sets you on the path to success.

I only erased this from every function : mySerial1.begin(115200)

I didn't touch mySerial

In this code :


#define COM1 0x55
#define COM2 0x56
#define COM3 0x57
#define COM4 0x58

int distance1 = 0;
int distance2 = 0;
int distance3 = 0;
int distance4 = 0;
unsigned char buffer_RTT[4];
uint8_t CS;

void setup() {
 Serial.begin(115200);
}

void loop() {
 readSensor1();
 readSensor2();
 readSensor3();
 readSensor4();
}

void readSensor1() {
 SoftwareSerial mySerial1(8, 9); // Fils blanc Ă  gauche (sur connecteur 8)
 mySerial1.begin(115200);
 mySerial1.write(COM1);
 delay(50);
 if (mySerial1.available() > 0) {
   delay(10);
   if (mySerial1.read() == 0xff) {
     buffer_RTT[0] = 0xff;
     for (int i = 1; i < 4; i++) {
       buffer_RTT[i] = mySerial1.read();
     }
     CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
     if (buffer_RTT[3] == CS) {
       distance1 = (buffer_RTT[1] << 8) + buffer_RTT[2];
       Serial.print("Distance from Sensor 1: ");
       Serial.print(distance1);
       Serial.println(" mm");
     }
   }
 }
}

void readSensor2() {
 SoftwareSerial mySerial2(10, 11); // fils blanc sur 10
 mySerial2.begin(115200);
 mySerial2.write(COM2);
 delay(50);
 if (mySerial2.available() > 0) {
   delay(10);
   if (mySerial2.read() == 0xff) {
     buffer_RTT[0] = 0xff;
     for (int i = 1; i < 4; i++) {
       buffer_RTT[i] = mySerial2.read();
     }
     CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
     if (buffer_RTT[3] == CS) {
       distance2 = (buffer_RTT[1] << 8) + buffer_RTT[2];
       Serial.print("Distance from Sensor 2: ");
       Serial.print(distance2);
       Serial.println(" mm");
     }
   }
 }
}

void readSensor3() {
 SoftwareSerial mySerial3(12, 13);
 mySerial3.begin(115200);
 mySerial3.write(COM3);
 delay(50);
 if (mySerial3.available() > 0) {
   delay(10);
   if (mySerial3.read() == 0xff) {
     buffer_RTT[0] = 0xff;
     for (int i = 1; i < 4; i++) {
       buffer_RTT[i] = mySerial3.read();
     }
     CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
     if (buffer_RTT[3] == CS) {
       distance3 = (buffer_RTT[1] << 8) + buffer_RTT[2];
       Serial.print("Distance from Sensor 3: ");
       Serial.print(distance3);
       Serial.println(" mm");
     }
   }
 }
}

void readSensor4() {
 SoftwareSerial mySerial4(6, 7);
 mySerial4.begin(115200);
 mySerial4.write(COM4);
 delay(50);
 if (mySerial4.available() > 0) {
   delay(10);
   if (mySerial4.read() == 0xff) {
     buffer_RTT[0] = 0xff;
     for (int i = 1; i < 4; i++) {
       buffer_RTT[i] = mySerial4.read();
     }
     CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
     if (buffer_RTT[3] == CS) {
       distance4 = (buffer_RTT[1] << 8) + buffer_RTT[2];
       Serial.print("Distance from Sensor 4: ");
       Serial.print(distance4);
       Serial.println(" mm");
     }
   }
 }
}

I read the 4 sensors and it works properly

you should. that creates a new instance of the interface each time the function is called

corrected - i don't know how you can call mySerial1.begin() in setup() without mySerial1 being defined

1 Like

Look at the examples, look at the code of others. Where do you see the connection being recreated every time a read is done?

your functions opens the SoftwareSerial port and fetch the data (making some timing assumptions which is never good for an asynchronous protocol)

As the SoftwareSerial variable is local to the function it gets tossed at the end of the function.

for good measure, I would still call mySerialx.end(). before the end of the function just to terminate "correctly"

void readSensor1() {
 SoftwareSerial mySerial1(8, 9); // Fils blanc Ă  gauche (sur connecteur 8)
 mySerial1.begin(115200);
 mySerial1.write(COM1);
 delay(50);
 if (mySerial1.available() > 0) {
   delay(10);
   if (mySerial1.read() == 0xff) {
     buffer_RTT[0] = 0xff;
     for (int i = 1; i < 4; i++) {
       buffer_RTT[i] = mySerial1.read();
     }
     CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
     if (buffer_RTT[3] == CS) {
       distance1 = (buffer_RTT[1] << 8) + buffer_RTT[2];
       Serial.print("Distance from Sensor 1: ");
       Serial.print(distance1);
       Serial.println(" mm");
     }
   }
 }
 mySerial1.end(); // <=== end "properly" the virtual UART 
}

On a mega you have 3 free hardware serial (Serial1 - Serial3). Please test with these.
Serial however is shared with USB. For the last sensor you probably still need to use SoftwareSerial.

SoftwareSerial has a few limitations on the pins that can receive.
See https://docs.arduino.cc/learn/built-in-libraries/software-serial/

I added the mySerial end :


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


//--------------------------------Bloc initialisation--------------------------------------------
// Initialisation pour les fonctions de mesure de distance (BLOC C)-----
#define COM1 0x55
#define COM2 0x56
#define COM3 0x57
#define COM4 0x58

int distance1 = 0; 
int distance2 = 0;
int distance3 = 0;
int distance4 = 0;
unsigned char buffer_RTT[4];
uint8_t CS;
//------------------------------------------------------------------------

//Initialisation pour les servo moteurs-----------------------------------
Servo myServo1; // Creates a servo object for controlling the servo motor
Servo myServo2; // Creates a servo object for controlling the servo motor
//------------------------------------------------------------------------



void setup() {

 Serial.begin(115200);

 myServo1.attach(4); // Defini le servo moteur horizontal branché sur le pin 4
 myServo2.attach(5);  // Défini le servo moteur vertical branché sur le pin 5

 pinMode(3, OUTPUT); // Relais branché sur le pin 3
}






// --------------------------------------Boucle principale-----------------------------------------------------------------

void loop() {


 for (int i = 0; i < 160; i=i+3) { // Boucle pour le déplacement horizontal
   myServo1.write(i); //On envoit l'impulsion de contrĂ´le au moteur pour lui donner son angle


   for (int j = 40; j <= 160; j=j+3){ // Boucle pour faire monter le servo moteur 
     myServo2.write(j);
     delay(30);
     detection_obstacle(); // On appelle la fonction détection d'obstacle pendant la montée du bloc capteurs (en allant de la gauche vers la droite)
   }
 i=i+3; // On fait tourner le servo vers la droite
 myServo1.write(i);

   for (int j = 160; j > 40; j=j-3){ // Boucle pour faire descendre le servo moteur
     myServo2.write(j);
     delay(30);
     detection_obstacle(); // On appelle la fonction détection d'obstacle pendant la descente du bloc capteurs       
   }
 }


 for (int i = 160; i > 0; i=i-3) { //Boucle pour faire la symètrie de ce qui a été fait au dessus
   myServo1.write(i); 

   for (int j = 40; j <= 160; j=j+3){ // Boucle pour faire monter le servo moteur 
     myServo2.write(j);
     delay(30);
     detection_obstacle(); // On appelle la fonction détection d'obstacle pendant la montée du bloc capteurs (en allant de la droite vers la gauche)
   }
 
 i=i-3; // On fait tourner le servo vers la gauche
 myServo1.write(i);

 for (int j = 160; j > 40; j=j-3){ // Boucle pour faire descendre le servo moteur
     myServo2.write(j);
     delay(30);
     detection_obstacle(); // On appelle la fonction détection d'obstacle pendant la montée du bloc capteurs (en allant de la droite vers la gauche)      
   }

 }
 

}
//---------------------------------------------------------------------------------------------------------------------------




//----------------- Fonction détection obstacle-------------------------
void detection_obstacle() {


 //récupération des distance 
 int distance1bis = readSensor1();
   Serial.print("Distance from Sensor 1: ");
   Serial.print(distance1bis);
   Serial.println(" mm");
 int distance2bis = readSensor2();
   Serial.print("Distance from Sensor 2: ");
   Serial.print(distance2bis);
   Serial.println(" mm");
 int distance3bis = readSensor3();
   Serial.print("Distance from Sensor 3: ");
   Serial.print(distance3bis);
   Serial.println(" mm");
 int distance4bis = readSensor4();
   Serial.print("Distance from Sensor 4: ");
   Serial.print(distance4bis);
   Serial.println(" mm");
 
 //Agissement en fonction de la distance

 if(distance1bis < 200 || distance2bis < 200 || distance3bis < 200||  distance4bis  < 200 ){

   if(distance1bis < 50 || distance2bis < 50 || distance3bis < 50||  distance4bis  < 50 ){ // Si nous somme Ă  une distance limite
     digitalWrite(3, HIGH); // On allume le relais donc on coupe l'allimentation du Romarin
     delay(3000); // On attend un peu
     digitalWrite(3, LOW); // On rallume le Romarin

   }
   else { // Si nous sommes Ă  une distance limite mais pas trop proche on avertit l'utilisateur via une vibration
     for (int i = 0; i <= 200; i++){
       digitalWrite(3, HIGH);
       delay(50);
       digitalWrite(3, LOW);
       delay(50);
     }
   }

 }




}

//------------------------------------------------------------------------






//------------------------ Fonctions de distance (BLOC C) -----------------------

int readSensor1() {
 SoftwareSerial mySerial1(6, 7);
 mySerial1.begin(115200);
 mySerial1.write(COM1);
 delay(50);
 if (mySerial1.available() > 0) {
   delay(10);
   if (mySerial1.read() == 0xff) {
     buffer_RTT[0] = 0xff;
     for (int i = 1; i < 4; i++) {
       buffer_RTT[i] = mySerial1.read();
     }
     CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
     if (buffer_RTT[3] == CS) {
       distance1 = (buffer_RTT[1] << 8) + buffer_RTT[2];
       
       return distance1;
     }
   }
 }
 mySerial1.end(); // <=== end "properly" the virtual UART
}

int readSensor2() {
 SoftwareSerial mySerial2(8, 9);
 mySerial2.begin(115200);
 mySerial2.write(COM2);
 delay(50);
 if (mySerial2.available() > 0) {
   delay(10);
   if (mySerial2.read() == 0xff) {
     buffer_RTT[0] = 0xff;
     for (int i = 1; i < 4; i++) {
       buffer_RTT[i] = mySerial2.read();
     }
     CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
     if (buffer_RTT[3] == CS) {
       distance2 = (buffer_RTT[1] << 8) + buffer_RTT[2];
       
       return distance2;
     }
   }
 }
 mySerial2.end(); // <=== end "properly" the virtual UART
}

int readSensor3() {
 SoftwareSerial mySerial3(10, 11);
 mySerial3.begin(115200);
 mySerial3.write(COM3);
 delay(50);
 if (mySerial3.available() > 0) {
   delay(10);
   if (mySerial3.read() == 0xff) {
     buffer_RTT[0] = 0xff;
     for (int i = 1; i < 4; i++) {
       buffer_RTT[i] = mySerial3.read();
     }
     CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
     if (buffer_RTT[3] == CS) {
       distance3 = (buffer_RTT[1] << 8) + buffer_RTT[2];
       
       return distance3;
     }
   }
 }
 mySerial3.end(); // <=== end "properly" the virtual UART
}

int readSensor4() {
 SoftwareSerial mySerial4(12, 13);
 mySerial4.begin(115200);
 mySerial4.write(COM4);
 delay(50);
 if (mySerial4.available() > 0) {
   delay(10);
   if (mySerial4.read() == 0xff) {
     buffer_RTT[0] = 0xff;
     for (int i = 1; i < 4; i++) {
       buffer_RTT[i] = mySerial4.read();
     }
     CS = buffer_RTT[0] + buffer_RTT[1] + buffer_RTT[2];
     if (buffer_RTT[3] == CS) {
       distance4 = (buffer_RTT[1] << 8) + buffer_RTT[2];
       
       return distance4;
     }
   }
 }
 mySerial4.end(); // <=== end "properly" the virtual UART
}
//------------------------------------------------------------------------


But the problem still persists 2 sensors stuck at 0 and one false value

You seem to have added it after the return statement