#include <FastLED.h>
#include <AsyncTimer.h>
#define LED_PIN 2
#define NUM_LEDS 13
#define LEDS_LEFT 3 // Indices 0 to 2
#define LEDS_RIGHT 10 // Indices 3 to 12
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 100
uint8_t maxBrightness = BRIGHTNESS;
uint32_t lastPulseTime = 0;
uint8_t tailLength = 3;
uint8_t currentPulseStep = 0;
bool pulseActive = false;
uint8_t leftPulsePosition;
uint8_t rightPulsePosition;
bool leftPulseStarted = false;
void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2811, LED_PIN, RGB>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
}
void loop() {
uint32_t currentTime = millis();
if (currentTime - lastPulseTime >= 2000) {
lastPulseTime = currentTime;
startPulse();
}
EVERY_N_MILLISECONDS(60) {
// Update the pulse effect
if (pulseActive) {
updatePulse();
} else {
// Clear LEDs when pulse is inactive
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Black;
}
}
FastLED.show();
}
}
void startPulse() {
currentPulseStep = 0;
pulseActive = true;
leftPulseStarted = false;
}
void updatePulse() {
// Clear LEDs before updating
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Black;
}
// Update right pulse position
rightPulsePosition = NUM_LEDS - 1 - currentPulseStep; // From 12 down to 3
// Start left pulse when right pulse reaches the mirrored point
// Calculate mirrored point for left LEDs
if (!leftPulseStarted && rightPulsePosition + 1 == LEDS_LEFT + currentPulseStep) {
Serial.println("Started");
leftPulseStarted = true;
leftPulsePosition = 0; // Start from index 0
}
// Right Pulse Tail
for (int i = 0; i < tailLength; i++) {
int pos = rightPulsePosition + i;
if (pos >= LEDS_LEFT && pos < NUM_LEDS) { // Ensure within right LEDs
Serial.println(pos);
uint8_t brightness = maxBrightness - (i * (maxBrightness / tailLength));
leds[pos] = CRGB(brightness, brightness, brightness);
}
}
// Left Pulse Tail
if (leftPulseStarted) {
leftPulsePosition = currentPulseStep - (LEDS_LEFT - 1 + tailLength); // Calculate left pulse position based on currentPulseStep
for (int i = 0; i < tailLength; i++) {
int pos = leftPulsePosition + i;
if (pos >= 0 && pos < LEDS_LEFT) { // Ensure within left LEDs
uint8_t brightness = maxBrightness - (i * (maxBrightness / tailLength));
leds[pos] = CRGB(brightness, brightness, brightness);
}
}
}
// Check if pulses have met or crossed
if (rightPulsePosition <= LEDS_LEFT - 1) {
pulseActive = false;
} else {
currentPulseStep++;
}
}