cookbook/basic-patterns/animations.md
Difficulty Level: ⭐ Beginner Time to Complete: 20-25 minutes Prerequisites: Solid Colors, Timing Concepts
You'll Learn:
Learn how to create smooth, dynamic effects by animating brightness and color over time. The fade in/out effect is one of the most fundamental animations in LED programming.
This classic effect smoothly fades LEDs from off to full brightness and back again.
void fadeInOut() {
static uint8_t brightness = 0;
static int8_t direction = 1;
brightness += direction;
if (brightness == 0 || brightness == 255) {
direction = -direction;
}
fill_solid(leds, NUM_LEDS, CRGB::Purple);
FastLED.setBrightness(brightness);
FastLED.show();
}
brightness and direction persist between function callsdirection is either 1 (increasing) or -1 (decreasing)setBrightness() affects all LEDs globallyStatic variables retain their values between function calls:
static uint8_t brightness = 0; // Initialized once, persists
Without static, the variable would reset to 0 every time the function is called.
// Global brightness (affects all LEDs)
FastLED.setBrightness(brightness);
// Per-LED brightness (using HSV)
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(160, 255, brightness);
}
Control animation speed by calling the function at different rates:
void loop() {
fadeInOut();
delay(20); // Faster animation
// Or use timing macros
EVERY_N_MILLISECONDS(20) {
fadeInOut();
}
}
#include <FastLED.h>
#define LED_PIN 5
#define NUM_LEDS 60
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(50);
}
void fadeInOut() {
static uint8_t brightness = 0;
static int8_t direction = 1;
brightness += direction;
if (brightness == 0 || brightness == 255) {
direction = -direction;
}
fill_solid(leds, NUM_LEDS, CRGB::Purple);
FastLED.setBrightness(brightness);
FastLED.show();
}
void loop() {
fadeInOut();
delay(10); // Control speed here
}
void fadeInOutColor(CRGB color) {
static uint8_t brightness = 0;
static int8_t direction = 1;
brightness += direction;
if (brightness == 0 || brightness == 255) {
direction = -direction;
}
fill_solid(leds, NUM_LEDS, color);
FastLED.setBrightness(brightness);
FastLED.show();
}
// Usage
void loop() {
fadeInOutColor(CRGB::Red);
delay(10);
}
For more control, fade individual LEDs:
void fadeInOutPerLED() {
static uint8_t brightness = 0;
static int8_t direction = 1;
brightness += direction;
if (brightness == 0 || brightness == 255) {
direction = -direction;
}
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(160, 255, brightness);
}
FastLED.show();
}
Adjust fade speed dynamically:
void fadeInOutSpeed(uint8_t speed) {
static uint8_t brightness = 0;
static int8_t direction = speed;
brightness += direction;
if (brightness == 0 || brightness >= 255) {
direction = -direction;
}
fill_solid(leds, NUM_LEDS, CRGB::Blue);
FastLED.setBrightness(brightness);
FastLED.show();
}
delay() or timing macros to control animation speedNow that you understand basic animations, explore Chases and Scanners to learn about moving patterns with trail effects.