Hi,
I'm building a temperature logger.
I have a question regrading to deep sleep mode and interrupt pin.
Sorry for my bad explain but it is a little bit confused.
I have 2 situation.
- Using Watch Dog Timer to wake up every 2 minutes. (working)
code layout
void loop (){
screen display(){
- Logging
- Change interval
if(logging){
check sensor;
print to screen;
sleep(2min);
}
if(changeinterval){
update interval (1,2,4 min);
}
}
- Using external interrupt Pin (Not working)
So I have a interrupt wake up inside a if(!check) loop. However, whenever I try to use PIN 11 to wake up from deep sleep mode when sleepNow() executed. I see the LED PIN 19 is blink but after that LED PIN 15 blink after that. It means the interrupt wake up not in the loop if(!check) but somehow it restart the program again. Thanks
#include <avr/sleep.h>
int check = 0;
// interrupt service routine in sleep mode
void wake ()
{
sleep_disable (); // first thing after waking from sleep:
detachInterrupt (digitalPinToInterrupt (11)); // stop LOW interrupt on D11
} // end of wake
void setup(){
Serial.begin(9600);
pinMode(19, OUTPUT);
pinMode(15, OUTPUT);
pinMode(14, INPUT);
digitalWrite(14, HIGH);
pinMode(11, INPUT);
digitalWrite(11, HIGH);
}
void loop(){
test();
check = digitalRead(14);
if (!check){
sleepNow();
}
else {}
}
void test(){
digitalWrite(15, HIGH);
delay(1000);
digitalWrite(15,LOW);
delay(1000);
}
void sleepNow(){
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
noInterrupts (); // make sure we don't get interrupted before we sleep
sleep_enable (); // enables the sleep bit in the mcucr register
attachInterrupt (digitalPinToInterrupt (11), wake, LOW); // wake up on low level on D11
interrupts (); // interrupts allowed now, next instruction WILL be executed
sleep_cpu (); // here the device is put to sleep
digitalWrite(19, HIGH);
delay(1000);
digitalWrite(19,LOW);
delay(1000);
}
Thank you very much.