signal detection problem

Hello. I'm using Arduino uno to detect a square wave pulse (externally generated), and if the time extends 0.9 seconds and no (external) pulse is received then Arduino should generate a pulse, and it should stop generating pulses as soon as external pulse is received again. I'm posting the code as well. The problem with this program is that once it is uploaded it detects and works properly but after sometime it does not detect the same pulse usually when external pulse gets off for 2nd or 3rd time. It works perfectly for the first time, and in rest of the time, it sometimes detect the pulse after 3-4minutes. I am unable to figure out the problem. Help will be highly appreciated.

int a = 7;
int b=13;
float duration;
unsigned long x;

void setup()
{ Serial.begin(9600);
 pinMode(a, INPUT);
pinMode(b,OUTPUT);}

void loop()
{
  x = pulseIn(a, LOW, 900000); //checks the off-time of the pulses for 0.9 seconds (microseconds conversion), pulsein takes the time in microseconds
  duration=x/1000000.0; //converting to seconds
  Serial.println(duration); // displaying the time period for each pulse in seconds
if(duration==0) //generate a pulse when no pulse is detected, when no pulse is detected pulsein returns 0.
//pulse generation code
 {digitalWrite(b,HIGH);
 delay(500);  
 digitalWrite(b,LOW);
delay (500); 
}

}

Maybe get rid of the delay()s?

“Maybe get rid of the delay()s?” I didn’t get it, do u mean by removing delays? If yes, then delays are necessary to blink an led.

No, the delays are not necessary, and are preventing you from detecting anything whilst they are running.

sadia_jangda: "Maybe get rid of the delay()s?" I didn't get it, do u mean by removing delays? If yes, then delays are necessary to blink an led.

No they're not. You can "Blink Without Delay" (search that or just see the example by that name that comes with the IDE). But the delays do stop your Arduino from looking for the pulse for a whole second at a time.

EDIT From a post that has now been removed:-

but can u help me that how can I blink an led without using delay?

he said:-

You can "Blink Without Delay" (search that or just see the example by that name that comes with the IDE).

Did you miss that?

If you did then see my http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html Or Robin2's several things at once http://forum.arduino.cc/index.php?topic=223286.0

@Grumpy_Mike, yes, I missed that. Thank you @AWOL and @Delta_G for the help :)