#include // ================= CONSTANTES ================= constexpr uint8_t LED_PIN = 4; constexpr uint8_t NUM_LEDS = 2; constexpr uint8_t SW1_PIN = 6; constexpr unsigned long DEBOUNCE_MS = 50; constexpr uint8_t NUM_COLORES = 5; // ================= ESTRUCTURAS ================= struct Button { uint8_t pin; ///< Pin asignado al botón. bool state; ///< Estado estable del botón (HIGH/LOW). bool lastRead; ///< Última lectura cruda del pin. unsigned long lastChange; ///< Último instante en que cambió la lectura bruta. }; // ================= VARIABLES GLOBALES ================= Adafruit_NeoPixel leds(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800); uint32_t colores[NUM_COLORES]; uint8_t colorIndexPerLed[NUM_LEDS] = {0}; uint8_t ledSeleccionado = 0; Button btn1; Button btn2; // ================= PROTOTIPOS ================= void initializeLeds(); void initializeColors(); void initializeButtons(); bool updateButton(Button &b); void applyColors(); void cycleColorSelected(); void cycleSelectedLed(); // ================= IMPLEMENTACIÓN ================= void initializeLeds() { leds.begin(); leds.show(); } void initializeColors() { colores[0] = leds.Color(255, 255, 0); // Amarillo colores[1] = leds.Color(0, 0, 255); // Azul colores[2] = leds.Color(255, 0, 0); // Rojo colores[3] = leds.Color(0, 255, 0); // Verde colores[4] = leds.Color(255, 255, 255); // Blanco } void initializeButtons() { btn1.pin = SW1_PIN; btn1.state = HIGH; btn1.lastRead = HIGH; btn1.lastChange = 0; pinMode(btn1.pin, INPUT_PULLUP); } bool updateButton(Button &b) { bool raw = digitalRead(b.pin); if (raw != b.lastRead) { b.lastChange = millis(); b.lastRead = raw; } if ((millis() - b.lastChange) > DEBOUNCE_MS) { if (b.state != raw) { b.state = raw; return (b.state == LOW); // Pulsación detectada } } return false; } void applyColors() { for (uint8_t i = 0; i < NUM_LEDS; ++i) { leds.setPixelColor(i, colores[colorIndexPerLed[i] % NUM_COLORES]); } leds.show(); } void cycleColorSelected() { colorIndexPerLed[ledSeleccionado] = (colorIndexPerLed[ledSeleccionado] + 1) % NUM_COLORES; applyColors(); } void cycleSelectedLed() { ledSeleccionado = (ledSeleccionado + 1) % NUM_LEDS; } // ================= SETUP & LOOP ================= void setup() { initializeLeds(); initializeColors(); initializeButtons(); applyColors(); } void loop() { if (updateButton(btn1)) { cycleColorSelected(); } }