Timer2 settings and use?

Hey guys I’m having trouble with the timer 2 interrupt. This is my code can anyone see anything wrong with it?

const byte LED = 13;
int count = 0;

ISR(TIMER2_COMPA_vect){
  static boolean state = false;
  if(count < 9){
    count++;
  }
  else{
  state = !state;  // toggle
  digitalWrite (LED, state ? HIGH : LOW);
  count = 0;
  }
}

void setup() {
  // put your setup code here, to run once:
  pinMode (LED, OUTPUT);

  TCCR2A = 0; //turn on CTC mode
  TCCR2B = 0;
  TCNT2 = 0;
  OCR2A = 249; // = (16*10^6)/(1000*64)-1 (must be <256)
  TCCR2A = bit(WGM21);
  TCCR2B = bit(CS22);
  TIMSK2 = bit(OCIE2A);
}

void loop() {
  // put your main code here, to run repeatedly:

}

I’ve read through other posts about the timer 2 interrupt which seems to be a common problem and I haven’t found a fix for it.

Hey guys I'm having trouble with the timer 2 interrupt.

But, I'm not going to tell you what the problem is.

This is my code can anyone see anything wrong with it?

No, it looks exactly like your code.

I've read through other posts about the timer 2 interrupt which seems to be a common problem and I haven't found a fix for it.

So, some other people have had a variety of problems, and none of the fixes for them worked.

Did I miss something?

Hey guys I'm having trouble with the timer 2 interrupt.

Perhaps if you stated what the "trouble" is.

int count = 0;

I'd be making that volatile for a start.

I'm sorry PaulS and everyone. I missed the obvious. The LED isn't blinking.

I've run the code with timer 1 where I can get it to flash but with timer2 it's off.

I'm also running it on an arduino UNO rev 3.

I'd be making that volatile for a start.

It's only used in the ISR, so volatile is not needed.

Hey guys,

I solved it.

This is my working code and thanks to Gammon for helping me realise that my LED was just blinking too fast for me to see.

const byte LED = 13;
volatile int count = 0;

ISR(TIMER2_COMPA_vect){
  static boolean state = false;
  if(count < 64){
    count++;
  }
  else{
  state = !state;  // toggle
  digitalWrite (LED, state ? HIGH : LOW);
  count = 0;
  }
}

void setup() {
  // put your setup code here, to run once:
  pinMode (LED, OUTPUT);

  TCCR2A = 0; //turn on CTC mode
  TCCR2B = 0;
  TCNT2 = 0;
  OCR2A = 249; // = (16*10^6)/(1000*64)-1 (must be <256)
  TCCR2A = bit(WGM21);
  TCCR2B = bit(CS22);
  TIMSK2 = bit(OCIE2A);
}

void loop() {
  // put your main code here, to run repeatedly:

}

PaulS:
It's only used in the ISR, so volatile is not needed.

If it is static, and only used in the ISR, it should be declared in the ISR. I presumed that, being declared globally, it would eventually be used globally.

Good point(s).