pulseIn()

Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds or 0 if no complete pulse was received within the timeout. The timing of this function has been determined empirically and will probably show errors on shorter pulses. Works on pulses from 10 microseconds to 3 minutes in length. Please also note that if the pin is already high when the function is called, it will wait for the pin to go LOW and then HIGH before it starts counting. pulseIn() works only with fast pins defined via s_pin data type and hence can work only on pins 9–16. width = pulseIn(pin, level, timeout); pin the number of the pin on which you want to read the pulse (s_pin type) level type of pulse to read: HIGH or LOW timeout the number of microseconds to wait for the pulse to be completed: the function returns 0 if no complete pulse was received within the timeout (DWORD, must be > 500 μs) the length of the pulse (in microseconds) or 0 if no pulse is completed before the timeout (DWORD)
s_pin pinPulse = 9;

void setup() {
  Serial.begin(115200);
  pinMode(pinPulse, INPUT); 
}

void loop() {
  DWORD duration;

  duration = pulseIn(pinPulse, HIGH, 1000000);
  Serial.println(duration);
  delay(1000);
}