Rotary Encoder Directional Issues

Hello all,

I am nearing completion for a project I've been working on, and would appreciate any help!

I was originally using a rotary encoder by reading the pins until I realized/was told how ineffecient and how I could miss pulses that way.

Since, I have been using this library: GitHub - PaulStoffregen/Encoder: Quadrature Encoder Library for Arduino

By reading pins I was able to distinguish between output A and output B to directionalize the encoder, however since using the library, I have not been able to find a way to do so considering that the myEnc.read() only ever returns a positive value, regardless of the direction of rotation.

Is there a way I can use this library to detect direction of rotation?

Thank you very much!

This is the library example I was referencing:

#define ENCODER_DO_NOT_USE_INTERRUPTS
#include <Encoder.h>

Encoder myEnc(5, 6);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(9600);
  Serial.println("Basic NoInterrupts Test:");
}

long position  = -999;

void loop() {
  long newPos = myEnc.read();
  if (newPos != position) {
    position = newPos;
    Serial.println(position);
  }

}

Here is my attempted implementation with directions.

long newPos = myEnc.read();
if (newPos != position) 
{
  if (newPos < position)
  {
    inches = inches - (0.03926990816 * pitchDiameter);
  }
  else if (newPos > position)
  {
    inches = inches + 0.03926990816 * pitchDiameter;
  }
  position = newPos;
}

reingoldm7:
myEnc.read() only ever returns a positive value, regardless of the direction of rotation.

Then something's wrong. A screen capture of my running of the sketch:

encoder output.PNG

Even if you only get positive values, the value you get one time is higher than the value you got last time, or it is lower. If the value is higher, you are rotating the encoder one way. If it is lower, you are rotating the encoder the other way.

Hello guys,

It appears you were right, doug. I had made a custom cable to attach the encoder to my setup, and one of the connections must have been off.

Paul, it was actually so messed up that it was incrementing upwards only, never less then the previous reading.

Thank you again guys!