How to check whether a Rotary Encoder Value is positive or negative?

Hello guys!

I am currently using the Encoder Library (Encoder Library, for Measuring Quadarature Encoded Position or Rotation Signals) and coded it in void loop() like this:

 long newRotate;
  newRotate = rotate.read();
  if (newRotate != positionRotate) {
    positionRotate = newRotate;

    if(positionRotate < 0){
      counter--;
    } else if(positionRotate > 0){
      counter++;
    }
  }

  Serial.println(counter);

  rotaryState = counter;
  
  if((counter > 0) && (rotaryState != prevRotaryState)) {
    Keyboard.press(KEY_UP_ARROW);
    delay(100);
    Keyboard.releaseAll();
  }
  if((counter < 0) && (rotaryState != prevRotaryState)) {
    Keyboard.press(KEY_DOWN_ARROW);
    delay(100);
    Keyboard.releaseAll();
  }

  if (readBR(4) == 0) {
    counter = 0;
  }

  prevRotaryState = rotaryState;

Now the Problem is I cannot reset the read rotate value.

Is there a way to reset it or how can I check whether I am rotating clockwise or counterclockwise?

EDIT :scream: unless you are interested in making this work, I'd suggest to not reinvent the wheel and use an encoder library

PS: Don't post snippets (Snippets R Us!)

I don't think that you have read my post.

You are right, apologies, I did not catch the first line

why don't you use directly the newRotate value

if it's the same as the previous one, nothing has changed
if it's higher you rotated right
if it's lower you rotated left

you could (but why) reset the encoder's position by just writing a new value to it

No worries^^

Well I don't need the exact value but only if its clockwise or counterclockwise.

And how do I reset the encoder's position? I couldn't find any command for it online.

may be just something like this if you don't care by how many ticks you moved

long newRotate = rotate.read();
if (newRotate > positionRotate) { // CW
  Keyboard.press(KEY_UP_ARROW);
  delay(100);
  Keyboard.releaseAll();
} else if (newRotate < positionRotate) { // CCW
  Keyboard.press(KEY_DOWN_ARROW);
  delay(100);
  Keyboard.releaseAll();
} // else no change 
positionRotate = newRotate;

no need to reset but if you want, you could do rotate.write(0);

thanks a lot, it works perfect!

have fun !

(curious what you drive with this?)