Edgetriggered pulse counter

The best way to detect the edge of a pulse is with an interrupt. Look up attachInterrupt() in the Reference section.

...R