Here's a simple sketch. Okay, could be a little simpler, but it works from USB power, and works from 3 AAA batteries also.
Here's a video clip - kinda big (28M), haven't figured out video editing yet
http://www.crossroadsfencing.com/MVI_1899.AVI
Connect a couple of resistors/LEDs to the noted pins in the blue & red arrays in void setup so you can that it's working.
After power up, you have 3 seconds to get out of scan range, then the PIR scans the room to set a baseline, and scans for 10 seconds, if no activity the arduino goes to sleep until interrupted by the PIR and then flashes the lights a few seconds, goes back to sleep.
You need a 10K pullup on Pin 2, and the PIR open collector connects there also.
#include <avr/sleep.h> // powerdown library
#include <avr/interrupt.h> // interrupts library
#define NUM_OFF 3
#define DELAY 50
#define PWM_MIN 10
#define PWM_MAX 128
int blue[5];
int red[5];
int interrupt = 2; // hardware interrupt, Motion Detector Open Collector pulls low
unsigned long awake_time = 0; // capture time when wake up from sleep
unsigned long elapsed_time = 0; // time that have been awake for
//***************************************************
// * Name: pin2Interrupt, "ISR" to run when interrupted in Sleep Mode
void pin2Interrupt()
{
/* This brings us back from sleep. */
}
//***************************************************
// * 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
/* First thing to do is disable sleep. */
sleep_disable();
// then go to the void Loop()
}
// ***********************************************************************
// set up the pins as Inputs, Outputs, etc.
void setup()
{
blue[0] = 4;
blue[1] = 5; // PWM
blue[2] = 7;
blue[3] = 6; // PWM
blue[4] = 8;
red[0] = 12;
red[1] = 10; // PWM
red[2] = 14;
red[3] = 11; // PWM
red[4] = 15;
for(int i=0;i<5;i++) {
pinMode(blue[i], OUTPUT);
pinMode(red[i], OUTPUT);
}
pinMode (interrupt, INPUT); // hardware interrupt for waking up
digitalWrite (interrupt, HIGH); // internal pullup enabled
randomSeed(analogRead(0));
awake_time = millis(); // capture wakeup time
//Serial.begin (19200); // for debug
delay (3000); // turn on, scan room
}
// ****************************************
// setup is done, start program
void loop()
{
// flashl lights in police sequence
allOn();
analogWrite(blue[1], random(PWM_MIN, PWM_MAX));
analogWrite(blue[3], random(PWM_MIN, PWM_MAX));
analogWrite(red[1], random(PWM_MIN, PWM_MAX));
analogWrite(red[3], random(PWM_MIN, PWM_MAX));
for(int i=0;i<NUM_OFF;i++) {
digitalWrite(blue[random(5)], LOW);
digitalWrite(red[random(5)], LOW);
}
delay(DELAY);
// check if awake 10 seconds, then call sleep mode
elapsed_time = millis() - awake_time; // see if 10 seconds gone by
//Serial.println (elapsed_time);
if (elapsed_time >=10000){
allOff(); // turn all light off
delay (3000); // time to walk away
enterSleep(); // call Sleep function to put us out
// THE PROGRAM CONTINUES FROM HERE after waking up in enterSleep()
awake_time = millis(); // capture the time we woke up
}
} // end void loop
// ****************************************
// function to turn all lights on
void allOn() {
for(int i=0;i<5;i++) {
digitalWrite(blue[i], HIGH);
digitalWrite(red[i], HIGH);
}
}
// ****************************************
// function to turn all lights off
void allOff() {
for(int i=0;i<5;i++) {
digitalWrite(blue[i], LOW);
digitalWrite(red[i], LOW);
}
}