/* ESP32 LED Strip animation (back and forth with gradient)
Displays a light going back and forth with a gradient tail
*/
#include <FastLED.h>
#define DATA_PIN 26
#define CLOCK_PIN 13
#define ledCount 20
CRGB leds[ledCount];
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, ledCount); // GRB ordering is assumed
}
int direction = 1; // 1 for left, -1 for right
int currentLED = 0;
void loop() {
delay(10); // this speeds up the simulation
// Clear all LEDs
for (int j = 0; j < ledCount; j++) {
leds[j] = CRGB::Black;
}
leds[currentLED] = CHSV(160, 255, 255);
// Add gradient tail
if (currentLED - direction <= ledCount-1 && currentLED - direction >= 0) {
leds[currentLED - direction] = CHSV(160, 255, 200);
}
if (currentLED - (direction * 2) <= ledCount-1 && currentLED - (direction * 2) >= 0) {
leds[currentLED - (direction * 2)] = CHSV(160, 255, 155);
}
if (currentLED - (direction * 3) <= ledCount-1 && currentLED - (direction * 3) >= 0) {
leds[currentLED - (direction * 3)] = CHSV(160, 255, 100);
}
if (currentLED - (direction * 4) <= ledCount-1 && currentLED - (direction * 4) >= 0) {
leds[currentLED - (direction * 4)] = CHSV(160, 255, 55);
}
if (currentLED - (direction * 5) <= ledCount-1 && currentLED - (direction * 5) >= 0) {
leds[currentLED - (direction * 5)] = CHSV(160, 255, 10);
}
// Switch direction
if (currentLED + direction > ledCount-1 || currentLED + direction < 0) {
direction = direction * -1;
}
currentLED = currentLED + direction;
FastLED.show();
delay(60);
}