Rotary encoders

Hi,

I have to use rotary encoders for a small project, so I bought them very cheap on eBay. I got them to work but I have one problem, because they are quadrature cycles I can't get it to increment or decrement by one for each step, instead I get 4 pulses for each step.

Is it possible to make to change only by one instead of 4?

This is the code I use :

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(5, 6);
//   avoid using pins with LEDs attached

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

long oldPosition  = -999;

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

Thanks.

How about just dividing the result by 4 / shifting it down 2 bits?

I already tried to divide by 4 but it's not accurate sometimes even if I turn it 3 steps it doesn't change.

I noticed also that when I turn it quickly it doesn't follow it's like it's too fast for the arduino.

/ Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability

And what type do you use? Take a guess...

Try (2, 3) or (2, 6) for the other possibilities.

hardmehdi:
Is it possible to make to change only by one instead of 4?

Yes. With a "quadrature rotary encoder" you can do "quarter step", "half step" and "full step" counting.

You are doing "quarter step counting", but you want "full step counting".

So you can either replace/configure/adapt/rewrite your "library" to do "full step counting".
(The code affected is in the library and NOT in the example you showed.)

Or write your own code for "full step counting"!

Dodgy code?

Should not use interrupts; needs to include debouncing.