Function for reading encoder and updating counter.

This commit is contained in:
2020-08-13 21:47:37 +02:00
parent 71fbf3f44c
commit dc493ccf03

View File

@@ -79,6 +79,34 @@ void displayFromSerialInput() {
}
}
boolean readEncoderAndUpdateCounter() {
currentStateCLK = digitalRead(CLK);
// If last and current state of CLK are different, then pulse occurred
// React to only 1 state change to avoid double count
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != currentStateCLK) {
counter --;
currentDir ="CCW";
} else {
// Encoder is rotating CW so increment
counter ++;
currentDir ="CW";
}
lastStateCLK = currentStateCLK;
return true;
}
lastStateCLK = currentStateCLK;
return false;
}
void drawHeader(String textToDisplay) {
display.setTextSize(1);
display.setCursor(2,0);