Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author c_alix on reddit
- #include <SPI.h>
- #include <Adafruit_VS1053.h>
- #include <SD.h>
- // These are the pins used for the breakout example
- #define BREAKOUT_RESET 9 // VS1053 reset pin (output)
- #define BREAKOUT_CS 10 // VS1053 chip select pin (output)
- #define BREAKOUT_DCS 8 // VS1053 Data/command select pin (output)
- // These are the pins used for the music maker shield
- #define SHIELD_RESET -1 // VS1053 reset pin (unused!)
- #define SHIELD_CS 7 // VS1053 chip select pin (output)
- #define SHIELD_DCS 6 // VS1053 Data/command select pin (output)
- // These are common pins between breakout and shield
- #define CARDCS 4 // Card chip select pin
- // DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
- #define DREQ 3 // VS1053 Data request, ideally an Interrupt pin
- Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
- int n_files;
- int led = 9;
- void setup()
- {
- Serial.begin(9600);
- Serial.println("Ready, Start!");
- pinMode(led, OUTPUT);
- pinMode(2,INPUT);
- // initialise the music player
- if (! musicPlayer.begin()) { // initialise the music player
- Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
- while (1);
- }
- Serial.println(F("VS1053 chip found..."));
- musicPlayer.sineTest(0x44, 500); // Make a tone to indicate VS1053 is working
- if (!SD.begin(CARDCS)) {
- Serial.println(F("SD failed, or not present"));
- while (1); // don't do anything more
- }
- Serial.println("SD Card Loaded...");
- Serial.println();
- musicPlayer.setVolume(1,1);
- n_files = countMP3File(SD.open("/"));
- Serial.print("Number of MP3 files: ");
- Serial.println(n_files);
- Serial.println();
- }
- int countMP3File(File dir)
- {
- int counter = 0;
- Serial.println("Listing files:");
- while(true)
- {
- File entry = dir.openNextFile();
- if (! entry)
- {
- dir.rewindDirectory();
- break;
- }
- Serial.println(entry.name());
- counter++;
- entry.close();
- }
- dir.close();
- Serial.println();
- return counter;
- }
- void loop()
- {
- int i, rand_song;
- Serial.print("Motion Sensor Val: ");
- Serial.println(digitalRead(2));
- if (digitalRead(2) == 1)
- {
- File folder = SD.open("/");
- File random_file;
- rand_song = random(0, n_files)+1;
- folder.rewindDirectory();
- random_file = selectFileN(rand_song, folder);
- folder.close();
- Serial.print("Playing: ");
- Serial.println(random_file.name());
- // smoke machine
- //digitalWrite(led, LOW);
- if(!musicPlayer.playFullFile(random_file.name()))
- {
- Serial.println("Could not open mp3 file");
- }
- random_file.close();
- // stop smoke machine
- //digitalWrite(led, HIGH);
- Serial.println("Waiting 6 sec...");
- delay(6000);
- }
- Serial.println();
- delay(100);
- }
- File selectFileN(int number, File dir)
- {
- int counter = 0;
- File return_entry;
- while(true)
- {
- File entry = dir.openNextFile();
- if (! entry)
- {
- //Serial.println("Last file reached");
- dir.rewindDirectory();
- break;
- }
- //Serial.println(entry.name());
- counter++;
- if(counter==number)
- {
- return_entry = entry;
- dir.rewindDirectory();
- break;
- }
- entry.close();
- }
- return return_entry;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement