Transmitting data between two Arduinos via serial

I'm trying to figure out how two get two Arduinos to talk to each other via serial, but I'm getting some weird results.

Transmit code (sending out Arduino Uno TX0):]

void setup() {
  Serial.begin(57600);
}


void loop() {
  byte x = 2;
  delay(500);
  Serial.write(x);
  Serial.println(x);
}

Receive code (receiving on Arduino Mega RX0):

void setup() {
  Serial.begin(57600);
}

void loop() {
  byte x = Serial.read();
  
  if (x != 255) {          //Serial.read() seems to return 255 when nothing to read
    Serial.print("x = ");
    Serial.println(x);
  }
}

Instead of just displaying "x = 2", serial monitor spits out:

x = 2
x = 50
x = 13
x = 10

The second value always seems to be x+48, i.e. if I change x to say, "3", I get:

x = 3
x = 51
x = 13
x = 10

...while the 13 and 10 seems to stay the same. Where are these extra values coming from? Also, sometimes the receiving end is inconsistent, and the Mega doesn't seem to read anything at all...

I've tried 9600 just to see if there was any difference in baud rates, but I still get the same results...

void loop() {
  byte x = 2;
  delay(500);
  Serial.write(x);
  Serial.println(x);
}

Your first Serial.write command sends the numerical byte value of 2 (B00000010).
The second Serial.println command, sends the ASCII character value for "2" and then the 10 and 13 are the ASCII line feed and carriage return .

See

Why aren't you using Serial1 (or Serial2 or 3) on the Mega - it would make life much simpler.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

cattledog:

void loop() {

byte x = 2;
  delay(500);
  Serial.write(x);
  Serial.println(x);
}




Your first Serial.write command sends the numerical byte value of 2 (B00000010).
The second Serial.println command, sends the ASCII character value for "2" and then the 10 and 13 are the ASCII line feed and carriage return .

See
http://www.rapidtables.com/code/text/ascii-table.htm

Ah! Gotcha. I didn't realize the print command was still sending the data out, I thought it was just to display on Serial Monitor - shows my lack of understanding on how serial comm works. Thanks!

Robin2:
Why aren't you using Serial1 (or Serial2 or 3) on the Mega - it would make life much simpler.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Oh, I had them on Serial because I was swapping the Mega and Uno back and forth while troubleshooting, and the Uno does not have the extra ports. Ideally the goal will be to have MIDI come into the Uno via RX0, dump that data into an array, and send that array out via SoftwareSerial to the Mega for further processing. (I'll most likely be back with further questions regarding SoftwareSerial later. :slight_smile: )

Finishing this thread up, any thoughts on why it would stop receiving?

Actually, when I try receiving on Serial1, I get nothing...

void setup() {
  Serial1.begin(57600);
}


void loop() {
  byte x = Serial1.read();
  
  if (x != 255) {
    Serial1.print("x = ");
    Serial1.println(x);
  }
}

I assume the RX/TX lights should at least be blinking?

Actually, when I try receiving on Serial1, I get nothing...

Are you using the correct pins for Tx and Rx ?

UKHeliBob:
Are you using the correct pins for Tx and Rx ?

Yes, I'm on pin 19 for RX, nothing on TX as I don't want to transmit anything.

...Unless it still needs a TX wire connected to work correctly?

@batmundo, I am losing contact with all the changes you are trying.

You need to post the latest version of both programs AND post a diagram showing how you have everything connected. Serial communication between an Uno and a Mega should be very straightforward.

...R

Robin2:
@batmundo, I am losing contact with all the changes you are trying.

You need to post the latest version of both programs AND post a diagram showing how you have everything connected. Serial communication between an Uno and a Mega should be very straightforward.

...R

Well, turns out the TX LED only blinks for Serial, and SoftwareSerial pin 1 - I was getting thrown off because I was expecting it to just blink in general for transmit signals on any port. Doh!

I was getting further thrown off until I realized that having a wire connected to RX0 when opening Serial Monitor causes something to hang, which was leading me to believe that the code was screwed up. Seems like if I open Serial Monitor first before connecting the wire, then everything is ok! Still not sure why though...

In any case, all works! Thanks for the help!