How to count the voltage pulses of a digital temperature sensor using Arduino Mega Atmega2560?
There are function called pulseIn which probably need exactly what you want.

Just set a variable with the current 'time' (millis, start counting until the current time reaches 50 ms later. 

So something like (not checked):

unsigned long timer = millis();
unsigned pulses = 0;
while (timer + 50 &lt; millis())
{
  pulseIn(PIN, LOW);
  pulses++;
}


PIN is a constant or variable that defines the pin number to check, LOW is to check low pulses, you also can use high, and optionally you can add a timeout. For example, you can use the timeout to check the last timeout is within the 50 ms region. Or to be more precise, check if the pulse was within the same 50 ms:

unsigned long timer = millis();
unsigned pulses = 0;
while (timer + 50 &lt; millis())
{
  pulseIn(PIN, LOW);
  if (timer + 50 &lt; millis())
  {
    pulses++;
  }
}