Senior Project
PromptThe goal of my project was to figure out what an Arduino is and what is it able to do. After figuring out what an Arduino is i decided i would use it to make a mp3 player as a fun project that i could use.
ReflectionI now have a black box that people question that all the time that purpose is to play music. If i was to do this again i would not spend as much money on getting a shield and i would instead get a cheap Mp3 player disassemble it then at the parts of the mp3 player would be attached to the Arduino and that would require some coding that i wont know how to do.
|
// 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); //initialize the serial monitor uint32_t seed = 0; // Generate random see start for ( uint8_t i = 10 ; i ; i-- ) { seed = ( seed << 5 ) + ( analogRead( 0 ) * 3 ); } randomSeed( seed ); //set random seed Serial.println("Adafruit VS1053 Library Test"); Serial.print("RandomSeed value = "); Serial.println(seed); // 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")); 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(0, 0); 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 ); delay(100); // 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); } Serial.print(F("Now playing ")); Serial.println(MP3); while (musicPlayer.playingMusic) { delay(250); //Serial.print("."); ////////////////////////////////////////////////////////////////////////////////////////////////////////// } } //////////////////////////////////////////////////////////////////////////////////////////////////// /// 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 = 0; dir.rewindDirectory(); while ( entry = dir.openNextFile() ) { if ( random( count ) == 0 ) { result = entry; } entry.close(); count++; } return result.name(); // returns the randomly selected file name } |
|