Help with Neopixel code

First time poster here. I’m new to Arduino, and programming in general, and I’ve been working on an audio visualizer/color organ using code I’ve found in a couple of places. Everything works fine, but I was hoping someone could tell me how to code a section of the pixels to turn off if the corresponding band of audio is below the filter threshold. Right now, any band without an audio signal has the corresponding pixels lit up green and I’d like for them to all be off. Also, any tips or corrections to my code would be greatly appreciated. Thanks in advance!

#include <Adafruit_NeoPixel.h>

#define PIN 6
int Lchannel   = A0;
int Rchannel   = A1;
int strobePin  =  4;
int resetPin   =  5;
int spectrumValue1[7];
int spectrumValue2[7];
int filter=50;
const int length = 40;
const int fifth =length/5;
Adafruit_NeoPixel strip = Adafruit_NeoPixel (40, PIN, NEO_GRB + NEO_KHZ800);
uint32_t top_array[fifth];
uint32_t topmid_array[fifth];
uint32_t mid_array[fifth];
uint32_t botmid_array[fifth];
uint32_t bot_array[fifth];
void setup(){
  
  Serial.begin(9600);
  
  //initialize neopixels
   for(int i=0; i<fifth;i++)
  {
    top_array[i] = 0;
    topmid_array[i] = 0;
    mid_array[i] = 0;
    botmid_array[i] = 0;
    bot_array[i] = 0;
  }
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'


  //initialize eq
  pinMode(strobePin, OUTPUT);
  pinMode(resetPin, OUTPUT);
  digitalWrite(resetPin, LOW);
  digitalWrite(strobePin, HIGH);
}

void loop(){
  
  digitalWrite(resetPin, HIGH);
  digitalWrite(resetPin, LOW);
  for (int i=0;i<7;i++){
    digitalWrite(strobePin, LOW);
    delay(0);
    spectrumValue1[i]=constrain(map(analogRead(Lchannel), filter , 1023, 0, 255), 0, 255);
    Serial.print(spectrumValue1[i]);
    Serial.print(" ");
    spectrumValue2[i]=constrain(map(analogRead(Rchannel), filter , 1023, 0, 255), 0, 255);
    Serial.print(spectrumValue2[i]);
    Serial.print(" ");
    digitalWrite(strobePin, HIGH);
    
    

  byte hue_top = ((spectrumValue1[4]+spectrumValue1[5])/2);
  uint32_t color_top = Wheel(hue_top);
  
  byte hue_topmid = spectrumValue1[4];
  uint32_t color_topmid = Wheel(hue_topmid);
  
  byte hue_mid = ((spectrumValue1[3]+spectrumValue1[4])/2);
  uint32_t color_mid = Wheel(hue_mid);
  
  byte hue_botmid = ((spectrumValue1[0]+spectrumValue1[1])/2);
  uint32_t color_botmid = Wheel(hue_botmid);
  
  byte hue_bot = ((spectrumValue1[1]+spectrumValue1[2])/2);
  uint32_t color_bot = Wheel(hue_bot);
  
  
  //Shift the current values.
  for (int i = 0; i<fifth-1; i++)
  {
    top_array[i] = top_array[i+1];
    topmid_array[i] = topmid_array[i+1];
    mid_array[i] = mid_array[i+1];
    botmid_array[i] = botmid_array[i+1];
    bot_array[i] = bot_array[i+1];
  }
  
  //Fill in the new value at the end of each array
  top_array[fifth-1] = color_top;
  topmid_array[fifth-1] = color_topmid;
  mid_array[fifth-1] = color_mid;
  botmid_array[fifth-1] = color_botmid;
  bot_array[fifth-1] = color_bot;
  
  //Go through each Pixel on the strip and set its color
  for (int i=0; i<fifth; i++)
  {
    //set pixel color
    strip.setPixelColor(i, botmid_array[i]);
    strip.setPixelColor(length-fifth*3-i-1, topmid_array[i]);
    strip.setPixelColor(i+fifth*2, mid_array[i]);
    strip.setPixelColor(length-fifth-i-1, topmid_array[i]);
    strip.setPixelColor(i+fifth*4, botmid_array[i]);
  }


  //Display the new values
  strip.setBrightness(255);
  strip.show();
  
  //sample delay
  delay(25);
  }
  Serial.println();

}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}
uint32_t Color(byte r, byte g, byte b)
{
  uint32_t c;
  c = r;
  c <<= 8;
  c |= g;
  c <<= 8;
  c |= b;
  return c;
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}
    delay(0);

Useless. Get rid of this to avoid the appearance of cluelessness.

Right now, any band without an audio signal has the corresponding pixels lit up green and I’d like for them to all be off.

What value being passed to setStripColor() causes the pixels to be green?

Why are there 7 elements in the spectrum arrays, and 5 portions in the pixel strip? Why do you use 6 elements of the spectrum arrays, but only initialize 5 of them?

  strip.show(); // Initialize all pixels to 'off'

This is NOT what that function does. All the pixels are off because they have not been assigned a color to show. You need to figure out what the default value is (probably 0) and assign that value to the portion(s) of the strip you want to be off rather than green.

Paul, that comment for strip.show() comes straight from the adafruit example code. I thought it odd myself at the time, but I can see where he got it from.

Thanks for taking the time to look through my code! I’ve gotten rid of that useless bit.

Why are there 7 elements in the spectrum arrays, and 5 portions in the pixel strip? Why do you use 6 elements of the spectrum arrays, but only initialize 5 of them?

I’m using an msgeq7 arduino shield that splits both left and right channels into 7 bands each. The neopixel shield i’m using is 8x5 pixels, so I’ve averaged some of the spectrum bands together to fit them into the 5 rows on the neopixel shield. My plan is to expand on this with more neopixels, which is why I’m pulling all of the spectrum data. Right now, I’m just trying to see what looks best and learn how to do it.

What value being passed to setStripColor() causes the pixels to be green?

spectrumValue1[i]=constrain(map(analogRead(Lchannel), filter , 1023, 0, 255), 0, 255);
    Serial.print(spectrumValue1[i]);

The values given from the msgeq7 analog read are mapped to a value from 0-255 which are then used to generate an rgb color using this code:

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

I see the problem now! When I get a zero reading, it actually maps to green because of the first condition of that code. Is there any way to fix this by excluding a zero reading or changing if(WheelPos < 85) to include only integers between 1 and 85? I’m really not sure how to try either of these.

Is there any way to fix this by excluding a zero reading or changing if(WheelPos < 85) to include only integers between 1 and 85? I’m really not sure how to try either of these.

Of course.

if(WheelPos == 0)
{
   // Do nothing
}
else if(WheelPos < 85)
{
   // Deal with 1 to 84
}
else // more clauses...

I’m sure I’m doing something wrong, but I’ve tried doing this and it doesn’t really have an effect other than some random blinking. Could you tell me what I’m doing wrong?

uint32_t Wheel(byte WheelPos) {
  if(WheelPos == 0){
    return strip.Color(0,0,0);
  }
  else if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

Could you tell me what I'm doing wrong?

I'd need to know what the input to that function is, and what it returns. Serial.print() to the rescue.