int ledPin = 5;
volatile bool LED_STATE = 0; // setting initial led state to low
volatile int count = 0;
void setup(){
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LED_STATE);
cli(); // Stops interuppts until all settings are made
TCCR1A = 0; //Timer/Counter 1 control register A (PWM) - SET TO 0
TCCR1B = 0; //Timer/Counter 1 control register B (PRESCALER) - SET TO O
TCCR1B |= B00000101; //set prescaler to 1024
TCNT1 = 0; // reset timer to 0
TIMSK1 |= B00000010; //enables OCIE1A to enable compare match A
OCR1A = 39063; //Sets compare register A to this value - (2.5 sec)
sei(); // Enable back the interuppts
}
void loop (){
}
ISR(TIMER1_COMPA_vect){
TCNT1 = 0; // resets timer to 0
count += 1; // after every 2.5 sec the count is inreased
Serial.print(count);
if (count == 2){ // when the count is 2 that is after 5sec the LED is toggled
LED_STATE =! LED_STATE;
digitalWrite(ledPin,LED_STATE); //Toggles the pin
count = 0;
}
}