PaulStoffregen / Encoder

Quadrature Encoder Library for Arduino
http://www.pjrc.com/teensy/td_libs_Encoder.html
540 stars 239 forks source link

Feature: getDirection() #93

Open dicagno opened 1 year ago

dicagno commented 1 year ago

Description

A method to determine step direction is missing and would be a great nice-to-have.

Steps To Reproduce Problem

No method to get step direction.

Hardware & Software

Board: any Shields / modules used: any

Usage

int direction = encoder.getDirection();
// 1 -> forward
// -1 -> backwards
// 0 -> no movement

Errors or Incorrect Output

--

bwalc0 commented 6 months ago

If you would like to use it, I have edited Examples -> Basic

long oldPosition  = -999;

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

  int direction = 0;
  if (newPosition != oldPosition) { 

    direction = (newPosition > oldPosition) ? 1 : -1;

    oldPosition = newPosition;
    Serial.println(newPosition);
  }
}
dicagno commented 6 months ago

@bwalc0 thanks for the answer. Did you take a look at the associated PR?