Programs work separately but not together???

I have two programs that I am trying to merge to work together. They work perfectly on their own. One is to print distances to the serial monitor from a LiDAR sensor (TF mini) and the other from an Ultrasonic sensor (SRF10). Merging them into one program does not work, the LiDAR sensor lags way behind the ultrasonic for some reason. Any help would be appreciated.

//LIDAR
#include<SoftwareSerial.h>// soft serial port header file

//ULTRAsonic
// include Wire library to read and write I2C commands:
#include <Wire.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>

// the commands needed for the SRF sensors:
#define sensorAddress 0x70
#define readInches 0x50

// use these as alternatives if you want centimeters or microseconds:
// #define readCentimeters 0x51
// #define readMicroseconds 0x52

// this is the memory register in the sensor that contains the result:
#define resultRegister 0x02

//LIDAR
SoftwareSerial Serial1(2,3); // define the soft serial port as Serial1, pin2 as RX, and pin3 as TX
/*For Arduino board with multiple serial ports such as DUE board, comment out the above two codes, and directly use Serial1 port*/

const int HEADER=0x59;// data package frame header


void setup()
{
 //ULTRAsonic
 // start the I2C bus
 Wire.begin();

 //LIDAR
 Serial.begin(9600);//set the Baud rate of Arduino and computer serial port
 Serial1.begin(115200);//set the Baud rate of LiDAR and Arduino serial port
 while (!Serial);
}

void loop()
{
 lidar_dist();
 sonar_dist();
 delay(450); 
}

void lidar_dist()
{
int dist;// LiDAR actually measured distance value
int strength;// LiDAR signal strength
int check;// check numerical value storage
int i;
int uart[9];// store data measured by LiDAR
 delay(70);
 if (Serial1.available())//check whether the serial port has data input
 {
    
   if(Serial1.read() == HEADER)// determine data package frame header 0x59
   {
     
     uart[0] = HEADER;
     if(Serial1.read() == HEADER)//determine data package frame header 0x59
     {
       
       uart[1] = HEADER;
       for(i=2;i<9;i++)// store data to array
       {
         //THIS WAS THE PROBLEM, CHANGED uart to uart[i] to populate array
         uart[i]=Serial1.read();
       }
     check = uart[0]+uart[1]+uart[2]+uart[3]+uart[4]+uart[5]+uart[6]+uart[7];
     if(uart[8]==(check&0xff))// check the received data as per protocols
     {
       dist=uart[2]+uart[3]*256;// calculate distance value
       strength=uart[4]+uart[5]*256;// calculate signal strength value
       Serial.print("LiDAR dist: ");
       Serial.print(dist);// output LiDAR tests distance value
       Serial.print("\t\tstrength = ");
       Serial.print(strength);// output signal strength value
       Serial.print("\t\t\t");   
      }
    }
  }
}
}

void sonar_dist()
{
 sendCommand(sensorAddress, readInches);

 // wait at least 70 milliseconds for a result:
 delay(70);

 // set the register that you want to reas the result from:
 setRegister(sensorAddress, resultRegister);

 // read the result:
 long  sensorReading = readData(sensorAddress, 2);
 Serial.print("sonar dist: ");
 if (sensorReading < 0)
   {
    Serial.print("invalid read\n");
   }
 else
   {
    Serial.print(sensorReading);
    Serial.println(" inches");
   }
   
// wait before next reading:

}

/*
 SendCommand() sends commands in the format that the SRF sensors expect
*/

void sendCommand (int address, int command) 
{
 
 // start I2C transmission:
 Wire.beginTransmission(address);

 // send command:
 Wire.write(0x00);
 Wire.write(command);

 // end I2C transmission:
 Wire.endTransmission();
}

/*
 setRegister() tells the SRF sensor to change the address pointer position
*/

void setRegister(int address, int thisRegister) 
{
 // start I2C transmission:
 Wire.beginTransmission(address);

 // send address to read from:
 Wire.write(thisRegister);

 // end I2C transmission:
 Wire.endTransmission();
}

/*
readData() returns a result from the SRF sensor
*/

long readData(int address, int numBytes) 
{
 long result = 0;        // the result is two bytes long

// send I2C request for data:
 Wire.requestFrom(address, numBytes);

 // wait for two bytes to return:
 // THIS IS THE PROBLEM, INFINITE LOOP
  while (Wire.available() < 2 )   {
   break;
 }

 // read the two bytes, and combine them into one int:
 result = Wire.read() * 256;
 result = result + Wire.read();
 return result;
}

Serial Monitor:

LiDAR dist: 358 strength = 287 sonar dist: 9 inches
LiDAR dist: 358 strength = 288 sonar dist: invalid read
LiDAR dist: 358 strength = 286 sonar dist: 9 inches
LiDAR dist: 358 strength = 288 sonar dist: 9 inches
sonar dist: 9 inches
LiDAR dist: 358 strength = 288 sonar dist: 9 inches
sonar dist: invalid read
sonar dist: invalid read
sonar dist: 9 inches
sonar dist: 9 inches
sonar dist: 9 inches
LiDAR dist: 358 strength = 288 sonar dist: 9 inches
sonar dist: 9 inches
sonar dist: invalid read
sonar dist: invalid read
sonar dist: 9 inches
sonar dist: 9 inches
sonar dist: 9 inches
sonar dist: 9 inches
sonar dist: 9 inches
sonar dist: 9 inches
sonar dist: invalid read
sonar dist: 9 inches
sonar dist: 9 inches
sonar dist: 9 inches
sonar dist: 9 inches
sonar dist: 9 inches
sonar dist: 9 inches
sonar dist: invalid read

It will periodically spit out the LiDAR distance after a long while but they should be polling at the same rate. Running them separately works perfectly.

Your problem is almost certainly SoftwareSerial. I never use it, but I understand that it is unreliable, especially at high Baud rates. Very likely, most of the records from the Lidar are rejected because of read errors.

Try AltSoftSerial or NeoSWserial instead.

I don't think those 2 alternatives are options considering the LiDAR sensor requires a 115200 baud rate :/ I just tried AltSoftSerial with no luck.

Post a link to the sensor data sheet or user manual. I would be surprised if the Baud rate cannot be changed.

If not, you need to use a hardware serial port. The Mega has four.

I see some useless delay() statements in the code. They are VERY rarely required, so try getting rid of them.

 delay(450);

There's your problem. At 115200 baud, how many characters are being ignored in 450 milliseconds? When you do start listening again, how do you know that you started at the start of a message?

I don't remember how many bytes SoftwareSerial will store in its buffer but it's probably similar to the hardware serial: about 64 bytes. So you get 64 bytes from the start of when you stopped listening (emptying the buffer) plus whatever arrived during the time that you did start servicing the buffer again.

Alternative, for an Uno: connect your Serial monitor at 9600 bps to a SoftwareSerial port (this speed should work reliably for SoftwareSerial - use a TTL to USB converter). Then you can free your hardware serial for your LiDAR.

And indeed ditch those delay() calls. Use mllis() based timing instead (see Blink Without Delay for simple example on how that works) to sample your sensors at a regular interval. You can even have your software sample both at the same time that way.