Arduino Leonardo to Arduino Leonardo Serial Communication

Hi all,

I try to develop a project related on Leonardo to Leonardo serial communication with using TX and RX pins. The problem is, code is not robust. So, when you compile the same code some times e' seen(for u transmitted) some times the true sending data(u) seen at the receiving arduino. The codes are following:

For the transmitter arduino;

char u = 'o';
void setup() {
  // put your setup code here, to run once:
  Serial1.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial1.write(u);
}

For the receiving arduino;

char d;
void setup() {
  // put your setup code here, to run once:
  Serial1.begin(9600);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial1.available()>0){
  d = Serial1.read();}
 //d = d - '0';
  Serial.println(d);
  Serial.flush();
}

(I wrote Serial.flush to preventing java heap error.)

Can you help me? Thank you.

kycn

Have a look at Serial Input Basics - simple reliable ways to receive data. The system in the 3rd example is the most reliable.

I was recently using this between a Leonardo and a Mega.

You need a GND connection between the two boards as well as Rx and Tx.

...R

Thank you Robin2 it is valuable subject for me. Anyway, i solved this problem too. The problem is compile order. Compile receiving arduino first, after that compile the transmitted arduino. Because of sync bits, data can corrupted if you first compile transmitter.

kycn

The order of compiling should be entirely irrelevant.

I could understand that if it was neccesary to start one Leonardo before the other - but code should be written so that does not matter either.

...R