Go Down

Topic: Spectrum Analyzer using Adafruit NeoPixel Strips & Arduino Mega (Read 6173 times) previous topic - next topic

Coding Badly


Edcastillo09

Yes it's does but I'm sure it doesn't like the way I set up my array. I don't think I can have an array index of "(r,g,b)" where r,g,b are the values. I'm going to put HMTL Hex color codes instead so I won't have multiple values in an array index.

I think my for loops in my void startshow are incorrect. I'm pretty much trying to create the [7][20] in my for loops instead of at the beginning so when it starts pulling values for color[Band][Pixel] in my  mapSpectrum it will give the correct color to each pixel it's in band. Then repeat for each band.

Sorry if that's confusing. If you want me to I can show you in an excel sheet the [7][20] array I want to create with my for loops. 

Coding Badly

Quote
I could you HEX codes for the colors but that's a pain.
As your code stands you are currently mixing the two methods.  You are storing separate RGB values then passing just one of those values in the calls to setPixelColor.  (You need to learn how the comma operator works.)  Pick one method and use it consistently.


Edcastillo09

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:

Code: [Select]

#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]

Coding Badly

I looked into the comma operator, but I'm not sure how I can use that to my benefit...
You can't.  The comma operator is essentially worthless junk.  Stop using it.


britishlibrary

Hi Everyone,

I've just been reading through this thread, as I'm trying to create a similar type of device myself - both with the 7-bar display, but also as a general 'loudness' display.

(In short, I'm trying to make a costume that flashes with the 'spectrum analysis' and also in time with the volume).

I'm trying to utilise one of these microphone breakouts for the sensing:

https://www.ebay.co.uk/itm/Microphone-Sensor-AVR-PIC-High-Sensitivity-Sound-Detection-Module-For-Arduino/152807326484

But looking at the info I can find - I'm not sure it can pass an appropriate analog signal into the MSGEQ7.

Can anyone point out a better component I should use for inputting into the EQ. The only restriction is that I need the audio source to be from a microphone, as I can't connect directly.

Thanks :)

Wawa

There are microhone boards with threshold detection pot (analogue and digital out).
The analogue amplification could be bad, or non-existing.

And there are microphone boards with proper amplification (only analogue out).
This is an example (no personal experience with this one).
Leo..

Go Up