How do I reset variable after song finishes playing?

I'm using the Serial MP3 Player with a mega 2560. I've got it working so that a PIR sensor starts the song, but once the song is over, I want to reset a variable, essentially setting it up to play again if triggered. How do I tell when the song is finished?

which one? a link would help...

As would seeing your code posted as described in

The module is the YX5300 Serial MP3 Player. YX5300 Serial MP3 Player (Catalex Module) – Arduino++ (wordpress.com)

That module sends you a message to tell you that the song is finished. You need to process unsolicited messages from the device.

If you are using the MD_YX5300 library, look at the examples provided with the library and read the library documentation.

/*
Project: Serial MP3 Player module
Function: Plays the first song in the micro sd card when motion is detected
*/
//*************************************************************************
#include <SoftwareSerial.h>//include library code
//*************************************************************************
#define ARDUINO_RX 5//connect to TX pin of the Serial MP3 Player module
#define ARDUINO_TX 4//connect to RX pin of the Serial MP3 Player module
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);//setup a myserial object
static int8_t Send_buf[8] = {0} ;
//define the commands
#define CMD_PLAY_W_INDEX 0X03
#define CMD_SET_VOLUME 0X06
#define CMD_SEL_DEV 0X09
#define DEV_TF 0X02
#define CMD_PLAY 0X0D
#define CMD_PAUSE 0X0E
#define CMD_SINGLE_CYCLE 0X19
#define SINGLE_CYCLE_ON 0X00
#define SINGLE_CYCLE_OFF 0X01
#define CMD_PLAY_W_VOL 0X22
int playmode = 0; // song is not playing

//******
//Motion sensor code
int inputPin = 2; // The input pin of the PIR sensor
int pirState = LOW; // initially, we assume that there is no motion detected
int val = 0; // variable for reading the pin status
//*************************************************************************
void setup()
{
  Serial.begin(9600);
  //* Motion sensor code ****
  pinMode(inputPin, INPUT); // declare sensor as input
  mySerial.begin(9600);//initialize serial communication at 9600 bps
  delay(500);//Wait chip initialization is complete
  //Delay for setup
  Serial.println("System Initializing...");
  delay(30000);//wait for 30 seconds
  Serial.println("System is Ready!");  
}

String str;

void loop()
{
  if (mySerial.available()>0){
    mySerial.read();
  }
  
  //Serial.println("Loop Started!"); 
  if (playmode == 0) {
    //* Motion Sensor Code *
    val = digitalRead(inputPin); // reads the input value
    if (val == HIGH) { // checks if the input is HIGH
      delay(150);
      if (pirState == LOW) {
        Serial.println("Motion detected!");
        pirState = HIGH;
        //Play Song
        sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card
        delay(200);//wait for 200ms
        Serial.println("Playing Song!");
        sendCommand(CMD_PLAY_W_VOL, 0X0F01);//0X0F01 - PLAYS FIRST SONG  - you can change and play another, for example 0X0F02 SECOND SONG, 0X0F03 THIRD SONG....with volume 15 class     
        playmode = 1;
      }
    } 
    else {
      delay(300);
      if (pirState == HIGH){
        Serial.println("Motion ended!");
        pirState = LOW;
      }
    }
  }
}
//*************************************************************************
void sendCommand(int8_t command, int16_t dat)
{
  delay(20);
  Send_buf[0] = 0x7e; //starting byte
  Send_buf[1] = 0xff; //version
  Send_buf[2] = 0x06; //the number of bytes of the command without starting byte and ending byte
  Send_buf[3] = command; //
  Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
  Send_buf[5] = (int8_t)(dat >> 8);//datah
  Send_buf[6] = (int8_t)(dat); //datal
  Send_buf[7] = 0xef; //ending byte
  for (uint8_t i = 0; i < 8; i++) //
  {
    mySerial.write(Send_buf[i]) ;
  }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.