pulseIn()
pulseIn(pin, value)
pulseIn(pin, value, timeout)
Parameters
pin: the number of the pin on which you want to read the pulse. (int)
value: type of pulse to read: either HIGH or LOW. (int)
timeout (optional): 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. Default is one second (unsigned long).
Returns
the length of the pulse (in microseconds) or 0 if no pulse is completed before the timeout (unsigned long)
Example
int pin = 7;
unsigned long duration;
void setup()
{
pinMode(pin, INPUT);
}
void loop()
{
duration = pulseIn(pin, HIGH);
}
int pin = 7;
unsigned long lowduration;
unsigned long highduration;
unsigned long timeperiod;
void setup()
{
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
}
void loop()
{
lowduration = pulseIn(pin, LOW);
highduration = pulseIn(pin, HIGH);
timeperiod = lowduration + highduration;
}