Encoder icon indicating copy to clipboard operation
Encoder copied to clipboard

wrong counting backwards

Open tappete opened this issue 8 months ago • 4 comments

Hi Paul, I use your code without problems on an Arduino iot33 with rotary encoder but with a high resolution encoder I see the count is constant but this happens:

counting forward from 0 to 17750, when I go back I get 3050 (not 0) then if I go forward I get 20800 and when I go back I get 6100 and so on...

In practice when I go forward I have constant counts of 17750 impulses while backwards 14700 effectively always losing 3050 when I go back

This is my test code

#include <Arduino.h>                   
#include <wiring_private.h>
#include <Wire.h> 
#include <Adafruit_SH110X.h>    
     
#define ENCODER_OPTIMIZE_INTERRUPTS
#include <Encoder.h>

int A = 2; 
int B = 3;

Encoder myEnc(A, B);

#define i2c_Address 0x3c //initialize with the I2C display addr 0x3C 

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1    //   QT-PY / XIAO
Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  
    Wire.begin();
  
    delay(50);
    display.begin(i2c_Address, true); // Address 0x3C default
    display.clearDisplay();
}

long oldPosition  = -999;

void loop() {
  
    long newPosition = myEnc.read();
    if (newPosition != oldPosition) {
    oldPosition = newPosition;


    display.setTextSize(1);
    display.setTextColor(SH110X_WHITE);
    display.setCursor(0, 0);
    display.print("pos");
    display.setCursor(30, 0);
    display.println(oldPosition);

  
    display.display();
    display.clearDisplay();
  }
}

If I don't use 2 interrupts, the count is completely off.

With or without the "ENCODER OPTIMIZE INTERRUPTS" function the result changes little.

I don't know what it could depend on.

Thank you, Davide

tappete avatar Nov 07 '23 13:11 tappete