ph5
1
Hello guys!
I am currently using the Encoder Library (Encoder Library, for Measuring Quadarature Encoded Position or Rotation Signals) and coded it in void loop() like this:
long newRotate;
newRotate = rotate.read();
if (newRotate != positionRotate) {
positionRotate = newRotate;
if(positionRotate < 0){
counter--;
} else if(positionRotate > 0){
counter++;
}
}
Serial.println(counter);
rotaryState = counter;
if((counter > 0) && (rotaryState != prevRotaryState)) {
Keyboard.press(KEY_UP_ARROW);
delay(100);
Keyboard.releaseAll();
}
if((counter < 0) && (rotaryState != prevRotaryState)) {
Keyboard.press(KEY_DOWN_ARROW);
delay(100);
Keyboard.releaseAll();
}
if (readBR(4) == 0) {
counter = 0;
}
prevRotaryState = rotaryState;
Now the Problem is I cannot reset the read rotate value.
Is there a way to reset it or how can I check whether I am rotating clockwise or counterclockwise?
J-M-L
2
EDIT
unless you are interested in making this work, I'd suggest to not reinvent the wheel and use an encoder library
PS: Don't post snippets (Snippets R Us!)
ph5
3
I don't think that you have read my post.
J-M-L
4
You are right, apologies, I did not catch the first line
why don't you use directly the newRotate value
if it's the same as the previous one, nothing has changed
if it's higher you rotated right
if it's lower you rotated left
you could (but why) reset the encoder's position by just writing a new value to it
ph5
5
No worries^^
Well I don't need the exact value but only if its clockwise or counterclockwise.
And how do I reset the encoder's position? I couldn't find any command for it online.
J-M-L
6
may be just something like this if you don't care by how many ticks you moved
long newRotate = rotate.read();
if (newRotate > positionRotate) { // CW
Keyboard.press(KEY_UP_ARROW);
delay(100);
Keyboard.releaseAll();
} else if (newRotate < positionRotate) { // CCW
Keyboard.press(KEY_DOWN_ARROW);
delay(100);
Keyboard.releaseAll();
} // else no change
positionRotate = newRotate;
no need to reset but if you want, you could do rotate.write(0);
ph5
7
thanks a lot, it works perfect!
J-M-L
8
have fun !
(curious what you drive with this?)