# How to make the built-in LED blink with a frequency of 5 Hz using interrupts

The task is: after five clicks on OK, the built-in LED starts flashing with a frequency of 5Hz using an interrupt. After the eighth click on OK, the flashing (and glowing) should stop.Why is flashing not working

``````bool ledState = false;
volatile bool ledOn = true;

void setup(){
TCCR2A = 0;
TCCR2B = 0;
TIMSK2 = 0;
TCNT2 = 0;
OCR2A = 77;
TCCR2B |= (1 << CS22) | (1 << CS21) | (1 << CS20);
TIMSK2 |= (1 << OCIE2A);
pinMode(13, OUTPUT);
}

void loop(){
if(button5.buttonPressedCount >= 5 && button5.buttonPressedCount < 8){
ledState = true;
}

if(button5.buttonPressedCount > 7){
ledState = false;
}
}

ISR(TIMER2_COMPA_vect){
if(ledState){
digitalWrite(13, ledOn);
ledOn = !ledOn;
}
}
``````
• Why `OCR2A = 77;`? May 11 at 12:49
• The value for obtaining a frequency of 5 Hz with a divider of 1024 May 11 at 12:50
• Could you please explain, within the question, how you go from “f = 5 Hz” to `OCR2A = 77;`? May 11 at 12:51
• timer will run at a frequency of 16 MHz / 1024 = 15.625 kHz, so desired interrupt frequency = (77 + 1) * 15.625 kHz / 1024 = 5 Hz May 11 at 12:54
• This calculation is wrong. May 11 at 12:57

• then it should look like this? `TCCR1A = 0; TCCR1B = 0; TIMSK1 = 0; TCNT1 = 0; OCR1A = 6249; // Set compare match value for prescaler 256 TCCR1B |= (1 << CS12); // Set prescaler to 256 TIMSK1 |= (1 << OCIE1A); // Enable interrupt on compare match A` May 11 at 13:51
• like this: `TCCR1A = (1 << WGM11) | (1 << COM1A0); TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 << CS12); TIMSK1 = (1 << OCIE1A); OCR1A = 6250;` May 11 at 13:58