#include <FastLED.h>
// Matrix and Pin Configuration
#define DATA_PIN 2 // ESP8266 Pin D4 is GPIO 2
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define MATRIX_WIDTH 25
#define MATRIX_HEIGHT 8
#define NUM_LEDS (MATRIX_WIDTH * MATRIX_HEIGHT)
// Global Settings
#define BRIGHTNESS 196 // Set brightness 0-255. Start low to save power!
#define FRAMES_PER_SECOND 60 // Animation speed
CRGB leds[NUM_LEDS];
uint16_t timeValue = 0; // A variable that increments to animate the effect
// This function maps an X,Y coordinate to the correct index in the leds[] array.
// It assumes a "serpentine" or "zigzag" layout, which is common for matrices.
// Adjust this if your matrix is wired differently (e.g., all rows left-to-right).
uint16_t XY(uint8_t x, uint8_t y) {
uint16_t i;
if (y & 0x01) {
// Odd rows run right to left
uint8_t reverseX = (MATRIX_WIDTH - 1) - x;
i = (y * MATRIX_WIDTH) + reverseX;
} else {
// Even rows run left to right
i = (y * MATRIX_WIDTH) + x;
}
return i;
}
void setup() {
Serial.begin(115200); // For debugging
delay(1000); // Power-on delay
// Initialize the FastLED library
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
// Set a global brightness limit. IMPORTANT for power management.
FastLED.setBrightness(BRIGHTNESS);
Serial.println("Setup complete. Starting animation.");
}
void loop() {
// Call the function that draws the rainbow wave pattern
drawRainbowWave();
// Update the LEDs with the new data
FastLED.show();
// Limit the frame rate to control the speed
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
void drawRainbowWave() {
// Increment the time variable for animation. The amount you add controls the speed.
timeValue += 2;
// Loop through every pixel (x,y) of the matrix
for (int x = 0; x < MATRIX_WIDTH; x++) {
for (int y = 0; y < MATRIX_HEIGHT; y++) {
// Calculate a hue value based on the pixel's position and the current time.
// The constants (e.g., 8, 6) control the angle and density of the rainbow bands.
// Experiment with these values to change the look of the wave.
uint8_t hue = (x * 8) + (y * 6) + timeValue;
// Get the matrix index for the current (x,y) coordinate
uint16_t index = XY(x, y);
// Assign the color to the pixel in the leds array using HSV color space.
// CHSV(Hue, Saturation, Value/Brightness)
leds[index] = CHSV(hue, 255, 255);
}
}
}