How do you have the PIR wired? I was just setting one of those up.
This one in fact
and 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:
at top of sketch:
#include <avr/sleep.h> // powerdown library
#include <avr/interrupt.h> // interrupts library
int interrupt = 2; // hardware interrupt, Motion Detector Open Collector pulls low
Interrupt Service Routine function:
//***************************************************
// * Name: pin2Interrupt, "ISR" to run when interrupted in Sleep Mode
void pin2Interrupt()
{
/* This brings us back from sleep. */
}
Sleep function:
//***************************************************
// * Name: enterSleep
void enterSleep()
{
/* Setup pin2 as an interrupt and attach handler. /
attachInterrupt(0, pin2Interrupt, LOW);
delay(50); // need this?
/ the sleep modes
SLEEP_MODE_IDLE - the least power savings
SLEEP_MODE_ADC
SLEEP_MODE_PWR_SAVE
SLEEP_MODE_STANDBY
SLEEP_MODE_PWR_DOWN - the most power savings
*/
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // setting up for sleep ...
sleep_enable(); // setting up for sleep ...
sleep_mode(); // now goes to Sleep and waits for the interrupt
/* The program will continue from here after the interrupt. */
// detachInterrupt(0); //disable interrupts while we wake up COMMENTED OUT, didn't work otherwise
/* First thing to do is disable sleep. */
sleep_disable();
// then go to the void Loop()
}
in setup:
pinMode (interrupt, INPUT); // hardware interrupt for waking up
digitalWrite (interrupt, HIGH); // internal pullup enabled
in loop:
// call this when song is done and you want to power down
enterSleep(); // call Sleep function to put us out
// THE PROGRAM CONTINUES FROM HERE after waking up in enterSleep()