Arduino & elechouse mp3 - trigger sounds

Hi

Apologies for my total cluelessness...

I feel like I've dug through the entirety of the internet and I can't find the solution to what I thought should be a fairly simple question.

I've got an arduino Uno and an elechouse mp3 shield v4.1 and HC SR04 ultrasonic sensor and I want to write something where a certain distance triggers an mp3 on the device to be played.

I can sort of control the sensor (make an led light up when there's objects at a certain distance) and I can make the mp3 shield play music. But I can't seem to figure out how to control it so the mp3 shield only plays music at certain events. All the codes I have found play the files automatically as soon as the device is connected.

On top of that I find the different codes I find very confusing. The one that comes in the library seems straightforward, but when I move the "play" command to the void loop section of the code I just get errors

#include <SoftwareSerial.h>
#include <MP3.h>

/** define mp3 class */
MP3 mp3;

void setup()
{
 /** begin function */
 mp3.begin(MP3_SOFTWARE_SERIAL);    // select software serial
//  mp3.begin();                       // select hardware serial(or mp3.begin(MP3_HARDWARE_SERIAL);)
 
 /** set volum to the MAX */
 mp3.volume(0x1F);
 
 /** set MP3 Shield CYCLE mode */
 mp3.set_mode(MP3::CYCLE);
 
 /** play music in sd, '0001' for first music */
 mp3.play_sd(0x0001);
 

}

void loop()
{

 

}

And then there are the other codes like this one that use some sort of hex code which I don't understand and can't find any explanation of.

//Written by : Mohannad Rawashdeh
// this code For MP3 Shield Elechosue
// Software serial interface


#include <SoftwareSerial.h>
SoftwareSerial Geno(7,8); // Rx , Tx
unsigned char cmd_buf[10];
unsigned char i;

void ArduinoMP3Shield_SendCMD(unsigned char *cmd_buf, unsigned len)
{
   unsigned i;
   for(i=0; i<len; i++){
       Geno.write(cmd_buf[i]);
   }
}

void setup(void)
{
   /** wait until arduino mp3 shield get ready */
   delay(1000);

   Geno.begin(9600);
  
   /** set volume */
   cmd_buf[0] = 0x7E;          // START
   cmd_buf[1] = 0x03;          // Length
   cmd_buf[2] = 0xA7;          // Command
   cmd_buf[3] = 0x0F;          // new volume
   cmd_buf[4] = 0x7E;          // END
   ArduinoMP3Shield_SendCMD(cmd_buf, 5);
  
   /** set play mode repeat all */
   cmd_buf[0] = 0x7E;          // START
   cmd_buf[1] = 0x03;          // Length
   cmd_buf[2] = 0xA9;          // Command SET MODE
   cmd_buf[3] = 0x02;          // set mode
   cmd_buf[4] = 0x7E;          // END
   ArduinoMP3Shield_SendCMD(cmd_buf, 5);
  
   /** select SD card first music and play */
   cmd_buf[0] = 0x7E;          // START
   cmd_buf[1] = 0x04;          // Length
   cmd_buf[2] = 0xA0;          //  For U Disk change this line to 0xA2 
   cmd_buf[3] = 0x00;          // file number high byte
   cmd_buf[4] = 0x01;          // file number low byte
   cmd_buf[5] = 0x7E;          // END
   ArduinoMP3Shield_SendCMD(cmd_buf, 6);
}

void loop(void)
{
  
}

Again, moving the bit that plays the music to the void loop section just breaks everything.

I've also read the manual for the shield and that just confuses me more.

Please help out a clueless newb!

My wonderful fantastic most loveable friend helped me find the solution (which now seems super obvious)
so I figured I'd share it here in case anyone else is struggling with a similar thing.

Basically what I needed to do was simply DEFINE a function for the mp3 to start playing, and then I can call it up in the code below.

I'm copying in the simple version below. This is without the ultrasound trigger, but once I had that kink worked out everything else makes total sense :slight_smile:

#include <SoftwareSerial.h>
#include <MP3.h>

/** define mp3 class */
MP3 mp3;

void setup()
{
  /** begin function */
  mp3.begin(MP3_SOFTWARE_SERIAL);    // select software serial
//  mp3.begin();                       // select hardware serial(or mp3.begin(MP3_HARDWARE_SERIAL);)
  
  /** set volum to the MAX */
  mp3.volume(0x1F);
  
  /** set MP3 Shield CYCLE mode */
  mp3.set_mode(MP3::SINGLE);
  
  /** play music in sd, '0001' for first music */
  /*mp3.play_sd(0x0001);
  
  /** play music in USB-disk */ 
  //mp3.play_usb_disk(0x0001);
  
  /** play music in SPI FLASH */ 
  //mp3.play_spi_flash(0x0001);
}
void playSound(){
mp3.play_sd(0x0001);
  
}

void loop()
{
playSound();
//delay(20000);

  

  
 
}