#include <FastLED.h>
// Define los pines GPIO a los que están conectados los colores del LED RGB
#define RED_PIN 2
#define GREEN_PIN 15
#define BLUE_PIN 0 // 4
// Define el tipo de LED RGB (ánodo común o cátodo común)
#define COMMON_ANODE // Comenta esta línea si tu LED es cátodo común
// Define un array para almacenar el color actual del LED
CRGB leds[1];
void setup() {
Serial.begin(115200);
Serial.println("¡LED RGB Creativo en ESP32!");
// Inicializa FastLED con un solo LED
FastLED.addLeds<WS2811, 1, GRB>(leds, 800000); // WS2811 es un protocolo, GRB el orden de los colores
FastLED.setBrightness(150); // Ajusta el brillo máximo (0-255)
// Configura los pines como salidas (aunque FastLED los maneja internamente)
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
// Función para establecer el color del LED RGB
void setRGB(int red, int green, int blue) {
#ifdef COMMON_ANODE
leds[0] = CRGB(255 - red, 255 - green, 255 - blue); // Invertir para ánodo común
#else
leds[0] = CRGB(red, green, blue);
#endif
FastLED.show();
}
// Efecto de "respiración" de color
void breathingEffect(CRGB color, int breathSpeed = 10) {
for (int brightness = 0; brightness < 256; brightness++) {
leds[0] = color.scale8(brightness);
FastLED.show();
delay(breathSpeed);
}
for (int brightness = 255; brightness >= 0; brightness--) {
leds[0] = color.scale8(brightness);
FastLED.show();
delay(breathSpeed);
}
}
// Efecto de "arcoíris" lento
void rainbowEffect(int speedDelay = 50) {
static uint8_t hue = 0;
for (int i = 0; i < 256; i++) {
leds[0] = CHSV(hue++, 255, 255);
FastLED.show();
delay(speedDelay);
}
}
// Efecto de "centelleo" aleatorio
void twinkleEffect(CRGB baseColor, int twinkleCount = 5, int delayMs = 100) {
for (int i = 0; i < twinkleCount; i++) {
leds[0] = CRGB::White;
FastLED.show();
delay(random(10, delayMs));
leds[0] = baseColor;
FastLED.show();
delay(random(50, delayMs * 2));
}
}
void loop() {
Serial.println("Efecto de respiración rojo...");
breathingEffect(CRGB::Red, 15);
delay(1000);
Serial.println("Efecto de respiración verde...");
breathingEffect(CRGB::Green, 10);
delay(1000);
Serial.println("Efecto de respiración azul...");
breathingEffect(CRGB::Blue, 20);
delay(1000);
Serial.println("Arcoíris lento...");
rainbowEffect(30);
delay(2000);
Serial.println("Centelleo amarillo...");
twinkleEffect(CRGB::Yellow, 8, 150);
delay(2000);
// Puedes añadir más efectos o combinaciones aquí
// setRGB(random(0, 256), random(0, 256), random(0, 256)); // Cambiar a un color aleatorio
// delay(500);
}