#include "FastLED.h"
#define NUM_LEDS 30
#define DATA_PIN 4
#define BUTTON_PIN 2
CRGB leds[NUM_LEDS];
int mode = 0;
int lastButtonState = HIGH;
void setup() {
Serial.begin(9600); // Inicializar la comunicación serial
pinMode(BUTTON_PIN, INPUT_PULLUP);
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
}
void loop() {
checkButton(); // Llamar al método que comprueba el estado del botón
switch (mode) {
case 0:
combinedMode();
break;
case 1:
modeTwo();
break;
case 2:
modeThree();
break;
}
}
int checkButton() {
int buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW && lastButtonState == HIGH) {
mode = (mode + 1) % 3;
delay(200);
}
lastButtonState = buttonState;
return mode;
}
void combinedMode() {
clearLeds();
for (int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed++) {
int mode = checkButton();
if(mode != 0){break;}
leds[whiteLed] = CRGB(255, 255, 255);
if (whiteLed > 0) leds[whiteLed - 1] = CRGB(220, 220, 220);
if (whiteLed > 1) leds[whiteLed - 2] = CRGB(190, 190, 190);
if (whiteLed > 2) leds[whiteLed - 3] = CRGB(120, 120, 120);
if (whiteLed > 3) leds[whiteLed - 4] = CRGB(50, 50, 50);
if (whiteLed > 4) leds[whiteLed - 5] = CRGB(1, 1, 1);
if (whiteLed > 5) leds[whiteLed - 6] = CRGB(0, 0, 0);
FastLED.show();
delay(20);
}
clearLeds();
for (int whiteLed = NUM_LEDS - 1; whiteLed >= 0; whiteLed--) {
int mode = checkButton();
if(mode != 0){break;}
leds[whiteLed] = CRGB(255, 255, 255);
if (whiteLed < 29) leds[whiteLed + 1] = CRGB(220, 220, 220);
if (whiteLed < 28) leds[whiteLed + 2] = CRGB(190, 190, 190);
if (whiteLed < 27) leds[whiteLed + 3] = CRGB(120, 120, 120);
if (whiteLed < 26) leds[whiteLed + 4] = CRGB(50, 50, 50);
if (whiteLed < 25) leds[whiteLed + 5] = CRGB(1, 1, 1);
if (whiteLed < 24) leds[whiteLed + 6] = CRGB(0, 0, 0);
FastLED.show();
delay(20);
}
clearLeds();
}
void modeTwo() {
checkButton();
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Green;
}
FastLED.show();
}
void modeThree() {
checkButton();
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Blue;
}
FastLED.show();
}
void clearLeds() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Black;
}
FastLED.show();
}