Timer problem

Beneath is a small stripped hypothetical program to illustrate a problem I have with timers.

Two buttons. One of them starts a timer which counts down time in seconds, the other increase start time (+1 for each push).

The problem comes when I start count down right after I have pushed the setTimeButton.Then the intervals becomes: 65 4 3 2 1 0. In other words the count down to 5 which should come after 1s comes immediately. The next times I press countDownButton the intervals always becomes the right: 6 5 4 3 2 1 0.

When I give cnt an initial value other than 0 then the same problem occurs even without first pressing the setTimeButton.

#define setTimeButton 4
#define countDownButton 5

boolean countDownState = false;
volatile boolean newValue = false;   
int startValue = 5;
volatile int cnt;

void setup() 
{
  pinMode(setTimeButton,INPUT_PULLUP);
  pinMode(countDownButton,INPUT_PULLUP);
  initTimer();
  Serial.begin(9600);
  Serial.print(startValue);
}

void loop() 
{
   if((digitalRead(countDownButton) == LOW) && (countDownState == false))
   {
      startCountDown();   
   }
   if(digitalRead(setTimeButton) == LOW)
   {
      increaseStartTime();
   }
   if(cnt == 0)
   {
      stopCountDown();
   }
   if(newValue)
   {
      newValue = false;
      Serial.print(cnt);
   }
}

void startCountDown()
{
   countDownState = true;
   Serial.print(startValue);
   cnt = startValue;
   TCNT1 = 0;
   TIMSK1 |= (1<<OCIE1A);   // Enable timer compare match interrupt  
}

void stopCountDown()
{
   TCNT1 = 0;
   countDownState = false;
   TIMSK1 &= ~(1<<OCIE1A);     // Disable timer compare match interrupt 
}

void increaseStartTime()
{
   delay(50);
   if(digitalRead(setTimeButton) == LOW)
   {
      startValue++;
      cnt = startValue;
      newValue = true;
   }
}

void initTimer()
{
   TCCR1A = 0;
   TCCR1B = 0;
   TCNT1 = 0;                  // Initial timer value
   OCR1A = 15625;              // Number of ticks to reach 1s with prescaler 1024   
   TCCR1B |= (1<<WGM12)|(1<<CS10)|(1<<CS12);   // prescaler 1024 and CTC mode
}

ISR(TIMER1_COMPA_vect)        
{
   cnt--;
   newValue = true;
}

Any help are very much appreciated!

If the first interrupt is triggered too early, then maybe have a look at clearing bit

OCF1A in register TIFR1 before enabling interrupts by manipulating OCIE1A

Thank you, just what was needed.

Your variable 'volatile int cnt' occupies 2 bytes, therefore access to it won't be atomic on an 8-bit machine. Any place in non-ISR code that you read or write it should be surrounded by a noInterrupts() / interrupts() pair.