Hi,
i have got some problems with my arduino that is equipped with an atmega
168.
I want to put the atmega into sleep mode for x milliseconds. The wakeup
is realized with a Timer2 interrupt. If I use in the loop function the
"delay(1)" just for testing purposes the led that is connected to pin 13
blinks in 5 sec interval. (as it should be)
But if I remove the delay and write the "sleep_now()" instead, the led
flashes every 10 seconds. Does anybody have an explanation for this
weird behavior?
Cheers,
Jochen
Here is the code:
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/sleep.h>
int volatile time=5000; //sleep time
boolean volatile timerEnabled=false;
int volatile counter = 0;
void sleepNow() /
set_sleep_mode(SLEEP_MODE_PWR_SAVE); // 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...
}
void setup() {
// Serial.begin(19200);
pinMode(13,OUTPUT);
TCCR2A |= (1 << WGM21); //ctc mode
OCR2A = 124; //set timer maximum to 124
TCNT2 = 0;//reset timer
TCCR2B = 0b00000100; //((1 << CS22)|(1 << CS20)); //prescaler 64
sei();
}
ISR(TIMER2_COMPA_vect) {
counter ++;
if (counter == time) {
TCCR2A =0; // stop timer compA interrupts
counter=0;
timerEnabled=false;
}
}
/////////////////////////////////////////////////////////////////
void loop() {
if(timerEnabled==true)
sleepNow();
//delay(1100);
else {
//Serial.println("TEST");
timerEnabled=true;
digitalWrite(13,HIGH);
delay(10);
digitalWrite(13,LOW);
//time=1000;
TCNT2 = 0; //reset timer
TIMSK2 |= (1 << OCIE2A); //enable timer interrupt
}
}