#include <avr/sleep.h>
#include <avr/interrupt.h>
const int mosfet = 0;
const int holdPin = 5;
unsigned long maxidletime = 60000UL;
boolean blocksleep = false;
unsigned long lastPinCheck = millis();
void setup() {
pinMode(1, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(holdPin, INPUT);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(holdPin, HIGH);
pinMode(mosfet, OUTPUT);
digitalWrite(mosfet, LOW);
} // setup
void sleep() {
GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts
PCMSK |= _BV(PCINT1);
PCMSK |= _BV(PCINT2);
PCMSK |= _BV(PCINT3);
PCMSK |= _BV(PCINT4);
PCMSK |= _BV(PCINT5);
//PCMSK |= _BV(PCINT5);// Use PB3 as interrupt pin
ADCSRA &= ~_BV(ADEN); // ADC off
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // replaces above statement
sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
sei(); // Enable interrupts
sleep_cpu(); // sleep
cli(); // Disable interrupts
sleep_disable(); // Clear SE bit
// ADCSRA |= _BV(ADEN); // ADC on
sei(); // Enable interrupts
} // sleep
ISR(PCINT0_vect) {
lastPinCheck = millis(); // This is called when the interrupt occurs, but I don't need to do anything in it
}
void loop() {
if (digitalRead(holdPin) == LOW)
lastPinCheck = millis();
if (millis() - lastPinCheck >= maxidletime) {
blocksleep = false;
digitalWrite(mosfet, LOW);
}
//digitalWrite(mosfet, LOW);
if (!blocksleep)
{
sleep();
}
blocksleep = true;
digitalWrite(mosfet, HIGH);
}