// Rotary Encoder Inputs
#define CLK 14
#define DT 27
int counter = 0;
String currentDir = "";
bool state = false;
void IRAM_ATTR updateEncoder() {
state = true;
}
void setup() {
// Set encoder pins as inputs
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
// Setup Serial Monitor
Serial.begin(115200);
// Call updateEncoder() when any high/low changed seen
// on interrupt 0 (pin 2), or interrupt 1 (pin 3)
attachInterrupt(14, updateEncoder, CHANGE);
}
void loop() {
if (state) {
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != digitalRead(CLK)) {
counter++;
currentDir = "CW";
} else {
// Encoder is rotating CW so increment
counter--;
currentDir = "CCW";
}
Serial.print("Direction: ");
Serial.print(currentDir);
Serial.print(" | Counter: ");
Serial.println(counter);
state = false;
}
}