Well, I can't really put any timing delays because this circuit is going to be hooked up to a machine and can't miss any pulses. Here's how it works:

A machine sends about 4 pulses per second (noisy signal) and my pic is supposed to count their high states and average the last 10. If the average falls out of range, then my pic sends a pulse to PLC which will stop the machine. Normally the pulses are high for about 20-40 ms.

I've done the pulse counting and averaging but since the pulses are so noisy my pic pics up the garbage. So I need to clean the signal comming out of the machime.