-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMusicPlayer.ino
133 lines (116 loc) · 3.81 KB
/
MusicPlayer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Standard Arduino libraries.
#include <SoftwareSerial.h> // https://www.arduino.cc/en/Reference/softwareSerial
// Third-party libraries.
#include <DFRobotDFPlayerMini.h> // DFRobotDFPlayerMini library by DFRobot (https://github.com/DFRobot/DFRobotDFPlayerMini), MP3 source: https://www.101soundboards.com/
#include <OneButton.h> // OneButton by Matthias Hertel (https://github.com/mathertel/OneButton)
// Pin configuration.
#define BUTTON_PIN 2 // Arduino pin - button - Arduino GND
#define TO_PLAYER_PIN 11 // Arduino pin (5V) -> 1kΩ resistor -> DFPlayer RX pin (3.3V)
#define FROM_PLAYER_PIN 10 // Arduino pin (5V) <- DFPlayer TX pin (3.3V)
// DFPlayer connection.
SoftwareSerial playerSoftwareSerial(FROM_PLAYER_PIN, TO_PLAYER_PIN); // RX, TX
DFRobotDFPlayerMini player;
// Buttons.
OneButton btn = OneButton(BUTTON_PIN, true, true); // Button is active LOW (connected to GND), internal pull-up resistor enabled.
void setup()
{
setupSerial();
setupPlayer();
setupButton();
}
void setupSerial() {
Serial.begin(115200);
Serial.println(F("Starting..."));
}
void setupPlayer() {
Serial.println(F("[Player] Initializing... (may take 3-5 seconds)"));
playerSoftwareSerial.begin(9600);
while (!player.begin(playerSoftwareSerial)) {
Serial.println(F("[Player] Unable to begin:"));
Serial.println(F("[Player] 1.Please recheck the connection!"));
Serial.println(F("[Player] 2.Please insert the SD card!"));
// while(true){
// delay(0); // Code to compatible with ESP8266 watch dog.
// }
}
Serial.println(F("[Player] Initialization completed successfully."));
player.volume(20); // Set volume value. From 0 to 30
player.play(1); // Play the first mp3, remove this line if you do not want a voice line on power-up
}
void setupButton() {
btn.attachClick(onButtonClicked);
btn.attachDoubleClick(onButtonDoubleClicked);
}
void loop()
{
// Update the button state.
btn.tick();
// Send debug information.
printPlayerStatus();
}
void onButtonClicked() {
Serial.println(F("[Button] Clicked"));
player.pause();
}
void onButtonDoubleClicked() {
Serial.println(F("[Button] Double clicked"));
player.start();
}
void printPlayerStatus() {
if (player.available()) {
uint8_t type = player.readType();
int value = player.read();
switch (type) {
case TimeOut:
Serial.println(F("[Player] Time Out!"));
break;
case WrongStack:
Serial.println(F("[Player] Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("[Player] Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("[Player] Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("[Player] Card Online!"));
break;
case DFPlayerPlayFinished:
Serial.print(F("[Player] Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("[Player] DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}
}