Spectrum Analyser Help

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
samme71
 
Posts: 3
Joined: Mon Dec 16, 2013 4:33 pm

Spectrum Analyser Help

Post by samme71 »

I am very new to the Arduino community and I am building a project for my church, But I'm having trouble with the code. Hope you guys can help.

I am building a 7 band audio spectrum analyzer using (7) 1 meter NeoPixel strips, Arduino and Bliptronics spectrum shield. I have done all the wiring and built the towers and I have successfully uploaded a test pattern. All the LED's work great. But now comes the hard part, (for me anyway) making them move to music. I am trying to find an Arduino sketch that will work with the equipment I'm using and look something like this.http://www.youtube.com/watch?v=JFKPlNoBpfk

I have found several project files that are similar to what I need, but they all seem use code that is for older LED tech (2 pins for data not 1 like mine). I just don't know enough to modify those codes or write one from scratch. So I was hoping some one out there has already done this with the new WS2812 NeoPixel LED's and a spectrum shield.

Any help or guidance would be greatly appreciated.

1chicagodave
 
Posts: 564
Joined: Wed Jun 19, 2013 3:35 am

Re: Spectrum Analyser Help

Post by 1chicagodave »

I found this shield using google…?
http://www.bliptronics.com/item.aspx?ItemID=116

And, on that page I found this example code -

Code: Select all


// Spectrum analyzer read values will be kept here.
int SpectrumLeft[7];
int SpectrumRight[7];

  //Setup pins to drive the spectrum analyzer. It needs RESET and STROBE pins.
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);

  //Init spectrum analyzer
  digitalWrite(4,LOW);  //pin 4 is strobe on shield
  digitalWrite(5,HIGH); //pin 5 is RESET on the shield
  digitalWrite(4,HIGH);
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);


// Function to read 7 band equalizers
 
void readSpectrum()
{
  // Band 0 = Lowest Frequencies.
  byte Band;
  for(Band=0;Band <7; Band++)
  {
    SpectrumLeft[Band] = analogRead(0); //left
    SpectrumRight[Band] = analogRead(1) //right
    digitalWrite(4,HIGH);  //Strobe pin on the shield
    digitalWrite(4,LOW);     
  }
}

From there, all you need to do is transfer the values from the array to the NeoPixel strips. How you would do that would depend greatly on how they are connected and how you would like them to 'look' (i.e. colors, etc..).

How do you have them connected? (In series? Parallel? Combo?) Which pins on Arduino? How many pixels per strip?
Do you have a diagram?

Thanks!

** Found example code for what you want on their site too.
http://www.bliptronics.com/arduinocode/ ... i_2801.zip

samme71
 
Posts: 3
Joined: Mon Dec 16, 2013 4:33 pm

Re: Spectrum Analyser Help

Post by samme71 »

Hey 1chicagodave,
Thanks for your help. Yea I did find this code as well, but from what I could tell this code was set up to use LEDs that have 2 data wires, but the neopixel strip I am using only has one data wire. I'm afraid that being new to arduino I just don't know enough to alter the code to make it work from me. What I have done is build 7 (1 meter) towers each with a 1 meter strip attached to the tower. The strips are wired together using extension cables, with data and power entering the bottom of the first strip, exiting the top and entering the bottom of the next strip. The strips are basically daisy chained together making one long strip. There are 30 LEDs per strip for a total of 210 LEDs .I'm sorry I don't have a diagram. I have the shield plugged into the headers on the arduino and I am using a ground pin and pin 7 for data. This worked well when I uploaded the test pattern. The strip is being powered by a separate power supply. As far as look goes what I was thinking was to have each strip light up green up about 3/4 up and then red at the top 1/4. I hope this clears up my plans a little.

Any additional help you could offer would be greatly appreciated.
Thanks.

1chicagodave
 
Posts: 564
Joined: Wed Jun 19, 2013 3:35 am

Re: Spectrum Analyser Help

Post by 1chicagodave »

OK, that second link to a .zip file with example code came from their page with their version of NeoPixels which use the same IC (WS2812) as the NeoPixels. So, I imagine that is what it was written for.
** EDIT - You're right, I misread that. Their examples are for WS2801(SPI) …not WS2812 (NeoPixels)….sorry. But, they would work with the RGB LED Pixels…. http://www.adafruit.com/products/738


I can probably sketch up something simple to get you started with your setup though. I'll try to post it sometime today.

1chicagodave
 
Posts: 564
Joined: Wed Jun 19, 2013 3:35 am

Re: Spectrum Analyser Help

Post by 1chicagodave »

