Waveshare Electronics
Published © GPL3+

ESP32-S3 Matrix: Waterfall and Effect Light with FastLED

Demonstrate lightning effects of waterfall light and effect light by combining Waveshare ESP32-S3 Matrix with FastLED library.

BeginnerProtip1 hour1,673
ESP32-S3 Matrix: Waterfall and Effect Light with FastLED

Story

Read more

Code

BlinkRGB

Arduino
Waterfall light with FlastLED library
#include <FastLED.h>

#define LED_PIN     14
#define NUM_LEDS    64
#define BRIGHTNESS  10
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];

const uint16_t bands = 8; // Number of frequency bands
uint8_t bar_values[bands]; // Store the height of each band
CRGB colors[bands]; // Store the color of each band

void setup() {
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
}

void loop() {
  static uint8_t lastBarValues[bands]; // Store the last height of the bands
  fill_solid(leds, NUM_LEDS, CRGB::Black); // Turn off all LEDs first

  // Generate new band heights and colors
  for (int i = 0; i < bands; i++) {
    bar_values[i] = random(0, 9); // Generate a random value between 0 and 8
    colors[i] = CHSV(random8(), 255, 255); // Generate a random color
  }

  // Draw each band
  for (int i = 0; i < bands; i++) {
    for (int j = 0; j < bar_values[i]; j++) {
      leds[i * 8 + j] = colors[i]; // Light up each bar of the band
    }
  }

  // Draw the descending effect
  for (int i = 0; i < bands; i++) {
    if (bar_values[i] < lastBarValues[i]) {
      for (int j = bar_values[i]; j < lastBarValues[i]; j++) {
        leds[i * 8 + j] = CRGB::Black; // Turn off each bar of the band
      }
      lastBarValues[i] = bar_values[i]; // Update the last band height
    } else {
      lastBarValues[i] = bar_values[i]; // If there is no change, update directly
    }
  }

  FastLED.show(); // Update the LED display
  delay(50); // Delay for 50 milliseconds

  // Update the last band height
  for (int i = 0; i < bands; i++) {
    lastBarValues[i] = bar_values[i];
  }
}

LED Spectrum

Arduino
Effect Light with FastLED library
#include <FastLED.h>

#define LED_PIN     14
#define NUM_LEDS    64
#define BRIGHTNESS  100
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];

const uint8_t matrixWidth = 8;
const uint8_t matrixHeight = 8;
int radius = matrixWidth / 2; // Initial radius set to maximum value
bool expanding = false; // Initial state is contracting

CRGB historyColor[matrixHeight][matrixWidth];

void setup() {
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
}

void loop() {
  fill_solid(leds, NUM_LEDS, CRGB::Black);

  // Update radius based on expanding state
  if (!expanding) {
    radius--;
    if (radius < 1) { // When contracted to 1, prepare to expand
      radius = 1;
      expanding = true; // Switch to expanding state
    }
  } else {
    radius++;
    if (radius > matrixWidth / 2) {
      radius = matrixWidth / 2;
      expanding = false; // Switch to contracting state
    }
  }

  uint8_t hue = 0;
  for (int i = 0; i < matrixHeight; i++) {
    for (int j = 0; j < matrixWidth; j++) {
      int dx = abs(j - matrixWidth / 2);
      int dy = abs(i - matrixHeight / 2);
      int dist = max(dx, dy); // Manhattan distance

      // Light up LEDs based on distance and expanding state
      if ((expanding && dist == radius) || (!expanding && dist <= radius)) {
        hue = (millis() / 10 + i * matrixWidth + j) % 360; // Color changes over time
        CRGB color = CHSV(hue, 255, BRIGHTNESS);
        if (expanding) {
          leds[matrixWidth * i + j] = color;
          historyColor[i][j] = color;
        } else {
          leds[matrixWidth * i + j] = historyColor[i][j];
        }
      }
    }
  }

  FastLED.show();
  delay(100);
}

Credits

Waveshare Electronics
1 project • 3 followers
Contact
Thanks to Lorraine.

Comments

Please log in or sign up to comment.