Hello
I'm working on a volume knob for my PC. I have a keyes rotary encoder with a switch-button. I've connected everything up to my Arduino pro micro like this:
The img link doesnt work. Please see the image here:
sorry for the bad drawing..
I've tried different codes from this thread:
https://forum.arduino.cc/index.php?topic=242356.msg2703618#msg2703618
I allways get the same problem:
Sometimes (quite often) when i rotate the knob it gets in a state where it keeps incrementing the number. It's like i'm rotating it without touching it. If i twist it the other was it usually stops. It's often one directional (it happens when i twist right, not left).
From the serial print from one of the codes i tried it seems that the arduino doesn't even think i'm twisting the knob, but its still triggering:
"
PinA: 0 PinB: 1 Counter: 1113
PinA: 0 PinB: 0 Counter: 1114
PinA: 0 PinB: 0 Counter: 1115
PinA: 0 PinB: 1 Counter: 1114
PinA: 0 PinB: 0 Counter: 1115
PinA: 0 PinB: 0 Counter: 1116
PinA: 0 PinB: 0 Counter: 1117
PinA: 0 PinB: 1 Counter: 1116
PinA: 0 PinB: 0 Counter: 1117
"
I've tried disconnecting the 10k R from V++ and use the internal pull-up instead, same problem.
Sometimes it works for a while, then suddenly it happens.
The current code i'm, testing (i didnt write it myself) is this:
volatile boolean TurnDetected;
volatile boolean up;
static long virtualPosition=0; // without STATIC it does not count correctly!!!
const int PinCLK=3; // Used for generating interrupts using CLK signal
const int PinDT=4; // Used for reading DT signal
const int PinSW=5; // Used for the push button switch
void isr () { // Interrupt service routine is executed when any CHANGE transition is detected on CLK
volatile boolean CLK = digitalRead(PinCLK);
volatile boolean DT = digitalRead(PinDT);
up=((!CLK && DT)||(CLK && !DT));
TurnDetected = true;
}
void setup () {
pinMode(PinCLK,INPUT);
pinMode(PinDT,INPUT);
pinMode(PinSW,INPUT);
attachInterrupt (0,isr,CHANGE); // interrupt 0 is always connected to pin 2 on Arduino UNO
Serial.begin (9600);
Keyboard.begin();
Serial.println("Start");
}
void loop () {
if (!digitalRead(PinSW)) { // check if pushbutton is pressed
virtualPosition=0; // if YES, then reset counter to ZERO
Serial.print ("Reset = "); // Using the word RESET instead of COUNT here to find out a buggy encoder
Serial.println (virtualPosition);
// Keyboard.print("Resett");
}
if (TurnDetected) { // do this only if rotation was detected
if (up){
virtualPosition++;
// Keyboard.print("Volum opp");
}
else{
virtualPosition--;
// Keyboard.print("Volum ned");
TurnDetected = false; // do NOT repeat IF loop until new rotation detected
}
Serial.print ("Count = ");
Serial.println (virtualPosition);
}
}
I'd be gratefull for any help. Please keep in mind that i'm a n00b on this stuff ![]()