Using an ultrasonic sensor in tandem with an MP3 Shield

hey everyone, I am currently trying to a project involving both an MP3 Shield as well as an ultrasonic sensor. I am trying to make it so that based on certain distances that are sensed by the ultrasonic senor triggers my MP3 shield to play a track. Currently I have it set to play "Song of Storms" when the distance value is greater than 40 and less than 50. The only problem is that it is not triggering the song AT ALL. Please help!

// Volume Variables
int potPin = 2;
int volumeRaw = 0;
int volumeVar = 0;

// Sensor Variables
int echoPin = 7; // Echo Pin
int trigPin = 8; // Trigger Pin
int maximumDistance = 100; // Maximum range needed
int minimumDistance = 3; // Minimum range needed
long duration, distance; // Duration used to calculate distance

// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// These are the pins used for the breakout example
#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer = 
  // create breakout-example object!
  //Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
  // create shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);


////

void setup() {
  Serial.begin(9600);
  Serial.println("Adafruit VS1053 Library Test");
  // Setup for Sensor
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  // initialise the music player
  if (! musicPlayer.begin()) { // initialise the music player
     Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
     while (1);
  }
  Serial.println(F("VS1053 found"));

  musicPlayer.sineTest(0x44, 500);    // Make a tone to indicate VS1053 is working
 
  if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);  // don't do anything more
  }
  Serial.println("SD OK!");
  
  // list files
  printDirectory(SD.open("/"), 0);
  


  /***** Two interrupt options! *******/ 
  // This option uses timer0, this means timer1 & t2 are not required
  // (so you can use 'em for Servos, etc) BUT millis() can lose time
  // since we're hitchhiking on top of the millis() tracker
  //musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT);
  
  // This option uses a pin interrupt. No timers required! But DREQ
  // must be on an interrupt pin. For Uno/Duemilanove/Diecimilla
  // that's Digital #2 or #3
  // See http://arduino.cc/en/Reference/attachInterrupt for other pins
  // *** This method is preferred
  if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
    Serial.println(F("DREQ pin is not an interrupt pin"));
}

void loop() {  
  //Sensor
        

         /* The following trigPin/echoPin cycle is used to determine the
         distance of the nearest object by bouncing soundwaves off of it. */ 
         digitalWrite(trigPin, LOW); 
         delayMicroseconds(2); 

         digitalWrite(trigPin, HIGH);
         delayMicroseconds(10); 
 
         digitalWrite(trigPin, LOW);
         duration = pulseIn(echoPin, HIGH);
    
         // some code to find duration of pulse...
         if (duration == 0) {
         pinMode(echoPin,OUTPUT);
         digitalWrite(echoPin,LOW);
         delay(100);
         pinMode(echoPin,INPUT);
         }
     
         //Calculate the distance (in cm) based on the speed of sound.
         distance = duration/58.2;
     
         if (distance >= maximumDistance){
         /* Send a negative number to computer and Turn LED ON 
         to indicate "out of range" */
         Serial.println("Get closer!");
         }
         else if (distance <= minimumDistance){
         /* Send a negative number to computer and Turn LED ON 
         to indicate "out of range" */
         Serial.println("Too close!");
         }else{
             /* Send the distance to the computer using Serial protocol, and
             turn LED OFF to indicate successful reading. */
             Serial.println(distance);  
        }
         //Delay 50ms before next reading.
         delay(100);
     
       if(distance > 40 && distance < 50){
            // Play one file, don't return until complete        
            Serial.println(F("Playing track 001"));
            musicPlayer.startPlayingFile("track001.mp3");

            while(musicPlayer.playingMusic){
              volumeControl();
            }
              
       }
    
}  



/// File listing helper
void printDirectory(File dir, int numTabs) {
   while(true) {
     
     File entry =  dir.openNextFile();
     if (! entry) {
       // no more files
       //Serial.println("**nomorefiles**");
       break;
     }
     for (uint8_t i=0; i<numTabs; i++) {
       Serial.print('\t');
     }
     Serial.print(entry.name());
     if (entry.isDirectory()) {
       Serial.println("/");
       printDirectory(entry, numTabs+1);
     } else {
       // files have sizes, directories do not
       Serial.print("\t\t");
       Serial.println(entry.size(), DEC);
     }
     entry.close();
   }
}

//Volume function
void volumeControl(){
        // Volume Knob Setup
        volumeRaw = analogRead(potPin);
        volumeVar = volumeRaw/30;
    
        //  Set volume for left, right channels. lower numbers == louder volume!
        musicPlayer.setVolume(volumeVar,volumeVar);
        delay(100);
}

Thanks!

You appear to be using Pin 7 and Pin 8 for two purposes each. That is NOT going to go well.

johnwasser:
You appear to be using Pin 7 and Pin 8 for two purposes each. That is NOT going to go well.

Thank you! It's such an obvious mistake that I'm surprised that I didn't catch it sooner!