Not getting in serial data from device

I an trying to read and verify messages from an MP3 player to confirm it has started or stopped the audio it is playing.

I have tested the code by sending the same messages from the Serial Monitor. Then it works.

I have also seen the messages it sends out, by using newSoftSerial, but that unreliable and adds a lot of noise to the message.

So I only want to use the inbuilt serial and have disconnected the USB and is only using external bttery power. I connect the MP3 to Rx and Tx pins.
I can send the messages OK, and the player starts, but the Arduino won't light up the LED to show it got a confirmed message back from the player.
I am testig the incoming message for particlar strings.

Is there a better and more reliable way to read and check incoming serial data?

char incomingByte;	// for incoming serial data
boolean isPlaying = false;

String myMsg = String();

void setup() {
    Serial.begin(9600);
    // Set fadeout-time
   Serial.println("FT 200");
   pinMode(8, INPUT);
   pinMode(13, OUTPUT);
}




void loop() {
  
  
// If button is pressed, start playing
if ((digitalRead(8) == HIGH) && (isPlaying==false)) {
    playAudio();
}

// If already playing, fade out and stop.
if ((digitalRead(8) == HIGH) && (isPlaying==true)) {
  fadeOut();
}


  
   //READ RESPONSE FROM  MP3 player
 
	while (Serial.available() > 0) {
             
		// read the incoming byte:       
		incomingByte = Serial.read();

              // Put each charather into a string
              myMsg += incomingByte;

              
              // If I get a confirmed message ( "PLAY: 10007.MP3") from the MP3 player that it has started: turn LED on
             if (myMsg.substring(myMsg.indexOf("."), myMsg.indexOf(".") + 4) == ".MP3") {
                             digitalWrite(13, HIGH);
                             myMsg ="";
               }
               
               
               // When gettin a message ("ACTION: Stopped" or "ACTION: End of file") that the file has stopped: turn the LED off.
                else if ((myMsg.substring(myMsg.indexOf(":"), myMsg.indexOf(":") + 9) == ": Stopped") || (myMsg.substring(myMsg.indexOf(":"), myMsg.indexOf(":") + 13) == ": End of file") )   {
                             digitalWrite(13, LOW);
                             myMsg ="";
                            
               } 

	}

  
}

void playAudio() {
 // Start file
  Serial.println("PF 10007.MP3");
 
 isPlaying = true;
 delay(500);
}
 
void fadeOut() {
  //Fade out
   Serial.println("FO");
   delay(5000);
   // Stop file
   Serial.println("SP");
   delay(500);
   isPlaying = false;
}

Regards

Before posting code you might do a CTRL-T in the IDE to do some auto-indenting => looks better =>reads better
that said, get a look at the code below read more - String() - Arduino Reference

void loop() 
{
  // handle start stop button
  if ((digitalRead(8) == HIGH) && (isPlaying==false)) playAudio();
  if ((digitalRead(8) == HIGH) && (isPlaying==true)) fadeOut();

  //READ RESPONSE FROM  MP3 player
  while (Serial.available() > 0) 
  {
    myMsg += Serial.read();
  }


  if (myMsg.endsWith("MP3"))
  {
    digitalWrite(13, HIGH);
    myMsg ="";
  }
  if (myMsg.endsWith("Stopped") || myMsg.endsWith("End of file") )
  {
    digitalWrite(13, LOW);
    myMsg ="";
  }
}

void playAudio() 
{
  // PF = Play File <filename>
  Serial.println("PF 10007.MP3");
  isPlaying = true;
  delay(500);
}
 
void fadeOut() 
{
  //FO = Fade out
   Serial.println("FO");
   delay(5000);
   // SP = Stop Playing
   Serial.println("SP");
   delay(500);
   isPlaying = false;
}

in your original code you addressed the index+4 when it might be not valid at that moment. (imagine it just received the '.' and then this if executes...

  • if (myMsg.substring(myMsg.indexOf("."), myMsg.indexOf(".") + 4) == ".MP3") {

Thanks for tips on a cleaner code. :slight_smile:

I just had to fix a char() arround the serial read to get ASCII characters.
myMsg += char(Serial.read());

But the problem remains the same.
It does not work when connected to the MP3 player.

According to the documentation of the MP3 player it works at "3.3V logic levels":

All signal levels present on the Application Interface connector X4 are 3.3V logic
levels.
A 100 Ohm series resistor is present on each I/O line to provide a minimum
protection against ESD. If long wires are connected, or if other signal levels are to
be applied, additional signal conditioning circuitry is required.

Can this make it incompatible with the Arduino that work by 5V??
Is the Arduinos serial 5V logic level??

Morten

Is the Arduinos serial 5V logic level??

Yes.

You need a level converter, it sounds like you have not got enough voltage from the device. Sparkfun sell a level converter or just make your own with a FET.