Hello. Im working on finishing a project I had abandoned some time ago. I'm making a spectrum visualizer using a spark fun MSGEQ7 shield and an Arduino Nano. I'm modifying a sketch I got from who knows where over a year ago. It was originally set up for a 7x20 matrix. 7 columns of 20 ws2812b LEDs. I modified it to work with 7x26 instead. I managed to get them all starting in the correct position but noticed when I try to max out the spectrum it leaves the last 3 LEDs in each column off. I'm not very good at the math of this and figure I have some numbers incorrect in my for loop. I have verified it is not a power delivery issue. Any help is appreciated, I would hate to have wasted some LEDs and leave them unused.
#include <MSGEQ7.h>
#include <Adafruit_NeoPixel.h>
//#define A0
//#define A1
#define pinReset 5
#define pinStrobe 4
#define BRIGHTNESS 50
#define LEDs 182 //total number of LEDs
#define PIN 12
#define BUTTON_PIN 10
#define OffColor 0x000000 // OFF
int SpectrumLeft[7];
int SpectrumRight[7];
//int red_rainbow3[26] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
//int green_rainbow3[26] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
//int blue_rainbow3[26] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int red[26];
int green[26];
int blue[26];
int red_rainbow[26] = {91,255,127,0,0,0,0,0,0,0,0,0,64,127,191,255,255,255,255,189,189,189,189,0,0,64};
int green_rainbow[26] = {2,0,0,0,64,127,191,255,255,255,255,255,255,255,255,255,191,0,0,0,0,0,0,0,0,255};
int blue_rainbow[26] = {47,255,255,255,255,255,255,255,191,127,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,75};
int red_rainbow2[26] = {255,255,200,200,185,185,170,170,155,155,120,120,110,110,110,98,98,92,92,85,85,45,40,0,0,0};
int green_rainbow2[26] = {0,0,0,7,9,12,14,17,19,22,26,30,37,40,45,47,50,55,59,63,69,72,78,82,90,100};
int blue_rainbow2[26] = {20,10,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,70,85,100,115};
int red_spect[26] = {15,25,32,45,53,60,68,75,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int green_spect[26] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int blue_spect[26] = {255,255,255,225,225,220,210,200,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int j;
int SpectrumGraphLeft[7][26];
int SpectrumGraphRight[7][26];
Adafruit_NeoPixel Strip = Adafruit_NeoPixel(LEDs, PIN, NEO_GRB + NEO_KHZ800);
//#define MSGEQ7_INTERVAL ReadsPerSecond(50)
//#define MSGEQ7_SMOOTH 191 // Range: 0-255
//CMSGEQ7<MSGEQ7_SMOOTH, pinReset, pinStrobe, pinAnalogleft> MSGEQ7;
// Choose a PWM pin!
#define pinLed 22
bool oldState = HIGH;
int showType = 0;
void setup()
{
Serial.begin(9600);
Strip.setBrightness(BRIGHTNESS);
//pinMode(pinAnalogleft, INPUT);
pinMode(pinStrobe, OUTPUT);
pinMode(pinReset, OUTPUT);
//analogReference(DEFAULT);
digitalWrite(pinStrobe,LOW);
digitalWrite(pinReset,HIGH);
digitalWrite(pinStrobe,HIGH);
digitalWrite(pinStrobe,LOW); //Strobe pin on the shield (go to next Band)
digitalWrite(pinReset,LOW);
// Assign pixels to graph array
for (byte bar = 0 ; bar < 7 ; bar++)
{
if ( bar == 0)
{
for (byte pixel = 0 ; pixel < 26 ; pixel ++)
{
SpectrumGraphLeft[bar][pixel] = (bar * 26) + pixel; //first
SpectrumGraphRight[bar][pixel] = (-1*pixel)+268;
} // end for
}
else if ( bar == 1) //flipped strip
{
for (byte pixel = 0 ; pixel < 26 ; pixel ++)
{
SpectrumGraphLeft[bar][pixel] = (-1*pixel)+51; //second
SpectrumGraphRight[bar][pixel] = (pixel)+252;
} // end for
}
else if (bar ==2)
{
for (byte pixel = 0 ; pixel < 26 ; pixel ++)
{
SpectrumGraphLeft[bar][pixel] = ((bar) * 26) + pixel; //third
SpectrumGraphRight[bar][pixel] = (-1*pixel)+252;
} // end for
}
else if (bar == 3) //flipped strip
{
for (byte pixel = 0 ; pixel < 26 ; pixel ++)
{
SpectrumGraphLeft[bar][pixel] = (-1*pixel)+103; //fourth
SpectrumGraphRight[bar][pixel] = (pixel)+224;
} // end for
}
else if (bar == 4)
{
for (byte pixel = 0 ; pixel < 26 ; pixel ++)
{
SpectrumGraphLeft[bar][pixel] = ((bar) * 26) + pixel; //fifth
SpectrumGraphRight[bar][pixel] = (-1*pixel)+212;
} // end for
}
else if (bar == 5)
{
for (byte pixel = 0 ; pixel < 26 ; pixel ++)
{
SpectrumGraphLeft[bar][pixel] = (-1*pixel)+155; //sixth
SpectrumGraphRight[bar][pixel] = (pixel)+197;
} // end for
}
else if (bar == 6)
{
for (byte pixel = 0 ; pixel < 26 ; pixel ++)
{
SpectrumGraphLeft[bar][pixel] = ((bar) * 26) + pixel; //seventh
SpectrumGraphRight[bar][pixel] = (-1*pixel)+261;
} // end for
}
} // end for
pinMode(BUTTON_PIN, INPUT_PULLUP);
Strip.begin();
Strip.show(); // Initialize all pixels to 'off'
}
void startShow(int i) {
switch(i){
case 1: // Rainbow
for (j=0; j<26; j++)
{
red[j] = red_spect[j];
green[j] = green_spect[j];
blue[j] = blue_spect[j];
}
mapSpectrum();
break;
case 2: //Red
for (j=0; j<26; j++)
{
red[j] = red_rainbow2[j];
green[j] = green_rainbow2[j];
blue[j] = blue_rainbow2[j];
}
mapSpectrum();
break;
case 3: //Blue
for (j=0; j<26; j++)
{
red[j] = 0;
green[j] = 0;
blue[j] = 225;
}
mapSpectrum();
break;
case 4: //Green
for (j=0; j<26; j++)
{
red[j] = 0;
green[j] = 255;
blue[j] = 0;
}
mapSpectrum();
break;
}
}
void loop()
{
Serial.print(SpectrumGraphLeft[7][26]);
Serial.print(SpectrumGraphRight[7][26]);
readSpectrum(); // Get values from spectrum shield
mapSpectrum(); // Color pixels according to spectrum values
// delay(15);
// Get current button state.
bool newState = digitalRead(BUTTON_PIN);
// Check if state changed from high to low (button press).
if (newState == LOW && oldState == HIGH) {
// Short delay to debounce button.
delay(20);
// Check if button is still low after debounce.
newState = digitalRead(BUTTON_PIN);
if (newState == LOW) {
showType++;
if (showType > 4)
showType=1;
startShow(showType);
}
}
// Set the last button state to the old state.
oldState = newState;
}
void readSpectrum()
{
// Band 0 = Lowest Frequencies.
byte Band;
for(Band=0;Band <7; Band++)
{
SpectrumLeft[Band] = analogRead(A0); //left
SpectrumRight[Band] = analogRead(A1); //right
digitalWrite(pinStrobe,HIGH);
digitalWrite(pinStrobe,LOW);
//delayMicroseconds(50);
}
}
void mapSpectrum() // map spectrum to pixel strips
{
for(byte Band=0 ; Band <7 ; Band++)
{
SpectrumLeft[Band] = map(SpectrumLeft[Band],100 , 800, 0, 19);
SpectrumRight[Band] = map(SpectrumRight[Band], 45, 800, 0, 19);
for(int Pixel = 0; Pixel < SpectrumLeft[Band]; Pixel++){
Strip.setPixelColor(SpectrumGraphLeft[Band][Pixel], red[Pixel],green[Pixel],blue[Pixel]);
}
for (byte Pixel = SpectrumLeft[Band] ; Pixel < 26 ; Pixel++)
{ // Turn the rest OFF!
Strip.setPixelColor(SpectrumGraphLeft[Band][Pixel], OffColor);
} // end for (Pixel
for(int Pixel = 0; Pixel < SpectrumRight[Band]; Pixel++){
Strip.setPixelColor(SpectrumGraphRight[Band][Pixel], red[Pixel],green[Pixel],blue[Pixel]);
}
for (byte Pixel = SpectrumRight[Band] ; Pixel < 26 ; Pixel++)
{ // Turn the rest OFF!
Strip.setPixelColor(SpectrumGraphRight[Band][Pixel], OffColor);
} // end for (Pixel
} // end for (Band
Strip.show();
}