/* DYSV5Wv8 Just plays throgh tracks with 2 sec gap after playing When track 5 plays (arc welder) the led on pin 9 with do arc flicker Mega 2560 Player on Serial 1 (pins 18-19) busyPin = 8 //receives busy signal from DY-SV5W arcPin = 9 //pin for arc welder led 330ohm resistor */ //variables always required to work DY-SV5W const int busyPin = 8;//change to any suitable digital pin byte commandLength; byte command[6]; int 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; //Arc Welder const int arcPin = 9;//pin used unsigned long arcTimer;//keeps track of when next arc change is due int arcTimerPeriod = 50;//intial value but will change all the time byte arcState = 0; //0 = off, 1 = ON...should the welder be running byte arcLEDState;//0 = off, 1 = ON...current state of led 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] = highByte(soundTrack);//snh...track HIGH bit command[4] = lowByte(soundTrack);//SNL... track low bit checkSum = 0; for (int q = 0; q < 5; q++) { checkSum += command[q]; } command[5] = lowByte(checkSum);//SM check bit... low bit of the sum of all previous values 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] = lowByte(checkSum);//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] = lowByte(checkSum);//SM check bit... low bit of the sum of all previous values commandLength = 5; sendCommand(); } //sends the command to the DY-SV5W void sendCommand() { int q; for (q = 0; q < commandLength; q++) { Serial1.write(command[q]); Serial.print(command[q], HEX); } Serial.println("End"); } //arc welder effect //this will cause the LED in pin 9 to flash intermittently. void arcWelder() { if (arcState > 0) {//if arc state is set if (currentMillis - arcTimer > arcTimerPeriod) { arcTimer = currentMillis; if (arcLEDState < 1) { //turn on arcLEDState = 1; //on arcTimerPeriod = random(40, 100);//set a new random timer period } else { //turn off arcLEDState = 0; //off arcTimerPeriod = random(20, 80);//different random for off period } digitalWrite(arcPin, arcLEDState); //set the LED to new state } } else { arcLEDState = 0; digitalWrite(arcPin, arcLEDState); //turn the LED off if arc welder not running arcState = 0; } } void setup() { Serial.begin(115200); Serial1.begin(9600); Serial.println("DYSV5Wv8"); pinMode(busyPin, INPUT);//pin to read from DY-SV5W buyPin pinMode(arcPin, OUTPUT); //out put pin for LED flicker playbackVolume(17);//sets volume to lvl 17 } void loop() { currentMillis = millis();//stores time in millis() for non blocking timings busyPinstate = digitalRead(busyPin);//check the busyPin state if (busyPinstate > 0) { //nothing playing if (playStatus < 1) {//test playStatus to make sure we are not in waiting period between tracks arcState = 0;//turn arc status off...nothing should be running playStatus = 1; //set playstatus to next mode. lastCheckTime = currentMillis;//set system ready for 2 sec delay } } //works thorugh the various playStatus switch (playStatus) { case 1: if (currentMillis - lastCheckTime >= 2000) { //2000 = 2 seconds of silence between tracks playStatus = 2;//set the playStatus to next mode //if the trackNum selected is 5 turn the arc welder flash on. if(trackNum == 5){ arcState = 1; } playTrack(trackNum);//play the track //Just selecting a new track //this could have been something like //trackNum = random(1,5); trackNum++; if(trackNum > 5){//I only have 5 tracks on SD Card trackNum = 1; } //end of new track selection section } break; case 2: //resets system for next track if(busyPinstate < 1){ playStatus = 0; } break; default: break; } arcWelder();//deal with arc welder routine }