#include <Arduino.h>
// --- Pines (Mapeo de pines seguros en la ESP32) ---
#define BTN1_PIN GPIO_NUM_13 // Botón 1 (Pull-down externo) -> Controla LED 1 [1]
#define BTN2_PIN GPIO_NUM_27 // Botón 2 (Pull-down externo) -> Controla LED 2 [1]
#define LED1_PIN GPIO_NUM_4 // LED 1 externo [1]
#define LED2_PIN GPIO_NUM_5 // LED 2 externo [1]
// Tiempo de espera para el filtro de antirrebote (ms)
const uint16_t DEBOUNCE_MS = 20;
// Función de lectura estable (antirrebote básico por software)
static inline int readButtonStable(gpio_num_t pin) {
int v = digitalRead(pin);
delay(DEBOUNCE_MS);
return (digitalRead(pin) == v) ? v : digitalRead(pin);
}
void setup() {
// Configura las entradas digitales (sin pull-up/down interno ya que es externo)
pinMode(BTN1_PIN, INPUT);
pinMode(BTN2_PIN, INPUT);
// Configura las salidas para los LEDs
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
// Asegura que ambos comiencen apagados
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
}
void loop() {
// 1. Lee de forma estable el estado de ambos botones usando nuestra función modular
int btn1 = readButtonStable(BTN1_PIN);
int btn2 = readButtonStable(BTN2_PIN);
// 2. Control independiente: Enciende el LED correspondiente SOLO si su botón está presionado (HIGH) [1]
digitalWrite(LED1_PIN, (btn1 == HIGH) ? HIGH : LOW);
digitalWrite(LED2_PIN, (btn2 == HIGH) ? HIGH : LOW);
// Un pequeño retraso de respiro para no sobrecargar el procesador
delay(5);
}