#include <FastLED.h>
#define LED_PIN 6
#define NUM_LEDS 50
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define SOUND_SENSOR_PIN A0
#define POT_PIN A1
// Variables for sound analysis
int soundLevel = 0;
int maxSoundLevel = 0;
int minSoundLevel = 1023;
int soundThreshold = 50; // Threshold for sound activation
// Variables for LED effects
uint8_t hue = 0;
uint8_t saturation = 255;
uint8_t value = 255;
int groupSize = 12; // Number of LEDs in each group
// Function prototypes
void setupLEDs();
void readSoundSensor();
void readPotentiometer();
void updateLEDs();
void spectrumEqualizerEffect();
void transitionEffect();
void magnetEffect();
void symphonyEffect();
void smoothTransition(CRGB startColor, CRGB endColor, int steps);
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);
if (soundLevel > maxSoundLevel) {
maxSoundLevel = soundLevel;
}
if (soundLevel < minSoundLevel) {
minSoundLevel = soundLevel;
}
Serial.print("Sound Level: ");
Serial.println(soundLevel);
}
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() {
if (soundLevel > soundThreshold) {
spectrumEqualizerEffect();
} else {
transitionEffect();
}
magnetEffect();
symphonyEffect();
FastLED.show();
}
void spectrumEqualizerEffect() {
int barHeight = map(soundLevel, minSoundLevel, maxSoundLevel, 0, NUM_LEDS / (2 * groupSize));
int center = NUM_LEDS / 2;
int activeLEDs = NUM_LEDS * 0.7 / 2; // 70% of LEDs, divided by 2 for each side
for (int i = 0; i < NUM_LEDS; i++) {
if (i >= center - activeLEDs && i <= center + activeLEDs) {
if (abs(center - i) / groupSize < barHeight) {
leds[i] = CHSV(hue, saturation, value);
} else {
leds[i] = CRGB::Black;
}
} else {
leds[i] = CRGB::Black;
}
}
hue += 1; // Slower hue change for smoother effect
}
void transitionEffect() {
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] = CHSV(currentHue, saturation, value);
}
transitionStep++;
if (transitionStep >= transitionSteps) {
transitionStep = 0;
startHue = endHue;
endHue = random(0, 255);
}
}
void magnetEffect() {
static int magnetPos = 0;
static int direction = 1;
int center = NUM_LEDS / 2;
int magnetRange = NUM_LEDS * 0.3 / 2; // 30% of LEDs, divided by 2 for each side
for (int i = 0; i < magnetRange; i++) {
int pos1 = center - magnetRange + i + magnetPos;
int pos2 = center + magnetRange - i - magnetPos;
if (pos1 >= 0 && pos1 < NUM_LEDS) {
leds[pos1] = CHSV(hue, saturation, value);
}
if (pos2 >= 0 && pos2 < NUM_LEDS) {
leds[pos2] = CHSV(hue, saturation, value);
}
}
magnetPos += direction;
if (magnetPos >= magnetRange || magnetPos <= 0) {
direction = -direction;
}
}
void symphonyEffect() {
static int symphonyPos = 0;
static int symphonyDirection = 1;
int center = NUM_LEDS / 2;
int symphonyRange = NUM_LEDS * 0.3 / 2; // 30% of LEDs, divided by 2 for each side
for (int i = 0; i < symphonyRange; i++) {
int pos1 = center - symphonyRange + i + symphonyPos;
int pos2 = center + symphonyRange - i - symphonyPos;
if (pos1 >= 0 && pos1 < NUM_LEDS) {
leds[pos1] = CHSV(hue + 128, saturation, value);
}
if (pos2 >= 0 && pos2 < NUM_LEDS) {
leds[pos2] = CHSV(hue + 128, saturation, value);
}
}
symphonyPos += symphonyDirection;
if (symphonyPos >= symphonyRange || symphonyPos <= 0) {
symphonyDirection = -symphonyDirection;
}
}
void smoothTransition(CRGB startColor, CRGB endColor, int steps) {
for (int step = 0; step < steps; step++) {
float ratio = (float)step / (float)steps;
CRGB currentColor = blend(startColor, endColor, ratio * 255);
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = currentColor;
}
FastLED.show();
delay(10); // Adjust delay for smoother transition
}
}