I notice when it times out and goes to sleep (set to 10 seconds for testing purposes), it wakes back up, then goes to sleep again for good (until I trip the interrupt pin). My interrupt is set as CHANGE, not LOW. I wonder if this is why it does this, or if I have a little problem in my code. Here's the code, this is what puts it to sleep in the main loop:
long idleTime = 10000; //1800000; //if we've been idle this long, we'll go to sleep
if (millis() - wakeTime > idleTime) { //has it been 30 minutes since the ants were fed?
digitalWrite(receiverPowerPin, HIGH); //this turns OFF receiver
sleep_now(); //if the mower's still in use, it'll wake right back up from the interrupt on the mercury switch
//WE WAKE UP HERE, SO RESET/RESTART THINGS:
Serial.println("We're awake again!");
wakeTime = millis();
digitalWrite(receiverPowerPin, LOW); //this turns ON receiver
}
}
And this is the sleep_now() routine:
void wake ()
{
// cancel sleep as a precaution
sleep_disable();
// must do this as the pin will probably stay low for a while
detachInterrupt (0);
} // end of wakevoid sleep_now() {
// disable ADC
ADCSRA = 0;
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable();
// Do not interrupt before we go to sleep, or the
// ISR will detach interrupts and we won't wake.
Serial.println("Going to sleep");
Serial.flush();
delay(50);
noInterrupts ();
if (!systemError) { //systemError means there's a big problem, so don't bother waking back up
attachInterrupt (0, wake, CHANGE); // will be called when pin D2 goes low
}
// turn off brown-out enable in software
// BODS must be set to one and BODSE must be set to zero within four clock cycles
MCUCR = bit (BODS) | bit (BODSE);
// The BODS bit is automatically cleared after three clock cycles
MCUCR = bit (BODS);
// We are guaranteed that the sleep_cpu call will be done
// as the processor executes the next instruction after
// interrupts are turned on.
interrupts (); // one cycle
sleep_cpu (); // one cycle
delay(1000);
sleep_disable(); //wake up here}