Skip to content

Instantly share code, notes, and snippets.

@Mrestof
Created December 15, 2019 19:22
Show Gist options
  • Save Mrestof/6d1c27603f045e3942b1a9f8d184cbdf to your computer and use it in GitHub Desktop.
Save Mrestof/6d1c27603f045e3942b1a9f8d184cbdf to your computer and use it in GitHub Desktop.
This code checks 10 and 11 digital pins of rotary encoder and add or subtract from counter variable.
int counter = 0;
bool R, L, END, ROT;
bool A, B;
bool ff, tf, ft, tt;
bool mode = true;
void setup() {
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
}
void loop() {
A = digitalRead(10);
B = digitalRead(11);
ff = !A && !B;
tf = A && !B;
ft = !A && B;
tt = A && B;
if(ft && !(END || R || L)) R = true;
if(tf && !(END || R || L)) L = true;
if(ff && (R || L)) R = L = END = false;
if(tf && R) {
R = false;
END = ROT = true;
}
if(ft && L) {
L = ROT = false;
END = true;
}
if(tt && END) R = L = END = false;
if(ff && END) {
END = false;
if(ROT) counter += 1;
else counter -= 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment