Serial on Arduino Mega

Hi all.

I'm a beginner with my new Mega 2560. I'm trying a simple thing : connecting serial 1 to serial 2 and sending a byte between them.

So I wired rx1 (19) to tx2 (16) and tx1 (18) to rx2 (17).

And here is the code :

unsigned long minWait;
void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial2.begin(9600);

  Serial2.print((byte)2);

  minWait=millis();
}

void loop()
{ 
  if (millis()>minWait) {
    if (Serial1.available() > 0) {
      byte b = Serial1.read();    
      Serial.println("RECEIVED S1 -> "+String(b, DEC));
      Serial2.print(b);
      minWait=millis()+2000;
    } else    
    if (Serial2.available() > 0) {
      byte b = Serial2.read();
      Serial.println("RECEIVED S2 -> "+String(b, DEC));
      Serial1.print(b);
      minWait=millis()+2000;
    }
  }
}

I expected to see on the serial monitor : RECEIVED S1 : 2 RECEIVED S2 : 2 RECEIVED S1 : 2 RECEIVED S2 : 2 RECEIVED S1 : 2 ...

Instead, I see : RECEIVED S1 : 2 RECEIVED S1 : 2 RECEIVED S1 : 2 ...

There must be a big mistake in my code, but I really can't find it.

If someone can help ...

Thanks !

Mike

Your code says: "If there is a character on Serial1, send a character from Serial2." Which is what you see on the serial monitor.

Your code only sends a character from Serial1 after Serial2.Available is greater than 0. See the error in your logic? You wait until you receive a character before you send the first one.

Ok, I'm stupid. Really sorry for the noise. And thanks a lot !