#define PIN_LED 13
#define PIN_INPUT 2
#define TIEMPO_ANTIRREBOTE 50L
#define TIEMPO_LED_ENCENDIDO 2000L
byte estado_pulsador, estado_led;
byte flanco_pulsador;
unsigned long millis_pulsador, millis_led;
void setup() {
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_INPUT, INPUT_PULLUP);
estado_pulsador = 0;
estado_led = 0;
flanco_pulsador = 0;
}
void loop() {
int boton = digitalRead(PIN_INPUT);
switch (estado_pulsador) {
case 0: // Reposo
if (boton == LOW) {
millis_pulsador = millis();
estado_pulsador = 1;
}
break;
case 1: // Delay antirrebote
if (boton == HIGH) {
estado_pulsador = 0;
}
if ((millis() - millis_pulsador) > TIEMPO_ANTIRREBOTE) {
estado_pulsador = 2;
flanco_pulsador = 1;
}
break;
case 2:
if (boton == HIGH) {
estado_pulsador = 0;
}
break;
}
switch (estado_led) {
case 0:
digitalWrite(PIN_LED, LOW);
if (flanco_pulsador == 1) {
flanco_pulsador = 0;
estado_led = 1;
millis_led = millis();
}
break;
case 1:
digitalWrite(PIN_LED, HIGH);
if (flanco_pulsador == 1) {
flanco_pulsador = 0;
millis_led = millis();
}
if ((millis() - millis_led) > TIEMPO_LED_ENCENDIDO) {
estado_led = 0;
}
break;
}
}