/* ESP8266DYSV5Wv7 Just plays track 5 with 2 sec gap after playing ESP8266 D1 mini Runs off Serial1 as only transmit is needed for sketch I have left Serial Print statements running so you can track what the sketch is doing, remove for production Player on Serial 1 (pin 2) busyPin = 4 //receives busy signal from DY-SV5W */ //Serial1 #define TXD2 2 //variables always required to work DY-SV5W const int busyPin = 4;//change to any suitable digital pin byte commandLength; byte command[6]; int16_t checkSum = 0; //my variables for demo sketch int trackNum = 1; int playStatus; //0 playing 1 stopped 2 waiting to start next track unsigned long lastCheckTime; unsigned long currentMillis; int busyPinstate; void playTrack(int soundTrack) { //select track Serial.print("soundTrack: "); Serial.println(soundTrack); command[0] = 0xAA;//first byte says it's a command command[1] = 0x07; command[2] = 0x02; command[3] = (soundTrack>>8) & 0xFF;//snh...track HIGH bit command[4] = soundTrack & 0xFF;//SNL... track low bit checkSum = 0; for (int q = 0; q < 5; q++) { checkSum += command[q]; //Serial.print(command[q]); //Serial.print(","); } //Serial.println(" "); //Serial.println(checkSum,BIN); //Serial.println(checkSum); //command[5] = lowByte(checkSum);//SM check bit... low bit of the sum of all previous values command[5] = (checkSum & 0xFF);//SM check bit... low bit of the sum of all previous values // Serial.println(command[5],HEX); // Serial.println(command[5],BIN); // Serial.println(command[5]); commandLength = 6; sendCommand(); } //plays whatever track has been paused or 1st track if nothing selected //May need to be selected after putting into random mode void play() { command[0] = 0xAA;//first byte says it's a command command[1] = 0x02; command[2] = 0x00; command[3] = 0xAC; commandLength = 4; sendCommand(); } //selects random mode void randomMode() { command[0] = 0xAA;//first byte says it's a command command[1] = 0x18; command[2] = 0x01; command[3] = 0x03;//random checkSum = 0; for (int q = 0; q < 4; q++) { checkSum += command[q]; } command[4] = (checkSum & 0xFF);//SM check bit... low bit of the sum of all previous values commandLength = 5; sendCommand(); //play() needs to be selected if you want the random tracks to start playing instantly play(); } //sets the device volume...0 - 30 void playbackVolume(int vol) { if (vol > 30) { //check within limits vol = 30; } command[0] = 0xAA;//first byte says it's a command command[1] = 0x13; command[2] = 0x01; command[3] = vol;//volume checkSum = 0; for (int q = 0; q < 4; q++) { checkSum += command[q]; } command[4] = (checkSum & 0xFF);//SM check bit... low bit of the sum of all previous values commandLength = 5; sendCommand(); } //sends the command void sendCommand() { int q; for (q = 0; q < commandLength; q++) { Serial1.write(command[q]); Serial.print(" "); Serial.print(command[q], HEX); } Serial.println(" End"); } void setup() { Serial.begin(115200); Serial1.begin(9600); Serial.println("ESP8266DYSV5Wv7"); pinMode(busyPin, INPUT);//pin to read from DY-SV5W buyPin playbackVolume(17);//sets volume to lvl 17 } void loop() { currentMillis = millis();//stores time in millis() for non blocking timings busyPinstate = digitalRead(busyPin); if (busyPinstate > 0) { //nothing playing if (playStatus < 1) { Serial.print(busyPinstate); playStatus = 1; lastCheckTime = currentMillis;//set system ready for 2 sec delay } } switch (playStatus) { Serial.print(" P: "); Serial.println(playStatus); case 1: if (currentMillis - lastCheckTime >= 2000) { //2000 = 2 seconds of silence between tracks playStatus = 2; //Just selecting a new track //this could have been something like //trackNum = random(1,5); // trackNum = 1;//fixed track number Serial.print("trackNum: "); Serial.println(trackNum); playTrack(trackNum); trackNum++; if(trackNum > 4){ trackNum = 1; } } break; case 2: if(busyPinstate < 1){ playStatus = 0; Serial.print(" P: "); Serial.println(playStatus); } break; default: break; } }