#define CLK_PIN 4
#define DT_PIN 5
volatile int posicion = 0;
volatile bool cambioPendiente = false;
void IRAM_ATTR isrCLK() {
if (digitalRead(DT_PIN) != digitalRead(CLK_PIN)) {
posicion++;
} else {
posicion--;
}
cambioPendiente = true;
}
void setup() {
Serial.begin(115200);
pinMode(CLK_PIN, INPUT_PULLUP);
pinMode(DT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(CLK_PIN), isrCLK, FALLING);
}
void loop() {
if (cambioPendiente) {
cambioPendiente = false;
int actual = posicion;
Serial.print("Posición: ");
Serial.println(actual);
}
}Loading
esp32-devkit-c-v4
esp32-devkit-c-v4