I first want to say I am new to Arduino and programming so I am having a little trouble grasping some of the concepts, or at least that is what I think I am doing wrong, amoung other things I am sure.
I have gotten some help from a few places, but I have been given more of vague references that I do not understand full, but I have included a copy of my code below.
I am using a Adafruit “Music Maker” MP3 Shield for Arduino w/3W Stereo Amp and an Arduino Mega 2560. I am trying to get the Adafruit_VS1053, “player_interrupts” code to run and play a random MP3 stored on SD card inserted into a Adafruit “Music Maker” MP3 Shield. I have not been able to figure out how to tell the code to play a random file. I can only get it to play if I specify the file name specifically in the code. I have tried placing a txt file, (MP3s.txt) on the SD card and read that into an array to pass to the line but I have not been able to get that to work either. The latest version of my code tries to take advantage of the random() function to get the file names; I played around with it and got it to run without errors, but when I upload it to my Arduino, it plays the beep, lists the files on the SD card, and stops.
// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////\
// 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);
void setup() {
Serial.begin(9600);
Serial.println("Adafruit VS1053 Library Test");
// 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 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 OK!");
// list files
printDirectory(SD.open("/"), 0);
// Set volume for left, right channels. lower numbers == louder volume!
musicPlayer.setVolume(20, 20);
if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
Serial.println(F("DREQ pin is not an interrupt pin"));
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
// put your main code here, to run repeatedly:
File path = SD.open("/");
File results;
char* MP3 = selectRandomFileFrom( path, results );
Serial.println(MP3);
delay(1000);
// Start playing a file, then we can do stuff while waiting for it to finish
if (! musicPlayer.startPlayingFile(MP3)) {
Serial.println("Could not open file: ");
//Serial.println(MP3);
//while (1);
}
Serial.println(F("Started playing"));
while (musicPlayer.playingMusic) {
// file is now playing in the 'background' so now's a good time
// to do something else like handling LEDs or buttons :)
Serial.print(".");
delay(1000);
}
Serial.println("Done playing music");
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// File listing helper
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
//Serial.println("**nomorefiles**");
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();
}
}
// Function to select random mp3
char* selectRandomFileFrom( File dir, File result ) {
File entry;
int count = 1;
dir.rewindDirectory();
while ( entry = dir.openNextFile() ) {
if ( random( count ) == 0 ) {
result = entry;
}
entry.close();
count++;
}
}
I am using this version of the code:
// Start playing a file, then we can do stuff while waiting for it to finish
if (! musicPlayer.startPlayingFile(MP3)) {
Serial.println("Could not open file: ");
//Serial.println(MP3);
//while (1);
}
Serial.println(F("Started playing"));
while (musicPlayer.playingMusic) {
// file is now playing in the 'background' so now's a good time
// to do something else like handling LEDs or buttons :)
Serial.print(".");
delay(1000);
because I need to run a couple of servos while the MP3 are playing.