Encoder gives 4 outputs even though I move encoder only once

I am using the encoder.h Library with an alps Ec11E rotary encoder. I have wired up the schematic as shown:

code:

#include <Encoder.h>
Encoder enc(2, 3);

long oldPos = 0;
long pos = 0;
void setup(){
}
void loop()
{
    key = keypad.getKey();
    pos = enc.read();
    if (pos != oldPos)
    {
        if (pos > oldPos)
        {
            oldPos = pos;
            Serial.println("right arrow");
        }
        if (pos < oldPos)
        {
            oldPos = pos;
            Serial.println("Left arrow");
        }
    }
}

the problem I am getting is that every time that I move the encoder one position, the Arduino prints 4 outputs.

That's normal behavior.
image
If you are using such a detent type encoder, there are 4 counts for each click.
Do the desired operation once for every four counts.

How can I get it to have only one output then?

Change the if conditional expression to execute the desired operation when there is an increase or decrease of 4 or more or -4 or less from oldPos.
Please little thinking about it, for your experience.

How can I get it to have only one output then?

pos = enc.read()/4;

Thanks! I did that and I also replaced pos = enc.read(); with pos = enc.read()/4;

Hi,
Also put 0.1uF capacitors between each of the encoder inputs and gnd.

Thanks.... Tom... :smiley: :+1: :coffee: :australia:

Is that really a good idea? :face_with_raised_eyebrow:

Hi,

Its one of these;

Tom... :smiley: :+1: :coffee: :australia:

You're up too late! Go to bed!

That's where I'm going right now! :yawning_face:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.