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);
}
}