#define led1 9 // LED azul
#define led2 10 // LED rojo
#define led3 11 // LED amarillo
#define puls1 4 // Botón azul
#define puls2 7 // Botón rojo
#define puls3 8 // Botón amarillo
int estadoAnterior1 = 0;
int estadoAnterior2 = 0;
int estadoAnterior3 = 0;
void setup() {
// Configurar los LEDs como salida
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
// Configurar los botones como entrada
pinMode(puls1, INPUT);
pinMode(puls2, INPUT);
pinMode(puls3, INPUT);
// Inicializar comunicación serie
Serial.begin(9600);
}
void loop() {
// Leer el estado actual de los botones
int estadoActual1 = digitalRead(puls1);
int estadoActual2 = digitalRead(puls2);
int estadoActual3 = digitalRead(puls3);
// Detectar flanco para el botón azul
if (estadoActual1 == HIGH && estadoAnterior1 == LOW) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
Serial.println("Botón Azul presionado");
}
// Detectar flanco para el botón rojo
if (estadoActual2 == HIGH && estadoAnterior2 == LOW) {
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
Serial.println("Botón Rojo presionado");
}
// Detectar flanco para el botón amarillo
if (estadoActual3 == HIGH && estadoAnterior3 == LOW) {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
Serial.println("Botón Amarillo presionado");
}
// Apagar los LEDs si no hay botones presionados
if (estadoActual1 == LOW && estadoActual2 == LOW && estadoActual3 == LOW) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
}
// Actualizar los estados anteriores
estadoAnterior1 = estadoActual1;
estadoAnterior2 = estadoActual2;
estadoAnterior3 = estadoActual3;
delay(50); // Retardo para evitar rebotes
}