boolean toggle = 0; // boolean for the 5 second process
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT); // pin with led connected
cli();//stop interrupts
TCCR0A = 0;// set entire TCCR2A register to 0
TCCR0B = 0;// same for TCCR2B
TCNT0 = 0;//initialize counter value to 0
// set compare match register for 2khz increments
OCR0A = 255;// = (16*10^6) / (2000*64) - 1 (must be <256)
// turn on CTC mode
TCCR0A |= (1 << WGM01);
// Set CS01 and CS00 bits for 64 prescaler
TCCR0B |= (1 << CS01) | (1 << CS00);
// enable timer compare interrupt
TIMSK0 |= (1 << OCIE0A);
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 = 39000;// = (16*10^6) / (1*1024) - 1 (must be <65536)
// above set to value for approx 2.5 sec interval
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();//allow interrupts
}
ISR(TIMER0_COMPA_vect){// isr for light on
digitalWrite(13,LOW); // turns led off after time
}
ISR(TIMER1_COMPA_vect){// isr for light off
//(takes two cycles for full wave- toggle high then toggle low)
if (toggle == 0){
toggle = 1; //first 2.5 sec to pass does nothing
} else if (toggle == 1){ // on second 2.5 second to pass (ie total 5)
digitalWrite(13, HIGH); // led set to high
toggle = 0; // reset toggle
}
}
void loop() {
// put your main code here, to run repeatedly:
}