/*
ESP32-S3 + 16x16 WS2812B RGB MATRIX
13 COLORFUL ARTISTIC ANIMATIONS
Created by: Arvind Patil
AI Centre Nandurbar
FastLED Library Required
CONNECTIONS:
WS2812B DIN -> GPIO 4
WS2812B 5V -> External 5V supply
WS2812B GND -> Common GND with ESP32-S3
BOOT BUTTON -> GPIO 0
Matrix: 16x16 = 256 LEDs
Mapping: Serpentine
*/
#include <FastLED.h>
#include <math.h>
#include <string.h>
#define LED_PIN 5
#define MATRIX_W 16
#define MATRIX_H 16
#define NUM_LEDS 256
#define BOOT_PIN 0
#define NUM_MODES 13
#define AUTO_MS 6000
CRGB leds[NUM_LEDS];
uint8_t modeIndex = 0;
unsigned long lastSwitch = 0;
bool autoAdvance = true;
bool lastBootState = HIGH;
float t = 0;
// =====================================================
// MATRIX MAPPING
// =====================================================
uint16_t XY(uint8_t x, uint8_t y) {
if (y % 2 == 0) {
return y * MATRIX_W + x;
} else {
return y * MATRIX_W + (MATRIX_W - 1 - x);
}
}
void setPix(uint8_t x, uint8_t y, CRGB c) {
if (x < MATRIX_W && y < MATRIX_H) {
leds[XY(x, y)] = c;
}
}
// =====================================================
// 1. RAINBOW WAVE
// =====================================================
void rainbowWave(float time) {
for (int y = 0; y < MATRIX_H; y++) {
for (int x = 0; x < MATRIX_W; x++) {
uint8_t hue = (uint8_t)(x * 16 + y * 16 + time * 80);
setPix(x, y, CHSV(hue, 255, 255));
}
}
}
// =====================================================
// 2. PLASMA
// =====================================================
void plasmaFx(float time) {
for (int y = 0; y < MATRIX_H; y++) {
for (int x = 0; x < MATRIX_W; x++) {
float v =
sin(x * 0.5 + time) +
sin(y * 0.5 + time * 1.3) +
sin((x + y) * 0.4 + time * 0.7) +
sin(sqrt((float)(x*x + y*y)) * 0.5 - time);
uint8_t hue = (uint8_t)(v * 32 + 128);
uint8_t val = (uint8_t)constrain(
150 + sin(v) * 100, 0, 255
);
setPix(x, y, CHSV(hue, 255, val));
}
}
}
// =====================================================
// 3. FIRE
// =====================================================
float fireBuf[MATRIX_H + 2][MATRIX_W];
void resetFire() {
memset(fireBuf, 0, sizeof(fireBuf));
}
void fireFx(float time) {
for (int x = 0; x < MATRIX_W; x++) {
fireBuf[MATRIX_H][x] = random(0, 256);
fireBuf[MATRIX_H + 1][x] = random(0, 256);
}
for (int y = 0; y < MATRIX_H; y++) {
for (int x = 0; x < MATRIX_W; x++) {
float below = fireBuf[y + 1][x];
float belowL =
fireBuf[y + 1][max(0, x - 1)];
float belowR =
fireBuf[y + 1][min(MATRIX_W - 1, x + 1)];
float below2 = fireBuf[y + 2][x];
fireBuf[y][x] =
((below + belowL + belowR + below2) / 4.0) * 0.85;
}
}
for (int y = 0; y < MATRIX_H; y++) {
for (int x = 0; x < MATRIX_W; x++) {
float v = fireBuf[y][x];
if (v < 8) {
setPix(x, y, CRGB::Black);
continue;
}
uint8_t hue = (uint8_t)constrain(
(v / 255.0) * 50, 0, 50
);
uint8_t val = (uint8_t)constrain(v * 1.4, 0, 255);
setPix(x, y, CHSV(hue, 255, val));
}
}
}
// =====================================================
// 4. MATRIX RAIN
// =====================================================
struct RainCol {
float pos;
float speed;
};
RainCol rainCols[MATRIX_W];
void resetRain() {
for (int x = 0; x < MATRIX_W; x++) {
rainCols[x].pos = random(-MATRIX_H, MATRIX_H);
rainCols[x].speed = 0.3 + random(0, 50) / 100.0;
}
}
void matrixRainFx(float time) {
for (int y = 0; y < MATRIX_H; y++) {
for (int x = 0; x < MATRIX_W; x++) {
leds[XY(x, y)].nscale8(180);
}
}
for (int x = 0; x < MATRIX_W; x++) {
rainCols[x].pos += rainCols[x].speed;
if (rainCols[x].pos > MATRIX_H + 6) {
rainCols[x].pos = -random(0, 10);
}
int y = (int)rainCols[x].pos;
if (y >= 0 && y < MATRIX_H) {
setPix(x, y, CRGB(40, 255, 90));
}
int yTail = y - 1;
if (yTail >= 0 && yTail < MATRIX_H) {
setPix(x, yTail, CRGB(10, 120, 40));
}
}
}
// =====================================================
// 5. RIPPLE
// =====================================================
void rippleFx(float time) {
float cx = MATRIX_W / 2.0 - 0.5;
float cy = MATRIX_H / 2.0 - 0.5;
for (int y = 0; y < MATRIX_H; y++) {
for (int x = 0; x < MATRIX_W; x++) {
float dx = x - cx;
float dy = y - cy;
float d = sqrt(dx * dx + dy * dy);
float v = sin(d * 1.3 - time * 3);
uint8_t val = (uint8_t)constrain(
max(0.0f, (v + 1) / 2) * 255, 0, 255
);
uint8_t hue = (uint8_t)(130 + d * 5);
setPix(x, y, CHSV(hue, 230, val));
}
}
}
// =====================================================
// 6. SPARKLE TWINKLE
// =====================================================
float sparkles[MATRIX_H][MATRIX_W];
void resetSparkle() {
memset(sparkles, 0, sizeof(sparkles));
}
void sparkleTwinkleFx(float time) {
for (int y = 0; y < MATRIX_H; y++) {
for (int x = 0; x < MATRIX_W; x++) {
sparkles[y][x] *= 0.88;
if (random(0, 100) < 2) {
sparkles[y][x] = 255;
}
uint8_t hue = (uint8_t)(x * 10 + y * 10);
uint8_t val = (uint8_t)constrain(
sparkles[y][x], 0, 255
);
setPix(x, y, CHSV(hue, 100, val));
}
}
}
// =====================================================
// 7. COLOR CHASE - CORRECTED
// =====================================================
void colorChaseFx(float time) {
for (int y = 0; y < MATRIX_H; y++) {
for (int x = 0; x < MATRIX_W; x++) {
int idx =
(y % 2 == 0 ? x : MATRIX_W - 1 - x)
+ y * MATRIX_W;
float pos = fmod(
idx - time * 40,
(float)(MATRIX_W * MATRIX_H)
);
if (pos < 0) {
pos += MATRIX_W * MATRIX_H;
}
float dist = min(
pos,
MATRIX_W * MATRIX_H - pos
);
uint8_t val = (uint8_t)constrain(
255 - dist * 20, 0, 255
);
uint8_t hue = (uint8_t)(time * 42);
if (val > 4) {
setPix(x, y, CHSV(hue, 255, val));
} else {
setPix(x, y, CRGB(0, 0, 0));
}
}
}
}
// =====================================================
// 8. BREATHING GRADIENT
// =====================================================
void breathingGradientFx(float time) {
uint8_t bri = (uint8_t)(
70 + (sin(time * 1.1) * 0.5 + 0.5) * 180
);
for (int y = 0; y < MATRIX_H; y++) {
for (int x = 0; x < MATRIX_W; x++) {
uint8_t hue = (uint8_t)(y * 14 + time * 20);
setPix(x, y, CHSV(hue, 230, bri));
}
}
}
// =====================================================
// 9. FIREWORKS
// =====================================================
struct Firework {
float x, y, r;
uint8_t hue;
bool alive;
};
#define MAX_FW 6
Firework fireworks[MAX_FW];
void resetFireworks() {
for (int i = 0; i < MAX_FW; i++) {
fireworks[i].alive = false;
}
FastLED.clear();
}
void fireworksFx(float time) {
for (int y = 0; y < MATRIX_H; y++) {
for (int x = 0; x < MATRIX_W; x++) {
leds[XY(x, y)].nscale8(200);
}
}
if (random(0, 100) < 6) {
for (int i = 0; i < MAX_FW; i++) {
if (!fireworks[i].alive) {
fireworks[i] = {
(float)random(0, MATRIX_W),
(float)random(0, MATRIX_H / 2),
0,
(uint8_t)random(0, 255),
true
};
break;
}
}
}
for (int i = 0; i < MAX_FW; i++) {
if (!fireworks[i].alive) continue;
fireworks[i].r += 0.5;
int n = 12;
for (int p = 0; p < n; p++) {
float a = (float)p / n * TWO_PI;
int px = round(
fireworks[i].x + cos(a) * fireworks[i].r
);
int py = round(
fireworks[i].y + sin(a) * fireworks[i].r
);
if (px >= 0 && px < MATRIX_W &&
py >= 0 && py < MATRIX_H) {
uint8_t val = (uint8_t)constrain(
255 - fireworks[i].r * 45, 0, 255
);
setPix(
px, py,
CHSV(fireworks[i].hue, 230, val)
);
}
}
if (fireworks[i].r > 6) {
fireworks[i].alive = false;
}
}
}
// =====================================================
// 10. STARFIELD
// =====================================================
void resetStarfield() {
FastLED.clear();
}
void starfieldFx(float time) {
for (int y = 0; y < MATRIX_H; y++) {
for (int x = 0; x < MATRIX_W; x++) {
leds[XY(x, y)].nscale8(220);
}
}
for (int i = 0; i < 3; i++) {
setPix(
random(0, MATRIX_W),
random(0, MATRIX_H),
CRGB::White
);
}
}
// =====================================================
// 11. SPIRAL BLOOM
// =====================================================
void spiralBloomFx(float time) {
float cx = MATRIX_W / 2.0 - 0.5;
float cy = MATRIX_H / 2.0 - 0.5;
for (int y = 0; y < MATRIX_H; y++) {
for (int x = 0; x < MATRIX_W; x++) {
float dx = x - cx;
float dy = y - cy;
float ang = atan2(dy, dx);
float dist = sqrt(dx * dx + dy * dy);
float v = sin(
ang * 3 + dist * 1.2 - time * 3
);
uint8_t val = (uint8_t)constrain(
max(0.0f, (v + 1) / 2) * 255, 0, 255
);
uint8_t hue = (uint8_t)(dist * 14 + time * 30);
setPix(x, y, CHSV(hue, 255, val));
}
}
}
// =====================================================
// 12. CHECKERBOARD PULSE - CORRECTED
// =====================================================
void checkerboardPulseFx(float time) {
uint8_t bri = (uint8_t)(
100 + (sin(time * 2) * 0.5 + 0.5) * 155
);
uint8_t hue = (uint8_t)(time * 35);
for (int y = 0; y < MATRIX_H; y++) {
for (int x = 0; x < MATRIX_W; x++) {
if ((x + y) % 2 == 0) {
setPix(x, y, CHSV(hue, 255, bri));
} else {
setPix(x, y, CRGB(0, 0, 0));
}
}
}
}
// =====================================================
// 13. CONWAY'S GAME OF LIFE - CORRECTED
// =====================================================
bool life[MATRIX_H][MATRIX_W];
int lifeTimer = 0;
void resetLife() {
lifeTimer = 0;
for (int y = 0; y < MATRIX_H; y++) {
for (int x = 0; x < MATRIX_W; x++) {
life[y][x] = random(0, 100) > 75;
}
}
}
void gameOfLifeFx(float time) {
lifeTimer++;
if (lifeTimer > 5) {
lifeTimer = 0;
bool next[MATRIX_H][MATRIX_W];
for (int y = 0; y < MATRIX_H; y++) {
for (int x = 0; x < MATRIX_W; x++) {
int n = 0;
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
if (dx == 0 && dy == 0) continue;
int ny = (y + dy + MATRIX_H) % MATRIX_H;
int nx = (x + dx + MATRIX_W) % MATRIX_W;
n += life[ny][nx] ? 1 : 0;
}
}
next[y][x] = life[y][x]
? (n == 2 || n == 3)
: (n == 3);
}
}
memcpy(life, next, sizeof(life));
if (random(0, 100) < 5) {
life[random(0, MATRIX_H)]
[random(0, MATRIX_W)] = true;
}
}
for (int y = 0; y < MATRIX_H; y++) {
for (int x = 0; x < MATRIX_W; x++) {
if (life[y][x]) {
setPix(x, y, CHSV(96, 230, 200));
} else {
setPix(x, y, CRGB(0, 0, 0));
}
}
}
}
// =====================================================
// MODE TABLE
// =====================================================
typedef void (*ModeFn)(float);
typedef void (*ResetFn)();
void noReset() {}
struct Mode {
const char* name;
ModeFn fn;
ResetFn reset;
};
Mode modes[NUM_MODES] = {
{"Rainbow Wave", rainbowWave, noReset},
{"Plasma", plasmaFx, noReset},
{"Fire", fireFx, resetFire},
{"Matrix Rain", matrixRainFx, resetRain},
{"Ripple", rippleFx, noReset},
{"Sparkle Twinkle", sparkleTwinkleFx, resetSparkle},
{"Color Chase", colorChaseFx, noReset},
{"Breathing Gradient", breathingGradientFx, noReset},
{"Fireworks", fireworksFx, resetFireworks},
{"Starfield", starfieldFx, resetStarfield},
{"Spiral Bloom", spiralBloomFx, noReset},
{"Checkerboard Pulse", checkerboardPulseFx, noReset},
{"Conway's Life", gameOfLifeFx, resetLife}
};
// =====================================================
// CHANGE ANIMATION
// =====================================================
void setModeIdx(int i) {
modeIndex = ((i % NUM_MODES) + NUM_MODES) % NUM_MODES;
FastLED.clear();
modes[modeIndex].reset();
t = 0;
lastSwitch = millis();
Serial.print("Animation: ");
Serial.print(modeIndex + 1);
Serial.print(" / ");
Serial.println(modes[modeIndex].name);
}
// =====================================================
// SETUP
// =====================================================
void setup() {
Serial.begin(115200);
pinMode(BOOT_PIN, INPUT_PULLUP);
randomSeed(micros());
FastLED.addLeds<WS2812B, LED_PIN, GRB>(
leds, NUM_LEDS
);
FastLED.setBrightness(80);
FastLED.setDither(0);
FastLED.clear();
FastLED.show();
setModeIdx(0);
Serial.println();
Serial.println("================================");
Serial.println(" AI CENTRE NANDURBAR");
Serial.println(" ARVIND PATIL");
Serial.println(" ESP32-S3 RGB MATRIX");
Serial.println(" 16 x 16 WS2812B");
Serial.println(" 13 ARTISTIC ANIMATIONS");
Serial.println("================================");
}
// =====================================================
// MAIN LOOP
// =====================================================
void loop() {
bool bootState = digitalRead(BOOT_PIN);
// Manual next animation
if (bootState == LOW && lastBootState == HIGH) {
autoAdvance = false;
setModeIdx(modeIndex + 1);
delay(30);
}
lastBootState = bootState;
// Animation speed
t += 0.02;
// Run current animation
modes[modeIndex].fn(t);
// Display LEDs
FastLED.show();
// Automatic animation switching
if (autoAdvance &&
millis() - lastSwitch >= AUTO_MS) {
setModeIdx(modeIndex + 1);
}
delay(20);
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4