-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmp3playercode.ino
146 lines (109 loc) · 2.12 KB
/
mp3playercode.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
#include <SoftwareSerial.h>
#include <pcmConfig.h>
#include <pcmRF.h>
#include <TMRpcm.h>
#include <SD.h>
#include <SPI.h>
#define CS 4
TMRpcm aud;
SoftwareSerial mySerial(2,3);
int n=0,i=0;
File root;
String songs[20];
int choice;
void setup() {
pinMode(5,OUTPUT);
aud.speakerPin=9;
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.print("Initializing SD card...");
if (!SD.begin(CS)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
mySerial.begin(9600);
root = SD.open("/");
printDirectory(root);
Serial.println("done!");
Serial.println("The wav files in the SD card are : \n");
for(int k=0;k<i;k++)
{
Serial.println(songs[k]);
}
playsong(n);
}
void loop()
{
digitalWrite(5,HIGH);
if (mySerial.available()) {
choice=mySerial.read();
if(choice>100)
choice=0;
Serial.println(choice);
}
switch(choice)
{
case 1:
choice=0;
if(aud.isPlaying())
aud.pause();
else
playsong(n);
break;
case 2:
aud.stopPlayback();
break;
case 3:
choice=0;
n++;
if(n>=i)
n=0;
playsong(n);
break;
case 4:
choice=0;
n--;
if(n<=0)
n=i-1;
playsong(n);
break;
case 5:
choice=0;
aud.volume(1);
break;
case 6:
choice=0;
aud.volume(0);
break;
case 7:
choice=0;
aud.setVolume(0);
break;
}
}
void playsong (int sno)
{
char sname[20];
songs[sno].toCharArray(sname,20);
aud.play(sname);
}
void printDirectory(File dir) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
break;
}
if (entry.isDirectory()) {
printDirectory(entry);
}
Serial.println(entry.name());
String entryname=entry.name();
if(entryname.endsWith(".WAV"))
{
songs[i]=entryname;
i++;
}
entry.close();
}
}