int A = 2;
int B = 4;
int ANTERIOR = 50;
volatile int POSICION = 50; //Se define variables volatile cuando van a ser utilizadas por otras funciones aparte de setup y loop.
void setup() {
pinMode(A, INPUT);
pinMode(B, INPUT);
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(A), encoder, LOW);
Serial.println("Listo");
}
void loop() {
if (POSICION != ANTERIOR) {
Serial.println(POSICION);
ANTERIOR = POSICION;
}
}
void encoder() {
static unsigned long ultimaInterrupcion = 0;
unsigned long tiempoInterrupcion = millis();
if (tiempoInterrupcion - ultimaInterrupcion > 5) { //Rutina Antirebote
if (digitalRead(B) == HIGH) {
POSICION--;
} else {
POSICION++;
}
POSICION = min(100, max(0, POSICION));
ultimaInterrupcion = tiempoInterrupcion;
}
}