Can any one tell me why this is not sleeping for 1 second, I tried with both idle and power down modes.
I know its to do with enabling / disabling interrupts. If i use idle how can i make sure only timer 1 interrupts, or if use power down how do i enable the interrupt from the timer
Cheers
#include <avr/sleep.h>
volatile byte toggle1 = 0;
void setup() {
Serial.begin(9600);
//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
//allow interrupts during sleep
sei();//allow interrupts
}
void loop() {
//each loop cont down by one second
//countdown = countdown - 1;
Going_To_Sleep();
}
void Going_To_Sleep(){
sleep_enable();//Enabling sleep mode
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_cpu();//activating sleep mode
}
ISR(TIMER1_COMPA_vect){//timer1 interrupt 1Hz toggles pin 13 (LED)
sleep_disable();
//generates pulse wave of frequency 1Hz/2 = 0.5kHz (takes two cycles for full wave- toggle high then toggle low)
// if (toggle1){
// digitalWrite(13,HIGH);
// toggle1 = 0;
// }
// else{
// digitalWrite(13,LOW);
// toggle1 = 1;
// }
}