Just insert a counter inside your PinA and PinB routines to count the number of pulses before you increment your encoder variable.
/*******Interrupt-based Rotary Encoder Sketch*******
by Simon Merrett, based on insight from Oleg Mazurov, Nick Gammon, rt, Steve Spence
*/
const int pinA = 2; // Our first hardware interrupt pin is digital pin 2
const int pinB = 3; // Our second hardware interrupt pin is digital pin 3
const int stepPin = 10; // pin for pulsing a step to the stepper driver
const int dirPin = 9; // pin for setting stepper driver direction
const bool CW = 0; // clockwise rotation direction (viewed from encoder body out toward shaft--like CNC Machines)
const bool CCW = 1; // counter-clockwise rotation direction (see above, these directions appear reverse when viewed from shaft side)
//#define SLEEP 12 // Pin 12 connected to SLEEP pin
volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
volatile unsigned long encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
volatile byte stepCount;
const byte maxStepCount = 4; // scale encoder pulses by /4
volatile bool encoderDir = CW; // stores the direction of rotation clockwise = 0 or counter-clockwise = 1
unsigned long encoderPosOld = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
void setup() {
pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
attachInterrupt(0, PinA, RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
attachInterrupt(1, PinB, RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
Serial.begin(115200); // start the serial monitor link
// pinMode(SLEEP, OUTPUT);
// digitalWrite(SLEEP, HIGH); // Wake up EasyDriver
}
void PinA() {
byte reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
if (reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
stepCount = (stepCount - 1) % maxStepCount;
if ( stepCount == 0 ) {
encoderPosRaw--; //decrement the encoder's position count
encoderDir = CW;
digitalWrite(dirPin, encoderDir);
digitalWrite(stepPin, HIGH);
//delayMicroseconds(5);
digitalWrite(stepPin, LOW);
}
bFlag = 0; //reset flags for the next turn
aFlag = 0; //reset flags for the next turn
}
else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
}
void PinB() {
byte reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
stepCount = (stepCount + 1) % maxStepCount;
if ( stepCount == 0 ) {
encoderPos ++; //increment the encoder's position count
encoderDir = CCW;
digitalWrite(dirPin, encoderDir);
digitalWrite(stepPin, HIGH);
//delayMicroseconds(5);
digitalWrite(stepPin, LOW);
}
bFlag = 0; //reset flags for the next turn
aFlag = 0; //reset flags for the next turn
}
else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
}
void loop() {
noInterrupts();
unsigned long encoderPosNew = encoderPos;
interrupts();
if (encoderPosOld != encoderPosNew) {
Serial.print(encoderPosNew);
if (encoderDir == CW) Serial.println(" CW");
if (encoderDir == CCW) Serial.println(" CCW");
encoderPosOld = encoderPosNew;
// digitalWrite(SLEEP, LOW); // Wake up EasyDriver
}
}
A couple of notes:
- you don't need to disable/re-enable interrupts inside PinA and PinB, that happens automatically since these are interrupt routines.
- your encoderPos variable should be
volatile unsigned longso any wrap-around will work without any code. Your checks forif (encoderPos > 2147483647L)can never be true since that is the maximum value a 32 bit int can hold. It can never be larger than that. Same for your negative check. - You need to make a copy of encoderPos in your main loop since a 4 byte variable can not be accessed atomically.