Inter-Arduino Serial Comms

I'm not a beginner with Arduino, but I'm about to pull my hair out over a seemingly simple problem:

I'm just trying to transfer one byte from one Arduino (Mega) to another Arduino (Mega as well) using a serial port.

Here is the TX code:

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("MASTER");
}

void loop()
{
  Serial1.write(100);
}

And here is the RX code:

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("SLAVE");
}

void loop()
{
  byte trash;
  
  while(Serial1.available())
  {
    trash = Serial1.read();
    Serial.println(trash);
  }
}

As for wiring: I have a common ground between the Arduinos and have the TX from Serial1 on the master connected to the RX of Serial1 on the slave.

The problem is that I am not getting a correct transfer of data. The receiver prints the number 22 instead of 100.

This is such a simple little project and there isn't much that can go wrong. Any ideas?

So, I changed the TX code to:

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("MASTER");
}

void loop()
{
  Serial1.write(100);
  Serial1.write(101);
}

And it worked. I then changed the TX code back to :

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial.println("MASTER");
}

void loop()
{
  Serial1.write(100);
}

And it continued to work. Not sure why it didn't work in the first place.

Try putting a delay() function call in the send loop.

aarg:
Try putting a delay() function call in the send loop.

Yup, looks like that did the trick, too. I guess without it there's input serial buffer overflow? But if there's buffer overflow, it still shouldn't give me wrong values. Still kind of confusing.

Ok, so now that I got a reliable transfer of a byte, I'm having trouble incorporating that into a larger project.

TX and RX code attached (too large to embed).

TX - DCU_2.01_-Relink_Protection
RX - SD_INTERFACE
-_Relink_Protection

Note large sections of the code are commented out for debugging purposes. Also, the TX side is correctly gathering sensor data and is correctly displaying data on the monitor. All electrical connections have been verified to be correct (see pic).

Problem: I'm only receiving 0s on the RX side instead of actual data.

DCU_2.01_-_Relink_Protection.ino (8.33 KB)

SD_INTERFACE_-_Relink_Protection.ino (1.5 KB)

You're receiving several characters in the time that you're printing one. It can't keep up. You need to implement some kind of flow control.

Slowing the code down with a delay(100); after each print doesn't help.

setup.jpg

Power_Broker:
Slowing the code down with a delay(100); after each print doesn't help.

What means, "after each print"? How would the transmitter know when the print is finished?

aarg:
What means, "after each print"? How would the transmitter know when the print is finished?

I put the delays in the TX code. As in:

Serial1.print(variable);
delay(100);

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

I have used it whenever I needed inter-Arduino communication as well as PC to Arduino communication.

...R

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

I have used it whenever I needed inter-Arduino communication as well as PC to Arduino communication.

...R

I've already reviewed your tutorials, but the problems still persist.

Power_Broker:
I've already reviewed your tutorials, but the problems still persist.

That was not apparent from the code in your Original Post.

Unless there is a compelling performance reason to send binary data it will be much easier to debug the programs if you send the data in human readable form using Serial.print() rather than Serial.write()

...R