Routine only works with small delay

Hi,

First project for a long time. This little code cut from a much larger file demonstrates unexpected behaviour. This code will toggle an LED a number of times, but only works if there is at least a 1 mS delay in the ledShow subroutine. A clock runs in a timer interrupt routine for timing the led transitions and the wake switch sets the permissive in wakeInt() to enable ledShow. If I remove the 1mS delay the code does not appear to flash the LED. May be a race condition, but of what? It’s being used on an Arduino Uno.

// Shows up need for 1mS delay to make loop work
#include <SoftwareSerial.h>
#include “C:\Temp\LowPower.h”

// Define the module pinouts
#define LED 12 // LED line
#define WAKESW 2 // Wake sw

enum LEDState {LED_HIGH, LED_LOW};
LEDState ledSt = LED_HIGH;

bool ledInhibit = 1;
int ledNum; // number of cycle
int ledCnt; // counter val of the LED counter

void setup() {
pinMode(WAKESW, INPUT_PULLUP);
pinMode(LED, OUTPUT); // Setup LED
digitalWrite(LED, HIGH);
attachInterrupt(digitalPinToInterrupt(WAKESW), wakeInt, LOW);
SetUpDoorTimer();
ledInhibit = 0;
}

void loop() {
ledShow(4);
}

void wakeInt() {
ledInhibit = 0;
}

void ledShow(int cycles) {
while (ledInhibit == 0) {
delay(1); // this 1mS delay was necessary to make the code work. Not clear as to why.
if (ledCnt >= 1) { // removes runt pulses
if ((ledCnt < cycles) && (ledInhibit == 0)) {
switch (bitRead(ledCnt, 0)) {
case 0:
digitalWrite(LED, HIGH);
ledSt = LED_HIGH;
break;
case 1:
digitalWrite(LED, LOW);
ledSt = LED_LOW;
break;
}
} else {
ledInhibit = 1;
ledCnt = 0;
digitalWrite(LED, HIGH);
}
}
}
}

ISR(TIMER1_COMPA_vect) {
// cli();
if (ledInhibit == 0) {
ledCnt++;
}
// sei();
}

void SetUpDoorTimer() {
cli();//stop interrupts
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
OCR1A = 7812;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS12) | (1 << CS10);
TIMSK1 |= (1 << OCIE1A);
sei();//allow interrupts
}

Please follow the advice in Read this before posting a programming question

Without the delay(1), how long is the LED pin in any given state?

int ledCnt; // counter val of the LED counterneeds to be declared 'volatile'

Thanks all, Deva_Rishi has solved the problem.