// Input pins to connect to the encoder
const uint8_t CLK = 10;
const uint8_t DT = 11;
int16_t inputDelta = 0; // Counts up or down depending which way the encoder is turned
bool printFlag = false; // Flag to indicate that the value of inputDelta should be printed
void setup() {
Serial1.begin(115200);
Serial1.println("Serial Ready!");
pinMode(CLK, INPUT_PULLUP);
pinMode(DT, INPUT_PULLUP);
}
void loop() {
readEncoder();
printDelta();
}
void readEncoder() {
static uint8_t state = 0;
bool CLKstate = digitalRead(CLK);
bool DTstate = digitalRead(DT);
switch (state) {
case 0: // Idle state, encoder not turning
if (!CLKstate){ // Turn clockwise and CLK goes low first
state = 1;
} else if (!DTstate) { // Turn anticlockwise and DT goes low first
state = 4;
}
break;
// Clockwise rotation
case 1:
if (!DTstate) { // Continue clockwise and DT will go low after CLK
state = 2;
}
break;
case 2:
if (CLKstate) { // Turn further and CLK will go high first
state = 3;
}
break;
case 3:
if (CLKstate && DTstate) { // Both CLK and DT now high as the encoder completes one step clockwise
state = 0;
++inputDelta;
printFlag = true;
}
break;
// Anticlockwise rotation
case 4: // As for clockwise but with CLK and DT reversed
if (!CLKstate) {
state = 5;
}
break;
case 5:
if (DTstate) {
state = 6;
}
break;
case 6:
if (CLKstate && DTstate) {
state = 0;
--inputDelta;
printFlag = true;
}
break;
}
}
void printDelta() {
if (printFlag) {
printFlag = false;
Serial1.println(inputDelta);
}
}