setIndexReset()
Description
Set an associated interrupt event to reset the index (when pin Z from the encoder interface go HIGH, it trigger the interrupt).
This function can be use under the Pulse/DIR, CW/CCW and A/B Pulse modes.
Attention: Calling this function trigger index reset (when pin Z changes to HIGH), and set encoder’s counter value to 0.
Syntax
Enc0.setIndexReset()
Enc0.setIndexReset(condition)
Enc1.setIndexReset()
Enc1.setIndexReset(condition)
Enc2.setIndexReset()
Enc2.setIndexReset(condition)
Enc3.setIndexReset()
Enc3.setIndexReset(condition)
Parameters
Enc0、Enc1、Enc2、Enc3
:Corresponding to ENC0, ENC1, ENC2 and ENC3 encoder interfaces.
condition
:tcan be set to true or false. To start interrupt event associate with index, set to true. Otherwise, set to false. (This value is set to true by default.)
Returns
None
Example
Set ENC0 to A/B Pulse mode, output the number of turns performed by the rotary encoder and read the counter value (assuming all 3 signals, A, B and Z are connected).
#include <Encoder.h> volatile unsigned long num1 = 0L; volatile unsigned long num2 = 0L; void encoder_isr(int flag) { // ISR function if(flag == INTR_COMPARE) num1++; // When counter value reaches the specified value, increase variable by 1. else if(flag == INTR_INDEX) num2 ++; // When the motor completes a turn, increase variable by 1 } void setup() { Serial.begin(9600); Enc0.begin(MODE_AB_PHASE); Enc0.setIndexReset(); // Set the Index signal (when pin Z changes to HIGH) to trigger an interrupt event and call the associated ISR function Enc0.setComparator(20L); // When counter reach 20, trigger an interrupt event to call the associated ISR function Enc0.attachInterrupt(encoder_isr); // Mount ISR function and enable interrupt } void loop() { Serial.print("Pulse number: "); Serial.print(Enc0.read()); // Read the pulse value Serial.print(" "); Serial.print("index: "); Serial.print(num1); //Output total number of turns by the rotary encoder Serial.print(" "); Serial.print("trigger: "); // Output total number of occurrence where the counter reached 20 and reset. Serial.println(num2); delay(100); }
See also
- begin()
- attachInterrupt()
- read()
- setComparator()
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.