Wave Shield Door Alarm / Welcome Music

When I enter my apartment I am welcomed home by a song playing on speakers. The door opening triggers an IR sensor which activates a Wave Shield. Only one song will play each time, and it plays a different song every time. After all the songs on the SD card have eventually been played it loops back to the beginning. You could potentially store up to 2 hours of songs on the card.
The contribution I wanted to make here is the Code. There is an If Statement that is activated when the sensor is activated. The original code would play every song in a row without stopping.

IR Sensor [ch8594] Arduino Wave Shield [ch8594] Speakers

#include <AF_Wave.h> //You will need the AF Wave library
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"

AF_Wave card;
File f;
Wavefile wave;      

#define redled 9

uint16_t samplerate;

int Power = 19;

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Wave test!");



  pinMode(2, OUTPUT); 
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(redled, OUTPUT);
  pinMode(Power, OUTPUT);
  
  if (!card.init_card()) {
    putstring_nl("Card init. failed!"); return;
  }
  if (!card.open_partition()) {
    putstring_nl("No partition!"); return;
  }
  if (!card.open_filesys()) {
    putstring_nl("Couldn't open filesys"); return;
  }

 if (!card.open_rootdir()) {
    putstring_nl("Couldn't open dir"); return;
  }

  putstring_nl("Files found:");
  ls();
}

void ls() {
  char name[13];
  int ret;
  
  card.reset_dir();
  putstring_nl("Files found:");
  while (1) {
    ret = card.get_next_name_in_dir(name);
    if (!ret) {
       card.reset_dir();
       return;
    }
    Serial.println(name);
  }
}

uint8_t tracknum = 0;

void loop() { 
 int sensor = 0;
 int sensorPin = 0;
  sensor = analogRead(sensorPin); 
  uint8_t i, r;
   char c, name[15];


   card.reset_dir();
   // scroll through the files in the directory
   
//------------------------------------------------  
   // THIS IS THE BUTTON IF STATEMENT!!!!
  //
  
    digitalWrite(Power, LOW);  //This turns the Speaker system
                              //Off using a powerSwitchTail relay
                             // when no music is playing.
                             // You don't need this if you
                             // want to leave your speakers on
  
   if (sensor > 300){    // 300 is for the IR distance sensor, 
                         // you can change that number depending
                         // on the type of sensor you use.
                         // This sensor is on pin Analog 0.
  
  digitalWrite(Power, HIGH);  //This turns the speakers ON using
                                //the powerswitchtail Relay
   for (i=0; i<tracknum+1; i++) {
     r = card.get_next_name_in_dir(name);
     if (!r) {
       // ran out of tracks! start over
       tracknum = 0;
       return;
     }
   }
   putstring("\n\rPlaying "); Serial.print(name);
   // reset the directory so we can find the file
   card.reset_dir();
   playcomplete(name);
   tracknum++;
}
  }
void playcomplete(char *name) {
  uint16_t potval;
  uint32_t newsamplerate;
  
  playfile(name);
  samplerate = wave.dwSamplesPerSec;
  while (wave.isplaying) {     
      // you can do stuff here!
      delay(500);
   }
  card.close_file(f);
}

void playfile(char *name) {
   f = card.open_file(name);
   if (!f) {
      putstring_nl(" Couldn't open file"); return;
   }
   if (!wave.create(f)) {
     putstring_nl(" Not a valid WAV"); return;
   }
   // ok time to play!
   wave.play();
}

I don't have a video demonstration, but this fellow used a different type of door sensor to make an arduino door alarm.

This code uses the Af Wave library.
Here is a website for all things Wave Shield:
http://www.ladyada.net/make/waveshield/index.html

One other thing that has been added is a relay to turn the speakers off when they are not being used. The speakers are plugged into a 'PowerSwitchTail' which is plugged into the wall. It is a short extension cord that can be turned on and off with a 5v input, so I treat it like an LED and turn it on when the music starts playing on the Wave Shield.
http://powerswitchtail.com/default.aspx

There are many possibilities for this system:
A door alarm,
a verbal welcome home message,
hello and goodbye messages or songs,
a different song for each day of the week,
a different song depending on the time of day,
a weight scale by the door that could identify different family members based on their weight and personalize a message.