Coding Badly,
I looked into the comma operator, but I'm not sure how I can use that to my benefit for assigning one color to each band.
I'd like to assign the colors like the following:
I decided I am going to stick with the r,g,b values for my map spectrum. It just works better. I want to add another case for assigning one color per band, but I can't figure this out with modifying my mapSpectrum(). Is there any way to do it?
See current code below:
#include <MSGEQ7.h>
#include <Adafruit_NeoPixel.h>
#define pinReset 27
#define pinStrobe 26
#define BRIGHTNESS 65
#define LEDs 280 //total number of LEDs
#define PIN 22
#define BUTTON_PIN 28
#define OffColor 0x000000 // OFF
Adafruit_NeoPixel Strip = Adafruit_NeoPixel(LEDs, PIN, NEO_GRB + NEO_KHZ800);
int SpectrumLeft[7];
int SpectrumRight[7];
int red[20];
int green[20];
int blue[20];
int color[7][20];
int red_rainbow[20] = {91,255,127,0,0,0,0,0,0,0,0,0,64,127,191,255,255,255,255,189};
int green_rainbow[20] = {2,0,0,0,64,127,191,255,255,255,255,255,255,255,255,255,191,0,0,0};
int blue_rainbow[20] = {47,255,255,255,255,255,255,255,191,127,64,0,0,0,0,0,0,0,0,0};
int red_warm[20] = {206,206,206,255,255,255,206,206,206,255,255,255,255,255,255,255,255,255,255,255};
int green_warm[20] = {36,36,36,0,0,0,132,132,132,165,165,165,205,205,205,255,255,255,255,255};
int blue_warm[20] = {36,36,36,50,50,50,36,36,36,0,0,0,0,0,0,0,0,0,0,0};
int red_cool[20] = {238,238,238,238,238,138,138,138,138,138,100,100,100,100,100,0,0,0,0,0};
int green_cool[20] = {130,130,130,130,130,43,43,43,43,43,0,0,0,0,0,255,255,255,255,255};
int blue_cool[20] = {238,238,238,238,238,226,226,226,226,226,255,255,255,255,255,255,255,255,255,255};
int red_flame[20] = {0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255};
int green_flame[20] = {0,0,0,0,0,0,100,100,100,255,255,255,255,255,255,255,255,120,120,120};
int blue_flame[20] = {255,255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int j;
int SpectrumGraphLeft[7][20];
int SpectrumGraphRight[7][20];
bool oldState = HIGH;
int showType = 0;
void setup()
{
Serial.begin(9600);
Strip.setBrightness(BRIGHTNESS);
pinMode(pinStrobe, OUTPUT);
pinMode(pinReset, OUTPUT);
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 = bar+2)
{
for (byte pixel = 0 ; pixel < 20 ; pixel ++)
{
SpectrumGraphLeft[bar][pixel] = (bar * 20) + pixel;
SpectrumGraphRight[bar][pixel] = 279 - ((bar * 20) + pixel);
} // end for
}
for (byte bar = 1 ; bar < 7 ; bar = bar+2)
{
for (byte pixel = 0 ; pixel < 20 ; pixel ++)
{
SpectrumGraphLeft[bar][pixel] = ((bar+1)*20)-(1+pixel);
SpectrumGraphRight[bar][pixel] = ((13-bar)*20)+pixel;
} // 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<20; j++)
{
red[j] = red_rainbow[j];
green[j] = green_rainbow[j];
blue[j] = blue_rainbow[j];
}
mapSpectrum();
break;
case 2: // Warm
for (j=0; j<20; j++)
{
red[j] = red_warm[j];
green[j] = green_warm[j];
blue[j] = blue_warm[j];
}
mapSpectrum();
break;
case 3: // Cool
for (j=0; j<20; j++)
{
red[j] = red_cool[j];
green[j] = green_cool[j];
blue[j] = blue_cool[j];
}
mapSpectrum();
break;
case 4: // Flame
for (j=0; j<20; j++)
{
red[j] = red_flame[j];
green[j] = green_flame[j];
blue[j] = blue_flame[j];
}
mapSpectrum();
break;
case 5: //Red
for (j=0; j<20; j++)
{
red[j] = 225;
green[j] = 0;
blue[j] = 0;
}
mapSpectrum();
break;
case 6: //Blue
for (j=0; j<20; j++)
{
red[j] = 0;
green[j] = 0;
blue[j] = 225;
}
mapSpectrum();
break;
case 7: //Green
for (j=0; j<20; j++)
{
red[j] = 0;
green[j] = 255;
blue[j] =0;
}
mapSpectrum();
break;
default:
//if nothing else matches, do the default
break;
}
}
void loop()
{
Serial.print(SpectrumGraphLeft[7][20]);
Serial.print(SpectrumGraphRight[7][20]);
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 > 7)
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 , 950, 0, 19);
SpectrumRight[Band] = map(SpectrumRight[Band], 100, 950, 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 < 20 ; 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 < 20 ; Pixel++)
{ // Turn the rest OFF!
Strip.setPixelColor(SpectrumGraphRight[Band][Pixel], OffColor);
} // end for (Pixel
} // end for (Band
Strip.show();
}
[/color]