mp3 ultrasonic trigger

Hi everyone i’m having trouble with a proyect: this code is supposed to low volume as someone gets closer to the ultrasound sensor:

//libraries -------------------------------
#include <Ultrasonic.h>
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>

#define MAX_DISTANCE 280 // maximum distance

//ULTRASOUND PINS
#define trigPin 4
#define echoPin 3


SdFat sd;   //SD OBJ DECLARATION
SFEMP3Shield MP3player;   //MP3 SHIELD OBJ DECLARATION
int8_t current_track = 1;// TRACK NAME SHOULD BE "track001.mp3"
uint8_t vol ;

// US SENSOR PINS AND MAX DISTANCE DECLARATION
Ultrasonic ultrasonido(trigPin, echoPin, MAX_DISTANCE * 58);// la maxima distancia se multiplica por 58 (asi funciona la libreria)

int volumen = 0;  //STARTING AT MAXIMUM VOLUME
uint8_t setvol;


void setup() {
  Serial.begin(9600);  //STARTING SERIAL COM 
 

//MP3 SHIELD COM STARTING--------------------------------------
  if (!sd.begin(9, SPI_HALF_SPEED)) sd.initErrorHalt();
  if (!sd.chdir("/")) sd.errorHalt("sd.chdir");
  MP3player.begin();
  MP3player.setVolume(volumen, volumen);
//MP3 SHIELD COM STARTING--------------------------------------
}

void loop()
{
    // IF SOME ERROR----------------------------------------------
    #if defined(USE_MP3_REFILL_MEANS) \
        && ( (USE_MP3_REFILL_MEANS == USE_MP3_SimpleTimer) \
        ||   (USE_MP3_REFILL_MEANS == USE_MP3_Polled)      )
      MP3player.available();
    #endif
    //------------------------------------------------------------
    
      MP3player.playTrack(current_track);//LOADS track001.mp3  
    
      // WATING 1 SEC BETWEEN PULSES
      delay(1000);
      int distancia; //SAVES DISTANCE
      distancia = ultrasonido.Ranging(CM); //DISTANCE IN CMs
      //PRINTING DISTANCE ON SERIAL
      Serial.print(distancia);
      Serial.println("cm");
    //----------------------------------------------
      union twobyte mp3_vol; // create key_command existing variable that can be both word and double byte of left and right.
          mp3_vol.word = MP3player.getVolume(); // returns a double uint8_t of Left and Right packed into int16_t
     
    if (distancia>0 && distancia <= 40 ) {
     vol = (uint8_t)(255);
    }
    if (distancia>40 && distancia <= 50 ) {
     vol = (uint8_t)(240);
    }
    if (distancia>50 && distancia <= 60 ) {
     vol = (uint8_t)(230);
    }
    if (distancia>60 && distancia <= 70 ) {
     vol = (uint8_t)(220);
    }
    if (distancia>70 && distancia <= 80 ) {
     vol = (uint8_t)(210);
    }
    if (distancia>80 && distancia <= 90 ) {
     vol = (uint8_t)(200);
    }
    if (distancia>90 && distancia <= 100 ) {
     vol = (uint8_t)(190);
    }
    if (distancia>100 && distancia <= 110 ) {
     vol = (uint8_t)(180);
    }
    if (distancia>110 && distancia <= 120 ) {
     vol = (uint8_t)(170);
    }
    if (distancia>120 && distancia <= 130 ) {
     vol = (uint8_t)(160);
    }
    if (distancia>130 && distancia <= 140 ) {
     vol = (uint8_t)(150);
    }
    if (distancia>140 && distancia <= 150 ) {
     vol = (uint8_t)(140);
    }
    if (distancia>150 && distancia <= 160 ) {
     vol = (uint8_t)(130);
    }
    if (distancia>160 && distancia <= 170 ) {
     vol = (uint8_t)(120);
    }
    if (distancia>170 && distancia <= 180 ) {
     vol = (uint8_t)(110);
    }
    if (distancia>180 && distancia <= 190 ) {
     vol = (uint8_t)(100);
    }
    if (distancia>190 && distancia <= 200 ) {
     vol = (uint8_t)(90);
    }
    if (distancia>200 && distancia <= 210 ) {
     vol = (uint8_t)(80);
    }
    if (distancia>210 && distancia <= 220 ) {
     vol = (uint8_t)(70);
    }
    if (distancia>220 && distancia <= 230 ) {
     vol = (uint8_t)(60);
    }
    if (distancia>230 && distancia <= 240 ) {
     vol = (uint8_t)(50);
    }
    if (distancia>240 && distancia <= 250 ) {
     vol = (uint8_t)(40);
    }
    if (distancia>250 && distancia <= 260 ) {
     vol = (uint8_t)(30);
    }
    if (distancia>260 && distancia <= 270 ) {
     vol = (uint8_t)(20);
    }
    if (distancia>270 && distancia <= 280 ) {
     vol = (uint8_t)(10);
    }
    if (distancia>280 ) {
     vol = (uint8_t)(0);
    }
     MP3player.setVolume(vol, vol);//SETS NEW VOLUME
}

The thing is when when the mp3 shield gets started, for some reason, the ultrasound turns on 0, I don’t know why, and I don’t know hot to solve it. I’ve tried comenting some code lines, that way i found the problem on that mp3 begin lines, but I can’t just throw that away cause my track wont ever reproduce. The proyect is using an HC-SR04 sensor conected on 5 and 10 pins of the MP3 sparkfun shield (SparkFun MP3 Player Shield - DEV-12660 - SparkFun Electronics), if someone could help me up with this would be nice.

PD: here just getting started with the forum, if you can give me any advice on how to post “correctly” I will be grateful.

Look at the schematic or documentation and you will see that the shield uses pins 3 and 4 so you probably should not use those for your ultrasonic sensor.

I’ve tried changing the TRIGGER and ECHO PINS to 5 and 10 respectively, cause the shield is not using them and the serial monitor is still showing 0.

You are using the SPI library to talk to an SD card. Unfortunately Pin 10 is the hardware Slave Select pin. If you change it to an INPUT you will flip the SPI interface from Master to Slave and you won't be able to use the SD card. You must leave Pin 10 as an OUTPUT. Try using 5 for ECHO and 10 for TRIGGER.

Hi, in my experience, MP3 sparkfun shield doesn´t work with the HC-SR04 sensor, so you have 2 options:

use other similar mp3 shield like: http://www.geeetech.com/wiki/index.php/Arduino_MP3_shield_board_with_TF_card using pins 4 and 5 for echo and trig

or

use other sensor like https://www.maxbotix.com/Ultrasonic_Sensors/MB1000.htm that only use one pin on the MP3 sparkfun,

we are trying to make some similiar like your project.. hope work it