Edit: This is fixed by calling "Serial1." functions if using rx/tx hardware serial.
I read this blog post and figured I'd give a go at getting a pair of Leonardo's to talk to each other. I wrote up the below transmitter and receiver sketches. The sender sends a byte every second, while the receiver lights up its LED and clears the buffer on receiving some data over serial.
I hooked up the microcontrollers to the same 3.7V battery so they have a common ground and crossed over their rx and tx pins and I'm getting nothing. I've hooked each up individually to the Arduino IDE serial monitor and they both behave appropriately on their own, but when I connect them together via their rx/tx pins I get nothing.
I've spent a few hours looking for what I've done wrong and haven't come up with a fix. Any help appreciated.
if (Serial.available()) {
delay(100);
digitalWrite(ledPin, HIGH);
while(Serial.available()) {
c = Serial.read();
}
digitalWrite(ledPin, LOW);
}
You also should move your delay statement, assuming it is meant to keep the LED on for 100ms so it is visible. With only one character being sent per second, the while statement will end after reading one character, so there will only be a very short time between the LED going on and off.
You could also remove the while loop. You check that at least one character has been received in the if statement. You then do stuff with the LED and read and discard the character in the Serial.read. The whole if statement will be repeated the next time loop() is executed, so (in this example) the while loop is redundant.