pulseIn()
Description
Read the pulse width value for a designated pin
(designated by the pin parameter), during signal HIGH or LOW condition (designated by the state parameter).
Pulse width value is the length of time the pin’s voltage remain HIGH or LOW.
For example, when the state
parameter is set to HIGH
, the pulseIn()
function wait for the pin’s
voltage changes to HIGH and measure the length of time the pin’s voltage remain HIGH until it changes to LOW. The length of time is measured in millisecond (ms) where the maximum pulse width is 71 minutes. When nothing is detected within the specified time-out period, the function abandon the read attempt and return 0.
Note: This function is designed to work in the PWM capture mode. This function cannot be used in conjunction with the encoder library’s attachInterrupt(). After calling attachInterrupt()
and mount ISR, the pulseIn()
function does not work
Syntax
Enc0.pulseIn(pin, state)
Enc0.pulseIn(pin, state, timeout)
Enc1.pulseIn(pin, state)
Enc1.pulseIn(pin, state, timeout)
Enc2.pulseIn(pin, state)
Enc2.pulseIn(pin, state, timeout)
Enc3.pulseIn(pin, state)
Enc3.pulseIn(pin, state, timeout)
Parameters
Enc0、Enc1、Enc2、Enc3
:Corresponding to ENC0, ENC1, ENC2 and ENC3 encoder interfaces.
pin
:0, 1 or 2 (I/O pin to read from):Set to 0 to read from pin A. Set to 1 to read from pin B. Set to 2 to read from pin Z.
state
:HIGH or LOW (to read data during signal HIGH or LOW condition)
timeout
:an unsigned long variable that represent micro-second (us) to specify the length of time to wait for pulse start condition. Default is set to 0, which set the function to wait forever until the pulse start condition is detected.
Returns
Pulse width in microsecond (us).
Example
#include <Encoder.h> void setup() { Serial.begin(9600); Enc0.begin(MODE_CAPTURE); } void loop() { Serial.println(Enc0.pulseIn(0, HIGH, 1000L)); delay(100); }
See also
The text of the 86Duino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.