Multiple sensors connected to Arduino MEGA 2560 Serial ports I am using Arduino MEGA 2560 to read values from two sensors. Sensor 1 is providing us two values separated by a comma and a space followed by a newline character. e.g.:

2.277, 7.942
2.269, 7.897
2.263, 8.040


Sensor 2 is providing us a single value followed by a newline character. e.g.

1.213
1.333
1.17


Sensor 1 gives twice as many readings as Sensor 2 in the same amount of time. Sensor 1 is connected via Serial1 and Sensor 2 is connected to Serial2 on MEGA board. I am using the following code to read the values:

void setup() {
  Serial.begin(57600);
  Serial1.begin(9600);
  Serial2.begin(9600);
  while (!Serial) 
  {
  }
}
void loop() { // run over and over
  if (Serial1.available()) {
    {
      Serial.print(Serial1.readStringUntil('\n'));
      Serial1.flush();
    }
  }

  Serial.print("\t\t\t");
  if (Serial2.available()) {
    while(Serial2.available())
    {
      Serial.write(Serial2.read());
      Serial2.flush();
    }
  }
  Serial.println();
  delay(1000);
}


I am expecting an output similar to this:

2.409, 8.150            2.134
2.490, 7.851            2.133
2.603, 8.759            2.122
2.634, 8.305            3.12
2.727, 8.408            1.32
3.018, 7.982            2.543
3.030, 7.982            1.345
3.020, 8.587            2.435
2.844, 8.034            2.763
2.690, 8.690            5.433
2.721, 8.011            2.348
2.705, 8.242            7.654
2.670, 8.713            2.123
2.756, 8.563            2.098
2.899, 8.413            2.765
3.464, 8.196            2.654
3.705, 8.437            2.343
3.435, 8.558            1.245
3.446, 8.592            2.345
3.421, 8.075            1.234


Instead I am getting something like this:

2.770, 8.332.635, 8.281         2.345
2.690, 8.690            2.674
2.721, 8.011            2.356
2.705, 8.242            2.345
2.670, 8.713            3.002
2.756, 8.563            2.094
2.899, 8.413.102, 8.523         2.984
3.464, 8.196            2.398
3.705, 8.437            2.745
3.435, 8.558            2.789
3.446, 8.592            2.543
3.421, 8.075            2.984
3.504, 8.153.772, 8.770         2.183
3.705, 8.552            2.784
3.653, 8.443            2.567
3.908, 7.971            2.098
5.020, 8.581            2.20
3.913, 8.247            2.342
3.762, 8.023.575, 8.345         2.123


The sensor readings after every few lines are distorted. I understand this might has something to do with the serial buffers. How could I rectify this and obtain the desired output?
Your serial buffers and moving in and out of phase, which is distorting your display.

Try something like:

void setup() {
  String sensor1;
  String sensor2;
  Serial.begin(57600);
  Serial1.begin(9600);
  Serial2.begin(9600);
  while (!Serial) 
  {
  }
}
void loop() { // run over and over
  if (Serial1.available()) {
    sensor1 = Serial1.readStringUntil('\n'));
  }

  if (Serial2.available()) {
    sensor2 = Serial2.readStringUntil('\n'));
  }

  Serial.print(sensor1);
  Serial.print(sensor2);
  Serial.println();
  delay(1000);
}


I would also recommend the method suggested by @Majenko in his comment below your question.