read()
Description
Function to read the encoder’s counter value and pulse width.
Note:  Under different mode, the return value has different representation. See the Returns section below.
Syntax
Enc0.read()
Enc1.read()
Enc2.read()
Enc3.read()
Parameters
Enc0、Enc1、Enc2、Enc3:Corresponding to ENC0, ENC1, ENC2 and ENC3 encoder interfaces.
Returns
Pulse/DIR, CW/CCW and A/B Pulse modes: Return the encoder’s counter value.
PWM pulse capture mode:  Return the pulse width value for digital HIGH or LOW condition in micro-second (us), where the maximum value is 71 minutes. (Note:  Under this mode, the read() function can only be called within an associated ISR function. When using this function elsewhere, it always return 0.)
SSI mode: Return SSI absolute value from the encoder.
Example
Set ENCO to A/B Pulse mode, and read the encoder’s counter value.
#include <Encoder.h>
void setup() {
  Serial.begin(9600);
  Enc0.begin(MODE_AB_PHASE);
}
void loop() {
  Serial.println(Enc0.read());// Read encoder counter value
  delay(100);
}
Set ENC1 to PWM pulse capture mode, read the pulse width for digital HIGH and LOW condition.
#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)// Read pulse width during digital LOW condition
      num1 = Enc1.read();
    else if (flag == INTR_A_PULSE_HIGH) // Read pulse width during digital HIGH condition
      num2 = Enc1.read();
}
void setup() {
   Serial.begin(9600);
   Enc1.begin(MODE_CAPTURE);
   Enc1.attachInterrupt(encoder_isr);// Mount ISR function and enable interrupt
}
void loop() {
   Serial.print("LOW:");
   Serial.print(num1); // Output pulse width during digital LOW condition in micro-second (us)
   Serial.print("    ");
   Serial.print("HIGH:");
   Serial.println(num2); // Output pulse width during digital HIGH condition in micro-second (us)
   delay(100);
}
See also
- attachInterrupt()
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.
