-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEliPlayer.ino
206 lines (174 loc) · 4.39 KB
/
EliPlayer.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <Adafruit_SSD1306.h>
#include <DFRobotDFPlayerMini.h>
#include <Keypad.h>
#include "SoftwareSerial.h"
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 5 // Reset pin # (or -1 if sharing Arduino reset pin)
SoftwareSerial mySerial(10, 11);
const int interrupt0 = 0;
boolean isPlaying = false;
int currVolume = 10;
int lastVolume = currVolume;
int CLK = 2;//CLK->D2
int DT = 3;//DT->D3
int SW = 4;//SW->D4
int lastCLK = 0;
int number_folders = 1;
int current_folder = 1;
int current_song = 1;
int current_folder_count = 1;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
const char hexaKeys[ROWS][COLS] PROGMEM = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 12, 13}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {17, 16, 15, 14}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
DFRobotDFPlayerMini myDFPlayer;
void setup () {
Serial.begin(9600);
mySerial.begin (9600);
delay(1000);
Serial.println("go");
pinMode(SW, INPUT);
digitalWrite(SW, HIGH);
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
attachInterrupt(interrupt0, ClockChanged, CHANGE);
isPlaying = true;
myDFPlayer.begin(mySerial);
delay(1000);
number_folders = myDFPlayer.readFolderCounts() - 1;
current_folder_count = myDFPlayer.readFileCountsInFolder(1);
myDFPlayer.setTimeOut(500);
myDFPlayer.volume(currVolume);
delay(300);
randomSeed(analogRead(0));
randomize_folder();
}
int digit_to_int(char d)
{
char str[2];
str[0] = d;
str[1] = '\0';
return (int) strtol(str, NULL, 10);
}
void loop () {
// handle volume
if (currVolume != lastVolume) {
setVolume(currVolume);
lastVolume = currVolume;
delay(100);
}
if (!digitalRead(SW)) {
Serial.println("Pause");
if(isPlaying) {
myDFPlayer.pause();
isPlaying = false;
} else {
isPlaying = true;
myDFPlayer.start();
}
delay(600);
}
char customKey = customKeypad.getKey();
int customKeyInt = digit_to_int(customKey);
if (customKeyInt > 0) {
// Serial.print("Playing sound: ");
Serial.println(customKeyInt);
myDFPlayer.advertise(customKeyInt);
}
if (customKey == 'A') {
if(isPlaying) {
myDFPlayer.pause();
isPlaying = false;
} else {
isPlaying = true;
myDFPlayer.start();
}
}
if (customKey == 'B') {
if(isPlaying) {
prev_song();
//myDFPlayer.previous();
}
}
if (customKey == 'C') {
if(isPlaying) {
next_song();
//myDFPlayer.next();
}
}
if (customKey == 'D') {
if (number_folders > 1) {
current_folder++;
if (current_folder >= number_folders) {
current_folder = 1;
}
randomize_folder();
}
}
if (myDFPlayer.available()) {
myDFPlayer.read();
uint8_t type = myDFPlayer.readType();
int value = myDFPlayer.read();
if (isPlaying && type == DFPlayerPlayFinished) {
next_random();
}
}
}
void prev_song() {
current_song--;
if (current_song == 0) {
current_song = current_folder_count;
}
myDFPlayer.playFolder(current_folder, current_song);
isPlaying = true;
}
void next_song() {
current_song++;
if (current_song > current_folder_count) {
current_song = 1;
}
myDFPlayer.playFolder(current_folder, current_song);
isPlaying = true;
}
void next_random() {
current_song = random(current_folder_count) + 1;
myDFPlayer.playFolder(current_folder, current_song);
isPlaying = true;
}
void randomize_folder() {
current_folder_count = myDFPlayer.readFileCountsInFolder(current_folder);
delay(100);
current_song = random(current_folder_count) + 1;
myDFPlayer.playFolder(current_folder, current_song);
isPlaying = true;
}
void ClockChanged() {
//Read the CLK pin level
int clkValue = digitalRead(CLK);
//Read the DT pin level
int dtValue = digitalRead(DT);
if (lastCLK != clkValue) {
lastCLK = clkValue;
//CLK and inconsistent DT + 1, otherwise - 1
currVolume += (clkValue != dtValue ? -1 : 1);
if (currVolume < 0) {
currVolume = 0;
}
if (currVolume > 23) {
currVolume = 23;
}
}
}
void setVolume(int volume)
{
myDFPlayer.volume(volume);
}