/* 30/09/2021 * UARTSerialV5 * * Added an extra function repeatSound() that loops a trackCreating some useful functions * Serial MP3 Player A | Arduino UNO R3 RX | 3 TX | 4 VCC | 5V GND | GND */ #include "SoftwareSerial.h" #define ARDUINO_RX 4//should connect to TX of the Serial MP3 Player module #define ARDUINO_TX 3//connect to RX of the module SoftwareSerial myMP3(ARDUINO_RX, ARDUINO_TX); byte sendBuffer[6]; //buffer that will be used to store commands before sending //Manual says the code for this is 7E 03 35 01 EF so load into buffer array void selectTFCard(){ sendBuffer[0] = 0x7E; sendBuffer[1] = 0x03; sendBuffer[2] = 0x35; sendBuffer[3] = 0x01; sendBuffer[4] = 0xEF; sendUARTCommand(); } void playSound(byte songNumber){ sendBuffer[0] = 0x7E; sendBuffer[1] = 0x04; sendBuffer[2] = 0x41; sendBuffer[3] = 0x00; sendBuffer[4] = songNumber; sendBuffer[5] = 0xEF; sendUARTCommand(); } //play a sound at a set volume, only seems to apply to root directory files void playSoundAtVolume(byte volume,byte songNumber){ sendBuffer[0] = 0x7E; sendBuffer[1] = 0x04; sendBuffer[2] = 0x31; sendBuffer[3] = volume; sendBuffer[4] = songNumber; sendBuffer[5] = 0xEF; sendUARTCommand(); } //loop sound 7E 04 33 00 01 EF void repeatSound(byte songNumber){ sendBuffer[0] = 0x7E; sendBuffer[1] = 0x04; sendBuffer[2] = 0x33; sendBuffer[3] = 0x00; sendBuffer[4] = songNumber; sendBuffer[5] = 0xEF; sendUARTCommand(); } //set the playback volume 7E 03 31 0F EF = set volume to 0x0F = 15 void setplayVolume(byte volume){ sendBuffer[0] = 0x7E; sendBuffer[1] = 0x03; sendBuffer[2] = 0x31; sendBuffer[3] = volume; sendBuffer[4] = 0xEF; sendUARTCommand(); } //stop the current track playing void stopSound(){ sendBuffer[0] = 0x7E; sendBuffer[1] = 0x02; sendBuffer[2] = 0x0E; sendBuffer[3] = 0xEF; sendUARTCommand(); } void sendUARTCommand(){ int q; for(q=0;q < sendBuffer[1] + 2;q++){ myMP3.write(sendBuffer[q]); } Serial.println("Commands Sent"); for(q=0;q < sendBuffer[1] + 2;q++){ Serial.println(sendBuffer[q],HEX); } delay(25);//stops odd commands being missed } void setup() { Serial.begin(9600); Serial.println("UARTSerialV5"); myMP3.begin(9600); delay(500);//allow everything to settle down //first we need to select the TF Card selectTFCard(); delay(100); //new function to repeat the same track repeatSound(22);//repeat sound 12 } void loop() { // int q; /* //play through the first 5 tracks on the card for(q=0;q<15;q++){ playSound(q); delay(4000); stopSound(); } */ /* //Change the volume and then play track 4 for(q=0;q<30;q++){ setplayVolume(q);//change volume playSound(4);//play track 4 delay(2000); stopSound(); } */ /* ////Play track 4 with pre set volumes for(q=0;q<60;q +=10){//volume incremented in steps of 10 playSoundAtVolume(q, 4);//play track 4 at increasing volumes (q) delay(2000); stopSound(); } */ }