#include <FastLED.h>
#define LED_PIN 2
#define NUM_LEDS 60
#define BRIGHTNESS 255
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
// CHANGE THIS VALUE TO CONTROL SPEED
// Higher value = Faster animation
#define UPDATES_PER_SECOND 150
CRGBPalette16 currentPalette;
TBlendType currentBlending;
int8_t animationDirection = 1;
unsigned long lastDirectionChangeTime = 0;
const unsigned long directionChangeInterval = 5000; // Animation reverses every 5 seconds
void setup() {
delay(800); // Power-up delay
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
currentPalette = RainbowStripeColors_p;
currentBlending = NOBLEND;
lastDirectionChangeTime = millis();
}
void loop() {
unsigned long currentTime = millis();
// Reverse the animation direction every few seconds
if (currentTime - lastDirectionChangeTime >= directionChangeInterval) {
animationDirection *= -1;
lastDirectionChangeTime = currentTime;
}
// This static variable keeps track of the animation's position
static uint8_t startIndex = 0;
startIndex = startIndex + animationDirection; // Move the animation forwards or backwards
// Fill the LED strip with colors from the palette
FillLEDsFromPaletteColors(startIndex);
FastLED.show();
// The delay is now shorter because UPDATES_PER_SECOND is higher
FastLED.delay(1000 / UPDATES_PER_SECOND);
}
// This function fills the strip with colors from the palette.
void FillLEDsFromPaletteColors(uint8_t colorIndex) {
uint8_t brightness = 255;
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = ColorFromPalette(currentPalette, colorIndex, brightness, currentBlending);
// This value controls the width of the color bands (set to 5 for ~3 pixel trails)
colorIndex += 5;
}
}