Steppenwolf

Hi there, I'm working on a project where when someone enters the bathroom, a music track randomly selected begins to play, and then shuts off when motion is no longer detected. A test sketch that plays the track over and over, without the PIR, proves that the hardware is sound, and now I've attached the PIR to pin 0 and the 5v and ground, and added four more tracks to the SD card.

Now I'm not really sure where to go. I've done some searching around this forum and the adafruit forums and I cannot find a proper sketch for this, and the fact that I am new to all this means I don't really know how to go about modifying the one's I have seen to make them do what I want.

SdReader card; // This object holds the information for the cardFatVolume vol; // This holds the information for the partition on the cardFatReader root; // This holds the information for the filesystem on the cardFatReader f; // This holds the information for the file we're play

WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time

// Now we will look for a FAT partition! uint8_t part; for (part = 0; part < 5; part++) { // we have up to 5 slots to look in if (vol.init(card, part)) break; // we found one, lets bail } if (part == 5) { // if we ended up not finding one :( putstring_nl("No valid FAT partition!"); sdErrorCheck(); // Something went wrong, lets print out why while(1); // then 'halt' - do nothing! }

// Lets tell the user about what we found putstring("Using partition "); Serial.print(part, DEC); putstring(", type is FAT"); Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?

// Try to open the root directory if (!root.openRoot(vol)) { putstring_nl("Can't open root dir!"); // Something went wrong, while(1); // then 'halt' - do nothing! }

// Plays a full file from beginning to end with no pause.void playcomplete(char *name) { // call our helper to find and play this name playfile(name); while (wave.isplaying) { // do nothing while its playing } // now its done playing}

void playfile(char *name) { // see if the wave object is currently doing something if (wave.isplaying) {// already playing something, so stop it! wave.stop(); // stop it } // look in the root directory and open the file if (!f.open(root, name)) { putstring("Couldn't open file "); Serial.print(name); return; } // OK read the file and turn it into a wave object if (!wave.create(f)) { putstring_nl("Not a valid WAV"); return; }

How do you have the PIR wired? I was just setting one of those up.This one in facthttp://www.sparkfun.com/products/8630and I found I had use an external pullup to +5V to get it to work well.It has an open collector output, the output stays high unil it senses motion, then it pulls low. I used the low to create an interrupt on pin2 to wake up from sleep mode and do an action, then go back into sleep mode to save battery life (3 AAs).

You could poll the PIR output line every 1000mS or something, when you see it go low then play your song.At top of void loop()while (digitalRead(PIR_output) ==1){delay (1000)// at some point it will = 0 and then continue to the rest of the your code}If running on batteries, have it go to sleep and then wake up on interrupt into pin 2 or 3 (external interrupt lines:

Interrupt Service Routine function://***************************************************// * Name: pin2Interrupt, "ISR" to run when interrupted in Sleep Modevoid pin2Interrupt(){ /* This brings us back from sleep. */}

SdReader card; // This object holds the information for the cardFatVolume vol; // This holds the information for the partition on the cardFatReader root; // This holds the information for the filesystem on the cardFatReader f; // This holds the information for the file we're play

WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time

// Now we will look for a FAT partition! uint8_t part; for (part = 0; part < 5; part++) { // we have up to 5 slots to look in if (vol.init(card, part)) break; // we found one, lets bail } if (part == 5) { // if we ended up not finding one :( putstring_nl("No valid FAT partition!"); sdErrorCheck(); // Something went wrong, lets print out why while(1); // then 'halt' - do nothing! }

// Lets tell the user about what we found putstring("Using partition "); Serial.print(part, DEC); putstring(", type is FAT"); Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?

// Try to open the root directory if (!root.openRoot(vol)) { putstring_nl("Can't open root dir!"); // Something went wrong, while(1); // then 'halt' - do nothing! }

// Plays a full file from beginning to end with no pause.void playcomplete(char *name) { // call our helper to find and play this name playfile(name); while (wave.isplaying) { // do nothing while its playing } // now its done playing}

void playfile(char *name) { // see if the wave object is currently doing something if (wave.isplaying) {// already playing something, so stop it! wave.stop(); // stop it } // look in the root directory and open the file if (!f.open(root, name)) { putstring("Couldn't open file "); Serial.print(name); return; } // OK read the file and turn it into a wave object if (!wave.create(f)) { putstring_nl("Not a valid WAV"); return; }

Steppenwolf

Steppenwolf

It might be my sd card. It's being totally inconsistent. Sometimes I get the card.init failure, sometimes it says ready, but then when I wave in front of the PIR, it says can't open BanjoSong.wav endlessly (which is at least encouraging, because it means the PIR is working).

Steppenwolf

It was loaded on my mac, and it perfectly readable there. I used a program called Blue Harvest to erase the "dust" (the invisible files that macs put on things like usb drives and sd cards), because they were giving the wave shield a hard time. But it is fat16 formatted and what not.

Ok, then I guess the next step is looking at your code compared to this working come from this thread and see whats different that this works and yours doesn'thttp://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1272995447/0#0

I don't have time right now, maybe later tonight, after 8pm east coast time.