forked from Harshil-27/Li-Fi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduino_Audio_Player.ino
99 lines (98 loc) · 2.74 KB
/
Arduino_Audio_Player.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
/*Author-Harshil Patel
*Purpose-Play wave audio files with Arduino.
*/
#include <SPI.h> // include Arduino SPI library
#include <SD.h> // include Arduino SD library
#include "TMRpcm.h" // include TMRpcm library
#define next 2
#define _pause 3
TMRpcm audio;
File root;
void setup(void) {
Serial.begin(9600);
pinMode(next, INPUT_PULLUP);
pinMode(_pause, INPUT_PULLUP);
Serial.print("Initializing SD card...");
if (!SD.begin()) {
Serial.println("failed!");
while(true); // stay here.
}
Serial.println("OK!");
audio.speakerPin = 9; // set speaker output to pin 9
root = SD.open("/"); // open SD card main root
printDirectory(root, 0); // print all files names and sizes
audio.setVolume(5); // 0 to 7. Set volume level
audio.quality(1); // Set 1 for 2x oversampling Set 0 for normal
}
// main loop
void loop() {
if ( !audio.isPlaying() ) {
// no audio file is playing
File entry = root.openNextFile(); // open next file
if (! entry) {
// no more files
root.rewindDirectory(); // go to start of the folder
return;
}
uint8_t nameSize = String(entry.name()).length(); // get file name size
String str1 = String(entry.name()).substring( nameSize - 4 ); // save the last 4 characters (file extension)
if ( str1.equalsIgnoreCase(".wav") ) {
// the opened file has '.wav' extension
audio.play( entry.name() ); // play the audio file
Serial.print("Playing file: ");
Serial.println( entry.name() );
}
else {
// not '.wav' format file
entry.close();
return;
}
while( debounce(next) ) ; // wait until 'next' button is released
}
if ( !digitalRead(next) ) {
// 'next' button is pressed
audio.stopPlayback(); // stop playing
return;
}
if ( !digitalRead(_pause) ) {
// '_pause' button is pressed
audio.pause(); // pauses/unpauses playback
while( debounce(_pause) ) ; // wait until '_pause' button is released
}
}
// a small function for buttons debounce
bool debounce (int bt)
{
byte count = 0;
for(byte i = 0; i < 5; i++)
{
if ( !digitalRead(bt) )
count++;
delay(10);
}
if(count > 2) return 1;
else return 0;
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
// end of code.