mathertel / RotaryEncoder

RotaryEncoder Arduino Library
Other
328 stars 107 forks source link

getDirection() is not working as expected #23

Closed IGB-Germany closed 3 years ago

IGB-Germany commented 3 years ago

If I call getDirection() I dont get the expected results. CLOCKWISE is detected when I turn COUNTERCLOCKWISE.

The serial output below shows correct newPos is 0,1,2,3,4,3,2,1,0,-1,-2,-3,-4 But the corresponding ::Direction is not correct in every case. It only is correct if newPos reaches 0.

I have no clue why - maybe because _positionExtPrev is not manipulated in tick().

Sketch

void loop() { static int pos = 0; encoder.tick();

int newPos = encoder.getPosition(); if (pos != newPos) { Serial.print(newPos); Serial.println(); processEncoder(encoder); pos = newPos; } // if

// The End void processEncoder(RotaryEncoder myEncoder) { switch (myEncoder.getDirection()) { case RotaryEncoder::Direction::NOROTATION: break;

case RotaryEncoder::Direction::CLOCKWISE:
  {
    Serial.println(F("CLOCKWISE"));
  }
  break;

case RotaryEncoder::Direction::COUNTERCLOCKWISE:
  {
    Serial.println(F("COUNTERCLOCKWISE"));
  }
  break;

default:
  {

  }

} }

} // loop ()

Serial output

COUNTERCLOCKWISE 0 1 CLOCKWISE 2 CLOCKWISE 3 CLOCKWISE 4 CLOCKWISE 3 CLOCKWISE 2 CLOCKWISE 1 CLOCKWISE 0 -1 COUNTERCLOCKWISE -2 COUNTERCLOCKWISE -3 COUNTERCLOCKWISE -4 COUNTERCLOCKWISE 2 CLOCKWISE 3 CLOCKWISE 4 CLOCKWISE 3 CLOCKWISE 2 CLOCKWISE 1 CLOCKWISE 0 -1 COUNTERCLOCKWISE -2 COUNTERCLOCKWISE -3 COUNTERCLOCKWISE -4 COUNTERCLOCKWISE

mathertel commented 3 years ago

Could not reproduce this. See the new version of SimplePollRotator.ino example where direction is written to Serial and re-test.