/*****************************************************************************/ // Function: MoodBox // Hardware: Grove - Serial MP3 Player // author : seeedstudio, arduino example and Alexandre Guerre /*******************************************************************************/ #include SoftwareSerial mp3(2, 3);// (RX,TX) modify this with the connector you are using. const int buttonPin = 11; int buttonState = 0; int max_song = 0; int lastButtonState = LOW; // the previous reading from the input pin // the following variables are long's because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long lastDebounceTime = 0; // the last time the output pin was toggled long debounceDelay = 50; // the debounce time; increase if the output flickers void setup() { //to let the MP3 boot in peace delay(5000); // initialise the interface mp3.begin(9600); Serial.begin(9600); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); // initialise the player if (true == SetPlayMode(0x00)) Serial.println("Set The Play Mode to 0x00, Single Mode."); else Serial.println("Playmode Set Error"); StopCurrentMusic(); //SetVolume(0x0E); SetVolume(0x1F); //Check the SD content max_song = GetMusicCount(); Serial.print("The SD contains "); Serial.println(max_song); } void loop() { int index = 0; // read the state of the switch into a local variable: int reading = digitalRead(buttonPin); // If the switch changed, due to noise or pressing: if (reading != lastButtonState) { // reset the debouncing timer lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { // whatever the reading is at, it's been there for longer // than the debounce delay, so take it as the actual current state: // if the button state has changed: if (reading != buttonState) { buttonState = reading; // only play a sound if the new button state is HIGH if (buttonState == HIGH) { index = random(1, max_song + 1); Serial.println(index); SetMusicPlay(00, index); //limite to 255 sounds } } } // save the reading. Next time through the loop, // it'll be the lastButtonState: lastButtonState = reading; } //Set the music index to play, the index is decided by the input sequence //of the music; //hbyte: the high byte of the index; //lbyte: the low byte of the index; boolean SetMusicPlay(uint8_t hbyte, uint8_t lbyte) { mp3.write(0x7E); mp3.write(0x04); mp3.write(0xA0); mp3.write(hbyte); mp3.write(lbyte); mp3.write(0x7E); delay(10); while (mp3.available()) { if (0xA0 == mp3.read()) return true; else return false; } } // Pause on/off the current music boolean PauseOnOffCurrentMusic(void) { mp3.write(0x7E); mp3.write(0x02); mp3.write(0xA3); mp3.write(0x7E); delay(10); while (mp3.available()) { if (0xA3 == mp3.read()) return true; else return false; } } // Stop 7E 02 A4 7E boolean StopCurrentMusic(void) { mp3.write(0x7E); mp3.write(0x02); mp3.write(0xA4); mp3.write(0x7E); delay(10); while (mp3.available()) { if (0xA4 == mp3.read()) return true; else return false; } } //Set the volume, the range is 0x00 to 0x1F boolean SetVolume(uint8_t volume) { mp3.write(0x7E); mp3.write(0x03); mp3.write(0xA7); mp3.write(volume); mp3.write(0x7E); delay(10); while (mp3.available()) { if (0xA7 == mp3.read()) return true; else return false; } } //read volume 7E 02 C1 7E return 0xC1 plus one word int GetVolume(void) { mp3.write(0x7E); mp3.write(0x02); mp3.write(0xC1); mp3.write(0x7E); delay(10); while (mp3.available()) { if (0xC1 == mp3.read()) return mp3.read(); else return -1; } } //7E 02 C4 7E return 0xC4 plus value on 2 words int GetMusicCount(void) { int response = 0; mp3.write(0x7E); mp3.write(0x02); mp3.write(0xC4); mp3.write(0x7E); delay(10); while (mp3.available()) { if (0xC4 == mp3.read()) { for (int i = 1; i >= 0; i--) { response += (mp3.read()) << (8 * i); } return response; } else return -1; } } boolean SetPlayMode(uint8_t playmode) { if (((playmode == 0x00) | (playmode == 0x01) | (playmode == 0x02) | (playmode == 0x03)) == false) { Serial.println("PlayMode Parameter Error! "); return false; } mp3.write(0x7E); mp3.write(0x03); mp3.write(0xA9); mp3.write(playmode); mp3.write(0x7E); delay(10); while (mp3.available()) { if (0xA9 == mp3.read()) return true; else return false; } }