Hi,
I am trying to use a DF player mini in my project. After trying and testing I encounter the following issue; the DF player sometimes (more often than not) ignores the serial commands. I can see the TX led blink but the DF player doesn't respond. I am using Arduino nanos.
Here is my code:
Test_DFplayer.ino
#include "DFplayer.h"
void setup() {
setup_DF();
}
void loop() {
}
DFplayer.h:
#ifndef DF_PLAYER_H
#define DF_PLAYER_H
#define DF_STARTUP_VOL 15
# 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]
#define c_play_file 0x0F
#define c_set_volume 0x06
#define c_stop_track 0x16
#define c_play 0x0D
#define c_pause 0x0E
#define c_setvolume 0x06
#define c_initialize 0x3F
#define c_track_mode 0x08
#define c_stop 0x16
#define c_equalizer 0x07
#define C_RESET_MODULE 0x0C
#define C_REPEAT_PLAY 0x11
void setup_DF(HardwareSerial& s = Serial);
void DF_initialize(HardwareSerial& s = Serial);
void DF_playfile(byte folder_num, byte file_num, HardwareSerial& s = Serial);
void DF_track_mode(byte track_mode, HardwareSerial& s = Serial);
void DF_pause();
void DF_stop();
void DF_play();
void DF_setVolume(int vol, HardwareSerial& s = Serial);
void DF_reset(HardwareSerial& s = Serial);
void DF_repeat_play(byte mode, HardwareSerial& s = Serial);
void DF_execute_CMD(byte CMD, byte Par1, byte Par2, HardwareSerial& s);
#endif
DFplayer.cpp:
#include <Arduino.h>
#include "DFplayer.h"
void setup_DF(HardwareSerial& s = Serial)
{
s.begin(9600);
DF_initialize(s);
//DF_reset(s);
//DF_repeat_play(0, s);
//DF_track_mode(0, s);
DF_setVolume(DF_STARTUP_VOL, s);
DF_playfile(1, 1, s);
}
void DF_initialize(HardwareSerial& s = Serial)
{
DF_execute_CMD(c_initialize, 0, 0, s);
//delay(300);
}
void DF_playfile(byte folder_num, byte file_num, HardwareSerial& s = Serial)
{
DF_execute_CMD(c_play_file, folder_num, file_num, s);
//delay(30);
}
void DF_track_mode(byte track_mode, HardwareSerial& s = Serial)
{
DF_execute_CMD(c_track_mode, 0, track_mode, s);
}
void DF_reset(HardwareSerial& s = Serial)
{
DF_execute_CMD(C_RESET_MODULE, 0, 0, s);
}
void DF_repeat_play(byte mode, HardwareSerial& s = Serial)
{
DF_execute_CMD(C_REPEAT_PLAY, 0, 0, s);
}
//void DF_pause()
//{
// DF_execute_CMD(c_pause,0,0);
// //delay(30);
//}
//
//void DF_stop()
//{
// DF_execute_CMD(c_stop,0,0);
//}
//
//void DF_play()
//{
// DF_execute_CMD(c_play,0,1);
// //delay(30);
//}
void DF_setVolume(int vol, HardwareSerial& s = Serial)
{
DF_execute_CMD(c_setvolume, 0, vol, s); // Set the volume (0x00~0x30)
//delay(30);
}
void DF_execute_CMD(byte CMD, byte Par1, byte Par2, HardwareSerial& s = Serial)
// 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++)
{
s.write(Command_line[k]);
}
}
One of my Arduinos has a sketch uploaded in which the DFplayer responds to all the commands as intended; but I can't find that sketch.
Hardware:
Arduino TX -> DFplayer RX
Arduino RX -> DFplayer TX
I tried both having 1k series resistors and not on the serial lines but it makes no difference.
Thank you.