Hello. I'm creating a speedometer, using an Arduino Pro Mini and some reed switches. For maximum power efficiency, I tried putting the Arduino to sleep and waiting on an interrupt from the reed switch, connected to the digital pin #2. Yet the Arduino is being stubborn and it simply refuses to wake up when the interrupt occurs... I am posting the code below:
//PINS
const int switchPin = 2; // the number of the reed switch pin, reed generates interrupt
<...>
unsigned long currMillis, prevMillis, T1;
<...>
//sleep function
void sleepNow() // here we put the arduino to sleep
{
attachInterrupt(0, turnOnDisplay, RISING);
delay(10);
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register
// so sleep is possible. just a safety pin
sleep_mode(); // here the device is actually put to sleep!!
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
sleep_disable(); // first thing after waking from sleep:
// disable sleep...
detachInterrupt(0); // disables interrupt 0 on pin 2 so the
// wakeUpNow code will not be executed
// during normal running time.
}
<...>
void setup() {
init();
Serial.begin(9600);
// while the serial stream is not open, do nothing:
while (!Serial) ;
Serial.println ("Setup start");
//initialize input pins, reed
pinMode(switchPin, INPUT);
<...>
}
void loop() {
//how long has it passed
currMillis = millis();
//sleep if current time - previous time exceeds timeout
if ((currMillis - prevMillis) >= T1) {
<...>
sleepNow(); //sleep and wait for interrupt from the sensor
currMillis = 0; //reset timekeeping variables
prevMillis = 0;
}
//awake
else {/* do something*/}
<...>
}
Irellevant code is omitted for easier reading (irellevant code is declarations and printing stuff on the LCD). The rest of the program is doing exactly what it should. But when the Arduino sleeps, it never wakes up again, no matter how many times the interrupt arrives... Any suggestions on what might be going on?