![]()
¿puedes usar una biblioteca de botones?
Eso haría su vida más fácil (y el código más fácil de leer)
la máquina de estado
podría implementarse de esta manera
#include <OneButton.h> //https://github.com/mathertel/OneButton (doc @ http://www.mathertel.de/Arduino/OneButtonLibrary.aspx)
const int botonPin1 = 2;
const int botonPin2 = 3;
const int ledPin = 13;
OneButton boton1(botonPin1); // activo LOW con PULLUP integrado
OneButton boton2(botonPin2); // activo LOW con PULLUP integrado
enum : byte {LED_OFF, ESTADO_B1, ESTADO_B2, LED_ON} estado;
void apagaLed() {
digitalWrite(ledPin, LOW);
estado = LED_OFF;
}
void enciendeLed() {
digitalWrite(ledPin, HIGH);
estado = LED_ON;
}
void click1() {
switch (estado) {
case LED_OFF: estado = ESTADO_B1; break;
case ESTADO_B2: apagaLed(); break;
default: break;
}
}
void click2() {
switch (estado) {
case ESTADO_B1: enciendeLed(); break;
case LED_ON: estado = ESTADO_B2; break;
default: break;
}
}
void setup() {
pinMode(ledPin, OUTPUT);
boton1.attachClick(click1);
boton2.attachClick(click2);
apagaLed();
}
void loop() {
boton1.tick();
boton2.tick();
}
corto y bueno + fácil de leer, ¿no?
