Serial communication between 2 Arduinos

Hello.
I am trying to create Serial communication between 2 Arduino Dues.
I wired TX1 on first arduino to RX0 on second and RX1 on first Arduino to TX0 on second. I also connected grounds. The problem now is with coding.

I want to send char 'r' to second arduino, which would send back 4 numerical values, after it received 'r'.
The the function I call in loop on first is:

void readFromDiodes()
{

  Serial1.println('r');
  delay(100);
  int i=0;
if (Serial1.available()) {
    delay(100); //allows all serial sent to be received together
    while(Serial1.available() && i<4) {
      value[i++] = Serial1.read();
    }


}
}

And on second is:
Also code on second works if I send 'r' through Serial Monitor, so I am guessing the code on first one must be wrong.

void loop() {
  // put your main code here, to run repeatedly:
  recvWithEndMarker();
  showNewData();
}

void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;

  // if (Serial.available() > 0) {
  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}

void showNewData() {
  if (newData == true) {


    if (receivedChars[0] == 'r')   {

      //     Serial.println('v');
      

          Serial.println(5);
          Serial.println(66);
          Serial.println(77);
          Serial.println(8);
          

       

    }
    
    }
    newData = false;
    }

so I am guessing the code on first one must be wrong.

Wrong? Maybe.

Incomplete? Definitely.

Can't help with incomplete code. Post ALL of your code.

Of course, by using the one hardware serial port for inter-board communication, you've made it impossible to use it for debugging/interfacing with the PC.

You should consider using SoftwareSerial and other pins for board-to-board communication.

i will always be 0 because it's a local non-static variable...

But you never describe what the problem is :wink:

And as long as you don't want to talk to the PC using the hardware serialis all fine :slight_smile: Even preferred.

    delay(100); //allows all serial sent to be received together

No, it does not. It wastes time in the hope that all the data will arrive. But, it does not ensure that that happens.

Thank you for answers, I changed it, but it is still not working.
The whole problem is: I have LCD TFT screen on first Arduino. And I would like it to display the values that second Arduino sends.
I am sending you the whole code:

int value[]={0,0,0,0}
int onOff=1;
int time1=0;
int time2=0;
int frequency=1000;
void loop()
{
  
 
  if (onOff == 1) {
    

    time1 = millis();

    int freq = atoi(frequency);

    if (time1 - time2 >= frequency) {
      
      readFromDiodes();
      for(int i=0;i<4;i++)
     {
      myGLCD.printNumI(value[i], 20+100*i, 200); //Prints numbers from value to screen
      
      }

      time2 = time1;
    }
  }

}
void readFromDiodes()
{

  Serial1.println('r');
  delay(100);
  int i=0;
if (Serial1.available()) {
    
    while(Serial1.available() && i<4) {
      value[i] = Serial1.read();
      i++;
    }


}
}

Let's quote to make it more clear:

PaulS:
Can't help with incomplete code. Post ALL of your code.

    int freq = atoi(frequency);

The atoi() function expects a string. frequency is NOT a string.

You are using Serial1, which implies that you have a Leonardo, Mirco, Mega, or Due (or something that isn't really an Arduino). What Arduino is this code running on?

Is there some part of ALL that is too hard for you to understand?

The code is too long, so I just took the important things out. I know, in my code the frequency is a string, and I have to turn it to int, but I forgot to change it in the post.
I figured it out, it works now, thanks everyone :slight_smile:

Nejcar:
The code is too long, so I just took the important things out.

Next time if the code is to long, take out "the important bits" (in quotes because that's questionalble, a lot of times the error isn't in it...) and make a small demo sketch with the same problem.

This gives two advantages

  • We can look at complete code and compile it
  • And often while making the demo you find the problem :wink: