Here's a step-by-step guide to constructing a simple 14-band (2 x 7) audio spectrum analyzer using two MSGEQ7s and a chipKIT or Arduino microcontroller development platform.
Modifying the software to drive the LEDs
Once again, the software for the spectrum analyzer is identical for the chipKIT MAX32 and the Arduino Mega. You can access the full source for this code (including comments) by clicking here.
Let's skim through this quickly. The initial declarations are the same as for our first program, except that we've added two arrays of integers called "pwmLEDsLeft" and "pwmLEDsRight." These are initialized with the numbers of the PWM-capable digital output pins we wish to use.
The setup() function is the same as before, so we don't need to cover this again. In the case of the loop() function, we just loop around first calling our readMSGEQ7s() function and then calling a new writeLEDs() function, as illustrated below.
With regard to the 10-millisecond delay at the end of the loop, there's no real reason for this to be here. I'm still tweaking things to see if a delay improves the quality of the display.
The readMSGEQ7s() function is pretty much the same as for our first program. As shown below, the only real difference is that we've added a couple of lines to filter any noise out of the "bottom" of the signals; this prevents the LEDs from flickering when there isn’t any music playing.
The main addition to this program is a new writeLEDs() function, as shown below.
This function loops around seven times, outputting the PWM values corresponding to each of the frequency bands to the appropriate LEDs. In this case, the analogWrite() functions are used to apply our PWM values to the PWM-enabled digital pins.
Thus we end up where we started, with this video showing everything in action. In this case, we're listening to a track from Supertramp's Breakfast in America album.
See also my step-by-step test guide, in which I describe the various test procedures I employed while getting everything up and running. Also, you may be interested in my follow-up column on software and timing. As always, of course, I would very much appreciate your comments and feedback.
Page 1: Introducing the MSGEQ7
Page 2: Creating the first-pass hardware
Page 3: Creating the first-pass software
Page 4: Modifying the hardware to add the LEDs
Page 5: Modifying the software to drive the LEDs
— Max Maxfield, Editor of All Things Fun & Interesting