×

SparkFun will be closing on Monday for Memorial Day (5/28). Orders placed after 2pm MT on Friday (5/25) will process and ship out on Tuesday (5/29).

Kevin_J

Member Since: December 6, 2015

Country: United States

  • For anyone looking for a place to start, I made a compact class for the shield (very bare bones, but it makes the application code look really nice). Copy the code below labelled “Header File” into a file called “Sparkfun_Spectrum_Shield.h” and the code labelled “CPP File” into a file called “Sparkfun_Spectrum_Shield.cpp” and put both of those into your Arduino project folder.

    Header File:

    #ifndef SPARKFUN_SPECTRUM_SHIELD_H
    #define SPARKFUN_SPECTRUM_SHIELD_H
    
    #include <Arduino.h>
    
    class Sparkfun_Spectrum_Shield
    {
    private:
        int reset_pin_;
        int strobe_pin_;
        int right_pin_;
        int left_pin_;
    
    public:
        // The following arrays hold the analog values for the 7 bands
        // and are updated when the .Update() function is called
        int right_channel[7];
        int left_channel[7];
        int average[7];
    
        void Init();
        void Update();
    
        Sparkfun_Spectrum_Shield(); //Constructor that uses pins defined by
                                    //how the shield if physically laid out
        Sparkfun_Spectrum_Shield(int reset,int strobe,int right,int left);
    };
    
    #endif
    

    CPP File:

    #include "Sparkfun_Spectrum_Shield.h"
    
    Sparkfun_Spectrum_Shield::Sparkfun_Spectrum_Shield()
    {
        reset_pin_ = 5;
        strobe_pin_ = 4;
        right_pin_ = 1;
        left_pin_ = 0;
    }
    
    Sparkfun_Spectrum_Shield::Sparkfun_Spectrum_Shield(int reset,int strobe,int right,int left)
    {
        reset_pin_ = reset;
        strobe_pin_ = strobe;
        right_pin_ = right;
        left_pin_ = left;
    }
    
    void Sparkfun_Spectrum_Shield::Init()
    {
    
        //Initialize pins for spectrum analyzer
        pinMode(reset_pin_, OUTPUT);
        pinMode(strobe_pin_, OUTPUT);
        digitalWrite(strobe_pin_, HIGH);
        digitalWrite(reset_pin_, HIGH);
    
        //Initialize spectrum analyzer
        digitalWrite(strobe_pin_, LOW);
        delay(1);
        digitalWrite(reset_pin_, HIGH);
        delay(1);
        digitalWrite(strobe_pin_, HIGH);
        delay(1);
        digitalWrite(strobe_pin_, LOW);
        delay(1);
        digitalWrite(reset_pin_, LOW);
        delay(5);
    }
    
    void Sparkfun_Spectrum_Shield::Update()
    {
        // Band 0=63Hz, 1=160Hz, 2=400Hz, 3=1kHz, 4=2.5kHz, 5=6.25kHz, 6=16kHz
        byte band;
    
        for (band = 0; band < 7; band++)
        {
            //Read twice and take the average by dividing by 2
            right_channel[band] = (analogRead(left_pin_) + analogRead(left_pin_) ) >> 1;
            //Read twice and take the average by dividing by 2
            left_channel[band] = (analogRead(right_pin_) + analogRead(right_pin_) ) >> 1;
    
            //Compute average of left and right channel
            average[band] = (right_channel[band]+left_channel[band])/2;
    
            //Strobe to get next frequency bin
            digitalWrite(strobe_pin_, HIGH);
            digitalWrite(strobe_pin_, LOW);
        }
    }
    

    Example Main .ino File:

    #include "Sparkfun_Spectrum_Shield.h"
    
    Sparkfun_Spectrum_Shield spectrum = Sparkfun_Spectrum_Shield();
    
    void setup()
    {
        //initialize spectrum shield
        spectrum.Init();
    
        //open serial terminal connection
        Serial.begin(9600);
    }
    
    void loop()
    {
        spectrum.Update();
    
        for (int i = 0; i < 7; i++)
        {
            Serial.println(spectrum.average[i]);
        }
    
        delay(100);
    }
    

No public wish lists :(