Rotary Encoder skips numbers between clicks

I have question about programming the rotary encoder:
it works, but when I go from click to click it increases randomly by 2, 3 or 4 .
Is that normal? I was expecting it to go one by one, and negative on the left, positive on the right.
Here the code I used:

#include // Verwendung der Bibliothek
const int CLK = 6; // Definition der Pins. CLK an D6, DT an D5.
const int DT = 5;
const int SW = 2; // Der Switch wird mit Pin D2 Verbunden. ACHTUNG : Verwenden Sie einen interrupt-Pin!
long altePosition = -999; // Definition der "alten" Position (Diese fiktive alte Position wird benötigt, damit die aktuelle Position später im seriellen Monitor nur dann angezeigt wird, wenn wir den Rotary Head bewegen)

Encoder meinEncoder(DT,CLK); // An dieser Stelle wird ein neues Encoder Projekt erstellt. Dabei wird die Verbindung über die zuvor definierten Varibalen (DT und CLK) hergestellt.

void setup() // Beginn des Setups

{
Serial.begin(9600);

pinMode(SW, INPUT_PULLUP); // Hier wird der Interrupt installiert.

attachInterrupt(digitalPinToInterrupt(SW), Interrupt, CHANGE); // Sobald sich der Status (CHANGE) des Interrupt Pins (SW = D2) ändern, soll der Interrupt Befehl (onInterrupt)ausgeführt werden.
}

void loop()

{

long neuePosition = meinEncoder.read(); // Die "neue" Position des Encoders wird definiert. Dabei wird die aktuelle Position des Encoders über die Variable.Befehl() ausgelesen.
neuePosition=-neuePosition; //invert rotation direction
if (neuePosition != altePosition) // Sollte die neue Position ungleich der alten (-999) sein (und nur dann!!)...
{
altePosition = neuePosition;
Serial.println(neuePosition); // ...soll die aktuelle Position im seriellen Monitor ausgegeben werden.
}

}

void Interrupt() // Beginn des Interrupts. Wenn der Rotary Knopf betätigt wird, springt das Programm automatisch an diese Stelle. Nachdem...

{
Serial.println("Switch betaetigt"); //... das Signal ausgegeben wurde, wird das Programm fortgeführt.

}

You started a topic in the Uncategorised category of the forum when its description explicitly tells you not to

Your topic has been moved to a relevant category. Please be careful in future when deciding where to start new topics

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Please use code tags when posting code.

Yes, this is normal for a quadrature encoder. Usually there is a full cycle of four transitions between detents (clicks). If you want to get just one increment per click, then you should look for one of the four transitions in the gray code cycle.

So instead of interrupting on change, just count the rising edges or the falling edges on one pin and use the other pin just to determine which direction it's going.

1. Are you using the following Rotatry Encoder (Fig-1? If not, please post the picture of the Rotary Encoder you are using.

image
Figure-1:

2. When yoy say click, do you mean that you are turning the shaft of the encoder step-by-step?

A mechanical switch bounces. See the Debounce example for debouncing the signals. You also can use an RC low pass filter to reduce bouncing.

Rotary encoders are available with various coding schemes for the output. Which does your switch have?

So we're all on the same page, which Arduino are you using?

If feasible, I'd just let a library like:

or

handle the interrupt bother. And, you can get a simple up/down signal back as opposed to fussing with counting/dividing intermediate encoder steps.

Hi, @arduinomarc

Try 0.1uF capacitors, each between each encoder output pin and gnd.

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