readNanoseconds()

Description

This function can only be used under PWM Pulse capture mode, similar function as read() with returning variable in nanosecond (ns) for the width of the pulse.

Syntax


Enc0.readNanoseconds()
Enc1.readNanoseconds()
Enc2.readNanoseconds()
Enc3.readNanoseconds()

Parameters

Enc0、Enc1、Enc2、Enc3:Corresponding to ENC0, ENC1, ENC2 and ENC3 encoder interfaces.

Returns

Return width of the pulse in LOW and HIGH condition, where the longest pulse is limited to 4.2 seconds.
Note: readNanoseconds() is called within an ISR function. When calling from other location, it always return 0.

Example

#include <Encoder.h>

volatile unsigned long num1 = 0L;
volatile unsigned long num2 = 0L;
void encoder_isr(int flag) { // ISR function
    if(flag == INTR_A_PULSE_LOW)
      num1 = Enc1.readNanoseconds(); // Read pulse width during signal LOW condition
    else if(flag == INTR_A_PULSE_HIGH)
      num2 = Enc1.readNanoseconds(); // Read pulse width during signal HIGH condition
}

void setup() {
  Serial.begin(9600);
  Enc1.begin(MODE_CAPTURE);
  Enc1.attachInterrupt(encoder_isr); // Mount ISR and enable interrupt
}

void loop() {
  Serial.print("LOW:");
  Serial.print(num1); // Output pulse width value in signal LOW condition, in nanosecond.
  Serial.print("    ");
  Serial.print("HIGH:");
  Serial.println(num2); // Output pulse width value in signal HIGH condition, in nanosecond. 
  delay(100);
}

See also

begin()
attachInterrupt()


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.