Sound Reactive LED Project - Help needed

I'm getting very close to the end of my project, with just one or two more issues that I haven't been able to work out on my own.

What I am struggling to work out is that until the index has an array, that nothing will happen. IE the RGBColor value will be 0,0,0

Secondly, I cannot work out how to have the Brightness of the LEDs constantly update depending on the value of the sound envelope, rather than waiting until a color change is called.

Any help would be appreciated.

#include "FastLED.h"

#define NUM_LEDS 120
#define LED_PIN 2
#define COLOR_ORDER RGB
#define LED_TYPE WS2811
#define delayLEDS 0

CRGB leds[NUM_LEDS];

int numReadings = 200;
int readings[200];
int index = 0;

int sensorMax = 0;
int sensorMin = 0;

void setup() {
  Serial.begin(9600);
  FastLED.addLeds <WS2811, LED_PIN, RGB > (leds, NUM_LEDS).setCorrection(TypicalLEDStrip);

  sensorMax = analogRead(A0);
  sensorMin = sensorMax;
}

void RGBColor(int Rcolor, int Gcolor, int Bcolor) {
  int envelopevalue = analogRead(A1);
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i].r = Rcolor;
    leds[i].g = Gcolor;
    leds[i].b = Bcolor;
  }

  if (envelopevalue > 10) {
    envelopevalue = map(envelopevalue, 0, 500, 0, 255);
  } else envelopevalue = 0;

  FastLED.setBrightness(envelopevalue);
  FastLED.show();

}

void loop() {
  int value = analogRead(A0);
  int envolopevalue = analogRead(A1);
  Serial.println(envolopevalue);
 

  readings[index] = value;
  index++;
  if (index >= numReadings) index = 0;

  if (value > sensorMax) sensorMax = value;
  if (value < sensorMin) sensorMin = value;

  float total = 0;
  for (int i = 0; i < numReadings; i++) total += readings[i];
  float average = total / numReadings;

  if (index >= 199) {
    if (value >= 500) {
      sensorMax = value;
    }
  }

  float threashold = (sensorMax - average) / 7;

  if (value >= 525); {
    {
      if (value > average + threashold * 1 && value <= average + threashold * 2) {
        //Serial.println("Yellow");
        RGBColor(255, 255, 0);
      } else if (value > average + threashold * 2 && value <= average + threashold * 3) {
        //Serial.println("Green");
        RGBColor(255, 0, 0);
      } else if (value > average + threashold * 3 && value <= average + threashold * 4) {
        //Serial.println("Blue");
        RGBColor(0, 0, 255);
      } else if (value > average + threashold * 4 && value <= average + threashold * 5) {
        //Serial.println("Violet");
        RGBColor(238, 130, 238);
      } else if (value > average + threashold * 5 && value <= average + threashold * 6) {
        //Serial.println("Red");
        RGBColor(0, 255, 0);
      } else if (value > average + threashold * 6 && value <= average + threashold * 7) {
        //Serial.println("Orange");
        RGBColor(50, 255, 0);
      } else if (value > average + threashold * 7) {
        //Serial.println("White");
        RGBColor(255, 255, 255);
      }

      // gitter reduction
      delay(50);
    }

  }

}


what is delayLEDS ?
int numReadings = 200; almost all INT can be changed to BYTE

Just some excess code I haven't cleaned up.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.