Ok, I threw something together… SUPER SIMPLE & not nearly as elegant or cool as what is possible. But, hopefully it will get ya started////and let you know if everything is working properly!

This code should work. It does compile alright, but I wasn't able to test it yet. So, if you have any issues please let me know!
I also assumed you only gave one 'board' set up - you don't have individual left and right…but ARE using both inputs on shield. If that is not the case, some simple changes need to be made.

It's only one color (Blue), and has zero interesting features. Extremely straight forward design. I'll try taking a look at some other code and libraries and see if I can port a cooler version over to work with NeoPixels!

Code: Select all

#include <Adafruit_NeoPixel.h>

#define PIN 7
#define LEDS 210
#define OnColor 0x0000FF    // BLUE
#define OffColor 0x000000    // OFF
Adafruit_NeoPixel Strip = Adafruit_NeoPixel(LEDS, PIN, NEO_GRB + NEO_KHZ800);


// Spectrum analyzer read values will be kept here.
int SpectrumLeft[7];
int SpectrumRight[7];
// combined/averaged values
int SpectrumMono[7];
// Individual NeoPixels
int SpectrumGraph[7][30];

void setup() 
{
  // Assign pixels to graph array
  for (byte bar = 0 ; bar < 7 ; bar++)
  {
    for (byte pixel = 0 ; pixel < 30 ; pixel ++)
    {
      SpectrumGraph[bar][pixel] = (bar * 30) + pixel;
    }    // end for
  } // end for

  //Setup pins to drive the spectrum analyzer. It needs RESET and STROBE pins.
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);

  //Init spectrum analyzer
  digitalWrite(4,LOW);  //pin 4 is strobe on shield
  digitalWrite(5,HIGH); //pin 5 is RESET on the shield
  digitalWrite(4,HIGH);
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);


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

}    //  end setup()


void loop()
{
  readSpectrum();      // Get values from spectrum shield
  mapSpectrum();      // Color pixels according to spectrum values

}

// Function to read 7 band equalizers

void readSpectrum()
{
  // Band 0 = Lowest Frequencies.
  byte Band;
  for(Band=0;Band <7; Band++)
  {
    SpectrumLeft[Band] = analogRead(0); //left
    SpectrumRight[Band] = analogRead(1); //right
    digitalWrite(4,HIGH);  //Strobe pin on the shield (go to next Band)
    digitalWrite(4,LOW);   
    //  Make a third Spectrum which combines/averages the left & right values
    SpectrumMono[Band] = ((SpectrumLeft[Band] + SpectrumRight[Band])/2); 
  }
}

void mapSpectrum()    // map spectrum to pixel strips
{
  for(byte Band=0 ; Band <7 ; Band++)
  {      // remap the analogRead values to fit our 30-pixel segments/'bars'
    SpectrumMono[Band] = map(SpectrumMono[Band], 0, 1023, 0, 30);

    for (byte Pixel = 0 ; Pixel < SpectrumMono[Band] ; Pixel++)
    {    // Turn appropriate pixels ON!
      Strip.setPixelColor(SpectrumGraph[Band][Pixel], OnColor);
    }    // end for (Pixel 
 
    for (byte Pixel = SpectrumMono[Band] ; Pixel < 30 ; Pixel++)
    {      // Turn the rest OFF!
      Strip.setPixelColor(SpectrumGraph[Band][Pixel], OffColor);
    }    // end for (Pixel

  }    // end for (Band
  Strip.show();
}    //  end mapSpectrum()



User avatar
hiduino
 
Posts: 862
Joined: Sat Sep 01, 2012 7:05 pm

Re: Spectrum Analyser Help

Post by hiduino »

I have already adopted SparkFun's SpectrumShield, which is the same as the Bliptronics based on the MSGEQ7 chip, to the Neopixel Matrix shield. You would need to modify it to use with the strips, but should be minimal.

Code: Select all

// Adafruit_NeoMatrix example for single NeoPixel Shield.

#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#ifndef PSTR
 #define PSTR // Make Arduino Due happy
#endif

#define PIN 6

Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 5, PIN,
  NEO_MATRIX_TOP     + NEO_MATRIX_LEFT +
  NEO_MATRIX_ROWS    + NEO_MATRIX_PROGRESSIVE,
  NEO_GRB            + NEO_KHZ800);

//For spectrum analyzer shield, these three pins are used.
//You can move pinds 4 and 5, but you must cut the trace on the shield and re-route from the 2 jumpers. 
int spectrumReset=5;
int spectrumStrobe=4;
int spectrumAnalog=0;  //0 for left channel, 1 for right.

