pulseInNanoseconds()

Description

This function is similar to the pulseIn() function, returns value in nano-second (ns) and limite the maximum pulse width to 4.2 seconds.
Note: This function is designed to work in the PWM pulse width capture mode. This function cannot be used in conjunction with the encoder library’s attachInterrupt(). After calling attachInterrupt() and mount ISR, the pulseInNanoseconds() function does not work.

Syntax


Enc0.pulseInNanoseconds(pin, state)
Enc0.pulseInNanoseconds(pin, state, timeout)

Enc1.pulseInNanoseconds(pin, state)
Enc1.pulseInNanoseconds(pin, state, timeout)

Enc2.pulseInNanoseconds(pin, state)
Enc2.pulseInNanoseconds(pin, state, timeout)

Enc3.pulseInNanoseconds(pin, state)
Enc3.pulseInNanoseconds(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 nanosecond (ns)

Example

#include <Encoder.h>

void setup() {
  Serial.begin(9600);
  Enc0.begin(MODE_CAPTURE);
}

void loop() {
  Serial.println(Enc0.pulseInNanoseconds(0, HIGH, 1000L));
  delay(100);
}

See also

pulseIn()


Libraries Reference Home

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.