need help with interrupt from PIR Sensor

I have this code below, is supposed to flash some red & blue lights for 10 seconds, pause 3 seconds then go into sleep mode.
Then stay in sleep mode until interrupted by a sparkfun PIR motion sensor. Am running on 3 AA's with a 5V Promini.
After I turn on power, while standing behind the sensor, it pauses 3 seconds to let the sensor map the room, then flashes 10 seconds, then appears to go into sleep mode. I wait 2, 3 minutes, no lights.
I then wave my hand - the lights flash 10 seconds, turn off for 3. Then on 10, off 3. Never seems to go into sleep mode again. Just on 10, off 3, over & over.
I am seeing interrupts on a scope that seem to coincide with end of the 3 second delay.
How do I get this thing to behave?


#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_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. */

  // 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


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
  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);

// 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);

A quote from the SparkFun site that may be relevant...

This says you can use it down to 5V but it does not appear to be stable to me at this voltage. The output is bouncing all over the place. When I change the input to 12V it works perfectly.

I checked the output of the sensor regulator, it seemed solid at 3V with 4.5 going in.

Try this...

// * Name: pin2Interrupt, "ISR" to run when interrupted in Sleep Mode
void pin2Interrupt()
/* This brings us back from sleep. */
detachInterrupt(0); //disable interrupts while we wake up

// * Name: enterSleep
void enterSleep()
/* The program will continue from here after the interrupt. */
// detachInterrupt(0); //disable interrupts while we wake up

Coding, that seems to work!
I don't understand why tho. Can you explain please?

Unfortunately, I cannot explain why it eliminates the problem you were having. I expected the symptom to be that the Arduino would "lock up" or become unresponsive. The behaviour you're seeing seems to be more like resetting.

Anyway. The problem is that the external level interrupt is "continuous fire". So long as the pin remains low, the interrupt fires. It won't fire in the interrupt service because interrupts are disabled. But, assuming the pin is still low, as soon as the interrupt service routine returns, the interrupt fires again.

Hmm, I was watching the interrupt line on a scope, and it seemed like going into sleep mode is what setoff the interrupt going line low. It would be high during the flashing, and the 3 seconds, then go low as the flashing started again. I suppose I could toggle a bit somewhere & try & catch the timimg of that.

It just occurred to me, I have the interrupt pin tied to A2 also as I was playing with reading the signal in via analog level (was having trouble with interrupts earlier, turned out to be coding logic problem). I was just thinking if the analog pin was getting pulled low when going into sleep & causing what I saw - but that can't be happening as it is able to create an interrupt now.

Not going to fret over it as it seems to be working now. Thanks again.