//This holds the 15 bit RGB values for each LED.
//You'll need one for each LED, we're using 25 LEDs here.
//Note you've only got limited memory, so you can only control 
//Several hundred LEDs on a normal arduino. Double that on a Duemilanove.

// Spectrum analyzer read values will be kept here.
int Spectrum[7];

const uint16_t colors[] = {
  matrix.Color(0, 0, 0),     matrix.Color(0, 180, 0),   matrix.Color(0, 180, 0),
  matrix.Color(180, 180, 0), matrix.Color(180, 180, 0), matrix.Color(180, 0, 0) };

void setup() {

  matrix.begin();
  matrix.setBrightness(40);
  matrix.show();

  //Setup pins to drive the spectrum analyzer. 
  pinMode(spectrumReset, OUTPUT);
  pinMode(spectrumStrobe, OUTPUT);

  //Init spectrum analyzer
  digitalWrite(spectrumStrobe,LOW);
  delay(1);
  digitalWrite(spectrumReset,HIGH);
  delay(1);
  digitalWrite(spectrumStrobe,HIGH);
  delay(1);
  digitalWrite(spectrumStrobe,LOW);
  delay(1);
  digitalWrite(spectrumReset,LOW);
  delay(5);
  // Reading the analyzer now will read the lowest frequency.
  
}


void loop() {
 
  showSpectrum();
  delay(15);  //We wait here for a little while until all the values to the LEDs are written out.
              //This is being done in the background by an interrupt.
}


// Read 7 band equalizer.
void readSpectrum() {
  // Band 0 = Lowest Frequencies.
  byte Band;
  for (Band=0;Band <7; Band++)
  {
    Spectrum[Band] = (analogRead(spectrumAnalog) + analogRead(spectrumAnalog) ) >>1; //Read twice and take the average by dividing by 2
    digitalWrite(spectrumStrobe,HIGH);
    digitalWrite(spectrumStrobe,LOW);     
  }
}


void showSpectrum() {
  //Not I don;t use any floating point numbers - all integers to keep it zippy. 
   readSpectrum();
   byte Band, BarSize, MaxLevel;
   static unsigned int  Divisor = 80, ChangeTimer=0; //, ReminderDivisor,
   unsigned int works, Remainder;

  MaxLevel = 0; 

  for (Band=0;Band<7;Band++)//We only graph the lowest 5 bands here, there is 2 more unused!
  {
  //If value is 0, we don;t show anything on graph
     works = Spectrum[Band]/Divisor;	//Bands are read in as 10 bit values. Scale them down to be 0 - 5
     if (works > MaxLevel)  //Check if this value is the largest so far.
       MaxLevel = works;    
       
     for (BarSize=1;BarSize <=5; BarSize++)  
     {
       if (works > BarSize)
			// LP.setLEDFast(LP.Translate(Band,BarSize-1),BarSize*6,31-(BarSize*5),0);
          matrix.drawPixel(Band, 5-BarSize, colors[BarSize]);  //below the level
       else if ( works == BarSize)
			// LP.setLEDFast(LP.Translate(Band,BarSize-1),BarSize*6,31-(BarSize*5),0); //at the level
          matrix.drawPixel(Band, 5-BarSize, colors[BarSize]);
       else
			// LP.setLEDFast(LP.Translate(Band,BarSize-1),5,0,5);
          matrix.drawPixel(Band, 5-BarSize, colors[0]);  //above the level
     }
  }
  matrix.show();

 // Adjust the Divisor if levels are too high/low.
 // If  below 4 happens 20 times, then very slowly turn up.
  if (MaxLevel >= 5)
  {
    Divisor=Divisor+1;
    ChangeTimer=0;
  }
  else if (MaxLevel < 4)
  {
    if (Divisor > 65)
      if (ChangeTimer++ > 20)
      {
        Divisor--;
        ChangeTimer=0;
      }
  }
  else
  {
    ChangeTimer=0; 
  }
}

samme71
 
Posts: 3
Joined: Mon Dec 16, 2013 4:33 pm

Re: Spectrum Analyser Help

Post by samme71 »

Hiduino,1chicagodave

Hey guys thanks for all you help with this. I'll give these codes a try and see what we get.

User avatar
michaelmunier
 
Posts: 1
Joined: Wed Dec 03, 2014 4:52 am

Re: Spectrum Analyser Help

Post by michaelmunier »

I'm new to Arduino but I have to thank 1chicagodave for his code.. I have been playing with it and able to have each band in rainbow color. I'm still working on how to divide the strip in half, so I can have half strip for the right channel and other half for left channel. The staff and students have enjoy listening and watching lights. I have been using Ipod classic with Fiio DAC amplifier and at home I use audio output on my stereo.

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”