#include <FastLED.h>
#define LED_PIN 6
#define NUM_LEDS 144
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define SOUND_SENSOR_PIN A2
#define POT_PIN A1
// Variables for sound analysis
int soundLevel = 0;
int maxSoundLevel = 0;
int minSoundLevel = 1023;
int soundThreshold = 50; // Threshold for sound activation
int smoothedSoundLevel = 0; // Smoothed sound level
const float smoothingFactor = 0.1; // Smoothing factor for low-pass filter
// Variables for LED effects
uint8_t hue = 0;
uint8_t saturation = 255;
uint8_t value = 255;
// Function prototypes
void setupLEDs();
void readSoundSensor();
void readPotentiometer();
void updateLEDs();
void spectrumEqualizerEffect();
void waveEffect();
void colorTransitionEffect();
void setup() {
Serial.begin(9600);
setupLEDs();
}
void loop() {
readSoundSensor();
readPotentiometer();
updateLEDs();
delay(20); // Adjust the speed of the loop
}
void setupLEDs() {
delay(3000); // 3 second delay for recovery
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(10); // Initial low brightness
}
void readSoundSensor() {
soundLevel = analogRead(SOUND_SENSOR_PIN);
smoothedSoundLevel = (smoothingFactor * soundLevel) + ((1 - smoothingFactor) * smoothedSoundLevel); // Low-pass filter
if (soundLevel > maxSoundLevel) {
maxSoundLevel = soundLevel;
}
if (soundLevel < minSoundLevel) {
minSoundLevel = soundLevel;
}
Serial.print("Sound Level: ");
Serial.println(smoothedSoundLevel);
}
void readPotentiometer() {
int potValue = analogRead(POT_PIN);
int brightness = map(potValue, 0, 1023, 1, 255); // Map potentiometer value to brightness with lower minimum
FastLED.setBrightness(brightness);
}
void updateLEDs() {
spectrumEqualizerEffect();
waveEffect();
colorTransitionEffect();
FastLED.show();
}
void spectrumEqualizerEffect() {
int center = NUM_LEDS / 2;
int barHeight = map(smoothedSoundLevel, minSoundLevel, maxSoundLevel, 0, center);
for (int i = 0; i < NUM_LEDS; i++) {
if (i >= center - barHeight && i <= center + barHeight) {
leds[i] = CHSV(hue, saturation, value);
} else {
leds[i] = CHSV(hue, saturation, 10); // LEDs remain on with low brightness
}
}
hue += 1; // Slower hue change for smoother effect
}
void waveEffect() {
static uint8_t wavePosition = 0;
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] += CHSV(hue + (i * 10), saturation, value * sin8(wavePosition + (i * 10)) / 255);
}
wavePosition += 1;
}
void colorTransitionEffect() {
static uint8_t startHue = 0;
static uint8_t endHue = 128;
static uint8_t transitionStep = 0;
static const uint8_t transitionSteps = 200; // Increase steps for smoother transition
for (int i = 0; i < NUM_LEDS; i++) {
uint8_t currentHue = startHue + ((endHue - startHue) * transitionStep / transitionSteps);
leds[i] = blend(leds[i], CHSV(currentHue, saturation, value), 128); // Blend with existing color
}
transitionStep++;
if (transitionStep >= transitionSteps) {
transitionStep = 0;
startHue = endHue;
endHue = random(0, 255);
}
}