Serial Communication Between Two Arduino Help

Hi all,

I work on a project. This project basicly consist of MEGA, UNO and Mp3 Player Shield.

I want to send a byte from Mega. UNO will receive the byte. Control the byte. According to the byte play the song.

For example: For byte 70, UNO will play track001, For byte 71 UNO will play track 002.

In that point I have two problems.

Firstly, when I send the 70, sometimes this byte received like 25, 70 or 148. Just these three numbers. I solve this problem but it may not be healthy. (As you know UNO has one serial port, because of this, for temporary I use UNO to send byte. I use MEGA to receive bytes. In this way I can see the received numbers in my different serial port thanks to MEGA. )

Second problem and the most important :slight_smile: The codes don't work. Actually, in receiver code (UNO) there is a if statement like: if (ret==70 ||ret == 148 || ret ==25) . This code is OK. I tried this.

By the way, Mp3 Player Shield is OK too. Without Serial it is work. I tried. I just add the Serial. The codes like that:

THIS IS FOR MEGA - SENDER

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

void loop() {
  Serial.write(70);
}

THIS IS FOR UNO - RECEIVER

#include <Wire.h>
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>
SdFat sd;
SFEMP3Shield MP3player;
int16_t last_ms_char; // milliseconds of last recieved character from Serial port.
int8_t buffer_pos; // next position to recieve character from Serial port.

char buffer[6]; // 0-35K+null

void setup() {

  uint8_t result; //result code from some function as to be tested at later time.

  Serial.begin(115200); // seri port ekranı için 
  if(!sd.begin(SD_SEL, SPI_FULL_SPEED)) sd.initErrorHalt();
  if(!sd.chdir("/")) sd.errorHalt("sd.chdir");

  result = MP3player.begin();
 
  last_ms_char = millis(); // stroke the inter character timeout.
  buffer_pos = 0; // start the command string at zero length.
  
  
}

int gelen;
void loop() {
    uint8_t result;
   
if(Serial.available()>0){ 
 
     gelen = Serial.read();

  if (gelen==70 ||gelen == 148 || gelen ==25){ 
   
    uint32_t offset = 0; 
    char trackName[] = "track023.mp3";
    result = MP3player.playMP3(trackName, offset);
   delay(1000);
}
} 
}

THIS IS FOR MEGA - SENDER

As fast as possible, spam the other device with the value 70. How is that useful?

The Serial instance on the Mega should be reserved for talking to the PC. Use one of the other SerialN instances (and correct pins) to send data to the Uno.

On the Uno, you should be using SoftwareSerial, and a reasonable baud rate, and two other digital pins (and ground) to communicate with the Mega. That leaves Serial available for debugging.

Have a look at serial input basics.

It is much easier to design the receiving code first and then make the sender comply with that.

And what @PaulS has said.

...R

PaulS:
As fast as possible, spam the other device with the value 70. How is that useful?

The Serial instance on the Mega should be reserved for talking to the PC. Use one of the other SerialN instances (and correct pins) to send data to the Uno.

On the Uno, you should be using SoftwareSerial, and a reasonable baud rate, and two other digital pins (and ground) to communicate with the Mega. That leaves Serial available for debugging.

Thank you for answer.

I tried Serial2 and connect them to RX2 and TX2. I recognize that and make it true. Thanks.

On the other hand it still doesn't work. I also use SerialSoftware library.

Note: TX2 from Mega to Digital0 RX on UNO
RX2 from Mega to Digital1 TX on UNO.
This is my connection.

In Software Library there is a line : SoftwareSerial mySerial(10, 11); it is correct?

In Software Library there is a line : SoftwareSerial mySerial(10, 11); it is correct?

There is NOT a line like that in the library. There is a line like that in an example that uses the library. It CLEARLY says there is something connected to pins 10 and 11. You have, you said, connected the Mega to pins 0 and 1. Do NOT try to do SoftwareSerial on the hardware serial pins.

Below is some serial test code I've used to send data between two arduinos. You can open two instances of the arduino IDE and test using the two serial monitors.

//zoomkat 3-5-12 simple delimited ',' string tx/rx 
//from serial port input (via serial monitor)
//and print result out serial port
//Connect the sending arduino rx pin to the receiving arduino rx pin. 
//Connect the arduino grounds together. 
//What is sent to the tx arduino is received on the rx arduino.
//Open serial monitor on both arduinos to test
//A diode between the slave tx and master rx is suggested 
//for isolation, with diode band attached to slave tx side. 
//

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >0) {
        Serial.print(readString); //prints string to serial port out
        Serial.println(','); //prints delimiting ","
        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

Nothing better than the EasyTransfer library for something like this.