Hello all!
I get trouble combining two examples of sleep/wake. I need let Atmega wakes every 4 seconds when sleeping and blink a LED, and go sleep again; and also make possible wake for some time (e.g. 45 sec). After that time it should go sleep again, and blink LED every 4 sec. In the waked mode LED should be on continuously.
Examples used as source were taken from
My sketch:
#define LEDPIN A0;
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
int seconds=0;
volatile boolean f_wdt=true,
bActive=true;
void pin2Interrupt(void){
detachInterrupt(0);
digitalWrite(LEDPIN,HIGH);
bActive=true;
}
ISR(WDT_vect){
if(!bActive && !f_wdt) f_wdt=true;
}
void enterSleep(void){
if(bActive){
attachInterrupt(0, pin2Interrupt, LOW);
digitalWrite(LEDPIN,LOW);
bActive=false;
}
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
sleep_disable(); /* First thing to do is disable sleep. */
power_all_enable(); // ??? - from WDT wakeup example, no such in Interrupt wakeup example
}
void setup(){
pinMode(LEDPIN,OUTPUT);
/* Clear the reset flag. */
MCUSR &= ~(1<<WDRF);
/* In order to change WDE or the prescaler, we need to
* set WDCE (This will allow updates for 4 clock cycles).*/
WDTCSR |= (1<<WDCE) | (1<<WDE);
/* set new watchdog timeout prescaler value */
WDTCSR = 1<<WDP3; /* 4.0 seconds */
/* Enable the WDT interrupt (note no reset). */
WDTCSR |= _BV(WDIE);
}
void loop(){
if(f_wdt && !bActive){
/* blink LED */
digitalWrite(LEDPIN,HIGH);
delay(50);
digitalWrite(LEDPIN,LOW);
f_wdt = false;
enterSleep();
}
if(bActive){
delay(1000);
seconds++;
if(seconds == 45){
seconds = 0;
enterSleep();
}
}
}
Now this wake MCU every 4 seconds and blinks LED, but won't wake up when I press button on D2 (Int0).