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