/* Open all files in the root dir and print their filename and modify date/time */ //Add the SdFat Libraries #include #include //and the MP3 Shield Library #include // Below is not needed if interrupt driven. Safe to remove if not using. #if defined(USE_MP3_REFILL_MEANS) && USE_MP3_REFILL_MEANS == USE_MP3_Timer1 #include #elif defined(USE_MP3_REFILL_MEANS) && USE_MP3_REFILL_MEANS == USE_MP3_SimpleTimer #include #endif /** \brief Object instancing the SdFat library. principal object for handling all SdCard functions. */ SdFat sd; /** \brief Object instancing the SFEMP3Shield library. principal object for handling all the attributes, members and functions for the library. */ SFEMP3Shield MP3player; /** \brief The PIN for our SD-card reader. This is pin 9 on my board. */ const uint8_t chipSelect = 9; // define a serial output stream ArduinoOutStream cout(Serial); /** \brief Find and play a random file This function reads ALL the files on the SD-card, to count them. Once it knows how many files are present it will pick a random one, and start playing that particular track. */ void play_random_file() { char filename[20]; int count = 0; // Count the files. SdFile file; sd.chdir("/", true); while (file.openNext(sd.vwd(), O_READ)) { count += 1; file.close(); } // Pick a random one int r = random( 0, count + 1); // Loop over all the files a second time - if our current // index equals the random number then that's the one we'll // play. sd.chdir("/", true); count = 0; while (file.openNext(sd.vwd(), O_READ)) { if ( count == r ) { file.getFilename(filename); Serial.print( "Playing the random filename " ); Serial.println( filename ); int8_t result = MP3player.playMP3(filename); if (result != 0) { Serial.print(F("Error code: ")); Serial.print(result); Serial.println(F(" when trying to play the track!")); } } file.close(); count += 1; } } //------------------------------------------------------------------------------ void setup() { // If analog input pin 0 is unconnected, random analog // noise will be read 32 times and the lowest bit updated // // This code came from the following link: // http://www.utopiamechanicus.com/article/better-arduino-random-numbers/ // unsigned long seed = 0, count = 32; while (--count) seed = (seed << 1) | (analogRead(0) & 1); randomSeed(seed); // Configure the serial console output. Serial.begin(9600); // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with // breadboards. use SPI_FULL_SPEED for better performance. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt(); //Initialize the MP3 Player Shield int result = MP3player.begin(); if ((result != 0) && (result != 6)) { Serial.print(F("Error code: ")); Serial.print(result); Serial.println(F(" when trying to start MP3 player")); } // Setup some volume. MP3player.setVolume(40, 40); // Swallow pending input while (Serial.available()) { Serial.read(); } } //------------------------------------------------------------------------------ void loop() { // If the player is not playing then pick a song and start it if ( ! MP3player.isPlaying() ) play_random_file(); }