Reading serial data from one arduino in another arduino

Hi,

So I have a bot with an Arduino Nano and an Arduino Mega. The Nano sends some data to the Mega using 3 Serial.println() statements at a time for 3 long values respectively based on sensor data at regular intervals. I want to read these sets of 3 values in the Mega into an array for the long integers. I tried the following code:

  long data[3];

  while (Serial3.available() == 0) {
    data[0] = 0;
    data[1] = 0;
    data[2] = 0;
  }
  if (Serial3.available() >= 3) {
    for (int i=0; i<3; i++) {
      data[i] = Serial3.read();
    }
    Serial3.flush();
  }

When I then write the values in data[] to the serial monitor to check them, I don't get the correct values. Any help to solve this is appreciated.

Have a look at the examples in Serial Input Basics. There is also a parse example.

...R

  if (Serial3.available() >= 3) {
    for (int i=0; i<3; i++) {
      data[i] = Serial3.read();
    }
    Serial3.flush();
  }

You don't have a clue what flush() does, do you?

I thought it clears the buffer, but a quick check tells me that information is outdated. My bad. Thanks.
Anyways, turns out when I use Serial.write() on the Mega, it shows the correct data. It shows incorrect data when I use Serial.print()

It shows incorrect data when I use Serial.print()

No. It shows different data. The data that it shows is NOT incorrect.