Square pulse wave with a delay and a certain length

The input signal comes from an external device. I am not sure whether there will be more than 3 input pulses (I will check). This could indeed explain why I keep generating pulses.
For now I included an additional delay() equal to the maximum time interval that the external device is sending input pulses at the end of the if (count ==3) statement. Then after that delay I've set the count back to count=0;

The input pulse is a 10µs TTL pulse. the time in between them is not constant, but in the order of a couple of 100ms - seconds.