#include <FastLED.h>
#define NUM_LEDS 60 // Number of LEDs in the strip
#define DATA_PIN 8 // Data pin for the LED strip
#define WAVE_WIDTH 10 // Width of each wave (number of LEDs lit at once)
#define NUM_WAVES 2 // Number of waves active at once
#define maxBrightness 255
#define minBrightness 0
CRGB leds[NUM_LEDS]; // Array to hold the LED colors
CRGB color = CRGB::Lime;
// Function to generate a sine wave effect
void wavePattern(CRGB color, int speed, int direction) {
static int positions[NUM_WAVES]; // Array to hold positions of multiple waves
// Initialize the wave positions (only runs once)
static bool initialized = false;
if (!initialized) {
for (int i = 0; i < NUM_WAVES; i++) {
positions[i] = i * (NUM_LEDS / NUM_WAVES); // Spread waves across the strip
}
initialized = true;
}
// Clear the LED strip
fill_solid(leds, NUM_LEDS, CRGB::Black);
// Iterate through each wave
for (int w = 0; w < NUM_WAVES; w++) {
// Create a wave for each position in positions array
for (int i = -WAVE_WIDTH / 2; i <= WAVE_WIDTH / 2; i++) {
int ledPos = positions[w] + i;
// Wrap the position around if it goes out of bounds
if (ledPos >= NUM_LEDS) ledPos -= NUM_LEDS;
if (ledPos < 0) ledPos += NUM_LEDS;
// Calculate brightness for an exponential wave
uint8_t brightness;
if (i < 0) {
brightness = maxBrightness * pow(0.5, abs(i) / (WAVE_WIDTH / 2.0));
} else {
brightness = maxBrightness * pow(2, i / (WAVE_WIDTH / 2.0));
}
if (brightness < minBrightness) brightness = minBrightness;
// Set the color with the adjusted brightness
leds[ledPos] = color;
leds[ledPos].fadeLightBy(256 - brightness);
}
// Update the wave position based on the direction
if (direction == 1) {
positions[w]++;
if (positions[w] >= NUM_LEDS) positions[w] = 0;
} else {
positions[w]--;
if (positions[w] < 0) positions[w] = NUM_LEDS - 1;
}
}
// Show the updated LED colors
FastLED.show();
// Control the speed of the wave
delay(speed);
}
void setup() {
// Initialize the LED strip
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.clear();
}
void loop() {
// Call wavePattern with customizable parameters
for (int i = 0; i <= 255; i++) {
color = CRGB(i, 255 - i, 0);
wavePattern(color, 25, 1); // Blue color, speed of 50ms, forward direction
}
for (int i = 255; i >= 0; i--) {
color = CRGB(i, 255 - i, 0);
wavePattern(color, 25, 1); // Blue color, speed of 50ms, forward direction
}
}