need help with script. not working correctly.

the goal is to make the arduino play the second song from startup and only play the first song if the hall sensor detects a magnetic field. rin it plays the first song after startup and when I hit the rest button it does what it should do. Also after hitting the reset button after a minute or so the 1st song starts playing. how can I fix this?

#include <SoftwareSerial.h>
#define ARDUINO_RX 5//should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 6//connect to RX of the module
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);
static int8_t Send_buf[8] = {0} ;

#define CMD_SEL_DEV 0X09
#define DEV_TF 0X02
#define CMD_PLAY_W_VOL 0X22
#define CMD_PLAY 0X0D
#define CMD_PAUSE 0X0E
#define CMD_PREVIOUS 0X02
#define CMD_NEXT 0X01

int SENSOR = 8 ; // define the Hall magnetic sensor
int val ; // define numeric variables

void setup()
  delay(500);//Wait chip initialization is complete
  sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card
  delay(200);//wait for 200ms


void loop() {

  val = digitalRead (SENSOR) ; // read sensor line
  if (val == LOW) // when the Hall sensor detects a magnetic field, Arduino LED lights up
    sendCommand(CMD_PLAY_W_VOL, 0X1E01);//play the first song with volume 30 class
    Serial.println("Magnetic field detected");
    sendCommand(CMD_PLAY, 0X1E02);//play the first song with volume 30 class
    Serial.println("No magnetic field detected");
void sendCommand(int8_t command, int16_t dat)
  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]) ;

What is Serial Monitor showing for messages?

What device is connected to Pin 5 and Pin 6?

What type of Hall magnetic sensor do you have? Did you add a resistor ?

I won't pretend to have an answer but why does line 23 have DEV_TF when lines 36 and 42 have hex numbers?


the serial monitor is showing the correct things: a megnetic field is detected or a magnetic field is not detected. pin 5 & 6 are connected to a catalex serial mp3 player.

I followed a guide online on how to set up the code for the serial mp3 player and followed that.

I won't pretend to have an answer but why does line 23 have DEV_TF when lines 36 and 42 have hex numbers?


Since it's a serial interface to your MP3 player what happens if you blast requests through the serial? Shouldn't you wait for the song to finish playing?

I'll try that and see if it works

You are using " sendCommand(CMD_PLAY, 0X1E02);" where you apparently want to use
" sendCommand(CMD_PLAY_W_INDEX, 0X0002);" or " sendCommand(CMD_PLAY_W_VOL, 0X1E02);". The CMD_PLAY command resumes play, it doesn't use a file index.

thx everyone for their help. I fixed it