//https://forum.arduino.cc/t/optical-encoder-skips-pulses-at-high-speeds/1275203/1
#define encoderPinA 2
#define encoderPinB 3
volatile long encoderCount = 0;
//-------------------------------------------------------------
void setup() {
Serial.begin(115200);
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(encoderPinA), handleEncoder, RISING);
// attachInterrupt(digitalPinToInterrupt(encoderPinB),handleEncoder2, RISING);
}
//-------------------------------------------------------------
void loop() {
if ((encoderCount % 1000) == 0 ) {
Serial.println(encoderCount);
}
}
//-------------------------------------------------------------
void handleEncoder() {
if (digitalRead(encoderPinA) > digitalRead(encoderPinB)) {
encoderCount++;
}
else {
encoderCount--;
}
}