Guys,
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?
Thanks
#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);
}
}