#include <DFRobotDFPlayerMini.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SD.h>
HardwareSerial mySerial(1); // 使用Serial1
DFRobotDFPlayerMini dfPlayer;
const int prevButtonPin = 12; // 前一首按钮引脚
const int pauseButtonPin = 13; // 暂停按钮引脚
const int nextButtonPin = 14; // 后一首按钮引脚
const int busyPin = 5; // DFPlayer Mini的Busy引脚
LiquidCrystal_I2C lcd(0x27, 16, 2); // 初始化LCD,地址0x27,16列2行
int totalTracks = 0;
int currentTrack = 1;
bool isPaused = false;
void setup() {
pinMode(prevButtonPin, INPUT_PULLUP);
pinMode(pauseButtonPin, INPUT_PULLUP);
pinMode(nextButtonPin, INPUT_PULLUP);
pinMode(busyPin, INPUT_PULLUP);
Serial.begin(2400);
mySerial.begin(9600, SERIAL_8N1, 16, 17); // RX = 16, TX = 17 for DFPlayer
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Initializing...");
if (!dfPlayer.begin(mySerial)) {
lcd.setCursor(0, 1);
lcd.print("Init failed!");
Serial.println("Unable to begin:");
Serial.println("1. Please recheck the connection!");
Serial.println("2. Please insert the SD card!");
while (true);
}
Serial.println("DFPlayer Mini online.");
lcd.setCursor(0, 0);
lcd.print("DFPlayer online");
totalTracks = dfPlayer.readFileCounts();
Serial.print("Total tracks: ");
Serial.println(totalTracks);
if (totalTracks == -1) {
Serial.println("Read SD card error!");
lcd.setCursor(0, 1);
lcd.print("Read SD error!");
} else {
Serial.println("SD card read successfully!");
lcd.setCursor(0, 1);
lcd.print("Tracks: ");
lcd.print(totalTracks);
}
dfPlayer.volume(5); // 设置音量(0-30)
playTrack(currentTrack);
}
void loop() {
if (digitalRead(prevButtonPin) == LOW) {
previousTrack();
delay(200); // 防抖动
}
if (digitalRead(nextButtonPin) == LOW) {
nextTrack();
delay(200); // 防抖动
}
if (digitalRead(pauseButtonPin) == LOW) {
pauseTrack();
delay(200); // 防抖动
}
if (digitalRead(busyPin) == LOW) {
nextTrack();
}
// 检查是否有DFPlayer错误
if (dfPlayer.available()) {
printDetail(dfPlayer.readType(), dfPlayer.read());
}
delay(100); // 延迟以便更好地观察输出信息
}
void playTrack(int track) {
dfPlayer.play(track);
lcd.setCursor(0, 1);
lcd.print("Playing: ");
lcd.print(track);
Serial.print("Playing track: ");
Serial.println(track);
}
void nextTrack() {
if (currentTrack < totalTracks) {
currentTrack++;
} else {
currentTrack = 1; // 循环播放
}
playTrack(currentTrack);
}
void previousTrack() {
if (currentTrack > 1) {
currentTrack--;
} else {
currentTrack = totalTracks; // 循环播放
}
playTrack(currentTrack);
}
void pauseTrack() {
if (isPaused) {
dfPlayer.start();
lcd.setCursor(0, 1);
lcd.print("Playing: ");
lcd.print(currentTrack);
Serial.println("Resuming track.");
} else {
dfPlayer.pause();
lcd.setCursor(0, 1);
lcd.print("Paused: ");
lcd.print(currentTrack);
Serial.println("Pausing track.");
}
isPaused = !isPaused;
}
void printDetail(uint8_t type, int value) {
switch (type) {
case DFPlayerPlayFinished:
Serial.print("Number:");
Serial.print(value);
Serial.println(" Play finished.");
nextTrack(); // 播放完自动播放下一首
break;
case DFPlayerError:
Serial.print("DFPlayerError: ");
Serial.print(value); // 打印错误码
Serial.print(" - ");
switch (value) {
case Busy:
Serial.println("Card not found");
break;
case Sleeping:
Serial.println("Sleeping");
break;
case SerialWrongStack:
Serial.println("Stack wrong");
break;
case CheckSumNotMatch:
Serial.println("Check sum not match");
break;
case FileIndexOut:
Serial.println("File index out of bounds");
break;
case FileMismatch:
Serial.println("Cannot find file");
break;
case Advertise:
Serial.println("In advertise");
break;
default:
Serial.println("Unknown error");
break;
}
break;
default:
break;
}
}