Being low on money and without a job, I thought i'd make a gift that used items I already had (nano) and some minimal extra parts. Figured I could make a personalized gift while learning about electronics and audio. While I have learned some, I've mostly learned I have no idea how to get this dfplayer mini to work. I'm using the code from Information about using DFPlayer Mini MP3 player card , and added a few lines. The code is as follows:
#include "SoftwareSerial.h"
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]
int rxPin = 10;
int txPin = 11;
SoftwareSerial mySerial(rxPin, txPin);
int digValue;
void setup () {
Serial.begin(9600);
mySerial.begin (9600);
execute_CMD(0x3F, 0, 0); // Send request for initialization parameters
while (mySerial.available()<10) {// Wait until initialization parameters are received (10 bytes)
delay(30);
Serial.print(".");
}
Serial.println("");
Serial.println("Init complete, create and transmit command to set volume level.");
// Pretty long delays between succesive commands needed (not always the same)
// Initialize sound to very low volume. Adept according used speaker and wanted volume
execute_CMD(0x06, 0, 3); // Set the volume (0x00~0x30)
Serial.println("Volume level set.");
}
void loop () {
if (Serial.available())
{
// Input Serial monitor: Command and the two parameters in decimal numbers (NOT HEX)
// E.g. 3,0,1 (or 3 0 1 or 3;0;1) will play first track on the TF-card
byte Command = Serial.parseInt();
byte Parameter1 = Serial.parseInt();
byte Parameter2 = Serial.parseInt();
// Write the input at the screen
Serial.print("Command : 0x");if (Command < 16) Serial.print("0"); Serial.print(Command, HEX);
Serial.print("("); Serial.print(Command, DEC);
Serial.print("); Parameter: 0x");if (Parameter1 < 16) Serial.print("0");Serial.print(Parameter1, HEX);
Serial.print("("); Serial.print(Parameter1, DEC);
Serial.print("), 0x");if (Parameter2 < 16) Serial.print("0");Serial.print(Parameter2, HEX);
Serial.print("("); Serial.print(Parameter2, DEC);Serial.println(")");
// Excecute the entered command and parameters
execute_CMD(Command, Parameter1, Parameter2);
}
if (mySerial.available()>=10)
{
// There is at least 1 returned message (10 bytes each)
// Read the returned code
byte Returned[10];
for (byte k=0; k<10; k++)
Returned[k] = mySerial.read();
// Wtite the returned code at the screen
Serial.print("Returned: 0x"); if (Returned[3] < 16) Serial.print("0"); Serial.print(Returned[3],HEX);
Serial.print("("); Serial.print(Returned[3], DEC);
Serial.print("); Parameter: 0x"); if (Returned[5] < 16) Serial.print("0"); Serial.print(Returned[5],HEX);
Serial.print("("); Serial.print(Returned[5], DEC);
Serial.print("), 0x"); if (Returned[6] < 16) Serial.print("0"); Serial.print(Returned[6],HEX);
Serial.print("("); Serial.print(Returned[6], DEC); Serial.println(")");
}
}
void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
mySerial.write( Command_line[k]);
}
}
Here is how I have it wired up (powered via usb)
I have a freshly formatted sd card (fat32) with a folder named mp3 and the tracks inside titled 0001.mp2 and 0002.mp3. When i send a command to play a song, whether 3;0;1; or 18;0;1; , the blue light on the dfplayer flashes for a second before shutting off. As soon as the led flashes a loud, distorted buzzing sound is played continuously until i power off the nano. Thank you for any help you could provide.
Another thing: when I leave it connected as follows and plug the nanos usb power supply in, a single period is printed to the screen (a single byte is available to read? I was expecting 10 periods). If I move the speaker neg. / ground to the ground rail, a period is printed endlessly and the dfplayer initialization never completes. Any idea as to why?
I'm being greedy at this point, but havent found answers: The code shown above has an unsigned int (word checksum) with a value that's wrapped in parentheses and a '-' before it. I read that giving an unsigned int a negative value will cause it to wrap around (starting with the largest value it can hold and decreasing from there). Is that the purpose of the '-' in this case?
Any help is greatly appreciated.