Hello!
I an trying to send serial commands to a MP3 player using the Tx and Rx pins.
I have been able to get data from the player and display it in the Serial monitor, if I trigger the player by physical connetions.
But I can't send it commands.
According to the documentation I should send "PF: 10000" to play the file "10000.mp3".
It should then send a resonse back "PLAYING 10000.mp3".
But I think i am getting confusion when trying to read and write serial data at the same time.
So how can i send a command and wait for a resonse, and write it to the serial monitor only?
I think waht happens now is, that when I try to write to the serial monitor, I also write to the MP3 player.
So how can i:
- send a serial command to the mp3 player
- get a response back and
- write that to the serial monitor?
my code so far:
char incomingByte; // for incoming serial data
void setup() {
Serial.begin(9600);
}
void loop() {
// Send play command to MP3 via Tx
Serial.write("PF:10000");
//READ REPSONSE FROM MP3 player
// send data only when you receive data:
while (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
//Serial.print("I received: ");
Serial.print(incomingByte);
}
//WAit 5 sec, and play again
delay(5000);
}
Hm. OK, but if I disconnect the USB, disable code that write to the monitor and use external power; should it then automatically connect to the MP3 player?
Will the onboard Tx and Rx LEDs blink when communicating with the player?
And I just need to find a different way to indicate that I got a response?
I am using the arduino Uno. Smb version. The one with the tiny chip and not the big chip.
Thanks for your help!
Ok I got the New Software Library working.
But i often get garbage in the messages, and I think that mess up the reliability.
But generally I can read the incomming messages from the player inbetween the garbage.
It was more stable in the standard Tx an Rx pins.
How can "filter out" the garbage?
My Code
#include <NewSoftSerial.h>
#define rxPin 2
#define txPin 3
char incomingByte; // for incoming serial data
boolean isPlaying = false;
NewSoftSerial mySerial(rxPin, txPin);
void setup() {
// define pin modes for tx, rx, led pins:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
Serial.begin(9600);
mySerial.begin(9600);
pinMode(8, INPUT);
}
void loop() {
if ((digitalRead(8) == HIGH) && (isPlaying==false)) {
playAudio();
}
if ((digitalRead(8) == HIGH) && (isPlaying==true)) {
fadeOut();
}
//READ RESPONSE FROM MP3 player
// send data only when you receive data:
while (mySerial.available() > 0) {
// read the incoming byte:
incomingByte = mySerial.read();
// say what you got:
Serial.print(incomingByte);
}
}
void playAudio() {
mySerial.flush();
mySerial.println("PF 10007");
isPlaying = true;
delay(500);
}
void fadeOut() {
//Fade out
mySerial.flush();
mySerial.println("FO");
delay(1000);
// Stop file
mySerial.flush();
mySerial.println("SP");
isPlaying = false;
}
My Garbage
F`ÿnÿ
?II=Iéªr¹½Ý¹
½µµ?¹?5)Rþ ÿ
Q%=9é?¢½ÁÁ??5)
¢?õÊJ*r?½??¥±?5)Rþ@ÿ`ÿ00)ÿPLAY: 10007.MP3
ý¨H(jUÔÊJ?¢½ÁÁ??5)
¢?õÊJ*r?½??¥±?5)Rþÿ`ÿ`ÿ7
PLAY: 10007.MP3
SP
ACTION: Stopped
ACTION: End of file
@ÿ`ÿ007ÿPLAY: 10007.MP3
SP
ACTION: Stopped
ACTION: End of file
ACTION: End of file
Thanks