volatile int encoderPosition = 0;
int clkPin = 3;
int dtPin = 5;
void setup() {
Serial.begin(115200);
pinMode(clkPin, INPUT);
pinMode(dtPin, INPUT);
attachInterrupt(digitalPinToInterrupt(clkPin), updatePosition, FALLING);
}
void loop() {
Serial.print("Encoder position is: ");
Serial.println(encoderPosition);
delay(100);
}
void updatePosition() {
// Read the state of pin B to determine direction
int dtPinState = digitalRead(dtPin);
if (dtPinState == HIGH) {
encoderPosition += 1; // Clockwise
} else {
encoderPosition -= 1; // Counter-clockwise
}
}