I have made good progress since my last post. This is my schematic. First time using Fritzing, sorry for the bad wiring!
#include <stdint.h>
#include <ffft.h>
#define IR_AUDIO 0 // ADC channel to capture
#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3
volatile byte position = 0;
volatile long zero = 0;
int16_t capture[FFT_N]; /* Wave captureing buffer */
complex_t bfly_buff[FFT_N]; /* FFT buffer */
uint16_t spectrum[FFT_N/2]; /* Spectrum output buffer */
uint16_t spectrumPrior[FFT_N/2]; // prior of spectrum
float levelsPrior[3];
typedef struct _RGB {
byte r;
byte g;
byte b;
} RGB;
RGB led;
void setRGB(int r, int g, int b)
{
// sanitize inputs
if (r < 0) r = 0; if (r > 255) r = 255;
if (g < 0) g = 0; if (g > 255) g = 255;
if (b < 0) b = 0; if (b > 255) b = 255;
led.r = r;
led.g = g;
led.b = b;
updateLights();
}
void updateLights()
{
analogWrite(REDPIN, led.r);
analogWrite(GREENPIN, led.g);
analogWrite(BLUEPIN, led.b);
}
void turnOff()
{
setRGB(0, 0, 0);
updateLights();
}
void bootLEDSequence();
{
turnOff();
setRGB(255, 0, 0);
delay(500);
setRGB(0, 255, 0);
delay(500);
setRGB(0, 0, 255);
delay(500);
turnOff();
}
void graph()
{
for (byte i = 0; i < 64; i++) {
Serial.print(spectrum[i]);
Serial.print('\t');
}
Serial.println();
}
void setup()
{
bootLEDSequence();;
Serial.begin(57600);
adcInit();
adcCalb();
setRGB(0, 0, 255);
}
void loop()
{
if (position == FFT_N) {
fft_input(capture, bfly_buff);
fft_execute(bfly_buff);
fft_output(bfly_buff, spectrum);
graph();
byte peak = 0;
for (byte i = 0; i < 41; i++) {
if (spectrum[i] > 160 && spectrum[i] > spectrum[i-1] && spectrum[i] > spectrum[i+1]) peak = i;
}
if (peak < 21) {
// setRGB(255 - 25*peak, 25*peak,0);
} else {
peak -= 20;
// setRGB(0, 255 - 25*peak, 25*peak);
}
position = 0;
}
}
/**
* Begin FFT functions
*/
ISR(ADC_vect)
{
if (position >= FFT_N)
return;
capture[position] = ADC + zero;
if (capture[position] == -1 || capture[position] == 1)
capture[position] = 0;
position++;
}
void adcInit()
{
/** REFS0 : VCC use as a ref,
* IR_AUDIO : channel selection,
* ADEN : ADC Enable,
* ADSC : ADC Start,
* ADATE : ADC Auto Trigger Enable,
* ADIE : ADC Interrupt Enable,
* ADPS : ADC Prescaler
*/
// free running ADC mode, f = ( 16MHz / prescaler ) / 13 cycles per conversion
ADMUX = _BV(REFS0) | IR_AUDIO; // | _BV(ADLAR);
// ADCSRA = _BV(ADSC) | _BV(ADEN) | _BV(ADATE) | _BV(ADIE) | _BV(ADPS2) | _BV(ADPS1)
//prescaler 64 : 19231 Hz - 300Hz per 64 divisions
ADCSRA = _BV(ADSC) | _BV(ADEN) | _BV(ADATE) | _BV(ADIE) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0);
// prescaler 128 : 9615 Hz - 150 Hz per 64 divisions, better for most music
sei();
}
void adcCalb()
{
long midl = 0;
// get 2 meashurment at 2 sec
// on ADC input must be NO SIGNAL!!!
for (byte i = 0; i < 2; i++) {
position = 0;
delay(100);
midl += capture[0];
delay(900);
}
zero = -midl/2;
}
/**
* End FFT functions
*/
The LED in the top right is actually a 5m strip of analog RGB LEDs, but I couldn't find a drawing for them in Fritzing. The whole circuit is driven by a 12V power supply from Adafruit.
I'm using this as a baseline for my code. From the GitHub code, in the main loop, I get a 64 element array that is the magnitude of the spectrum from 0 to 4.8 kHz as given from the microphone. My graph() function writes this code out to Serial, as a tab delimited string of values from the array (one line per array instance). The main array is called spectrum.
Then I use LabVIEW to read this data, parse the string into an array, and display the array as a bar graph. It's somewhat of a workaround, but I wanted a visualization of the spectrum data. However, whenever I control the LED lights, the spectrum graph becomes messed up. These are the lines that are giving me trouble are here:
graph();
byte peak = 0;
for (byte i = 0; i < 41; i++) {
if (spectrum[i] > 160 && spectrum[i] > spectrum[i-1] && spectrum[i] > spectrum[i+1]) peak = i;
}
if (peak < 21) {
// setRGB(255 - 25*peak, 25*peak,0);
} else {
peak -= 20;
// setRGB(0, 255 - 25*peak, 25*peak);
}
With this code, I simply output the spectrum through USB, and then find the (last) peak index in the range 0 - 40 (my choice), and then do nothing with it. Index 40 corresponds to about 3kHz. This is what I see in LabVIEW:
Video (no LED control)
I'm using an iPhone to generate the tones. This is actually a very accurate output. I was surprised at how well it performed and how generally responsive it was given that I'm doing a software FFT.
However, if I uncomment the setRGB lines, then the array from the FFT starts going haywire:
Video (LEDs changing)
Can anyone help me reason as to why this is happening? Ideally, my code would give you a smooth transition from red to green to blue as the calculated peak goes from 0 to 40. Am I getting a noisy signal because of the analogWrites? When I went past 3kHz on the iPhone (index 40), the FFT stabilized because the Arduino stops changing the values of the LEDs (by design of the code -- there are no peaks > 160 through index 40 so the peak stays at 0 and the LEDs stay red).
Further note:
I initially had wired the microphone breakout, the three transistors, and the reset button all to the same ground, but I was getting a value of around 160 on spectrum[0] and about 80 on spectrum[1] with no input. With this setup, at least the spectrum stays clean. There is the slightest bit of noise on indexes 0 and 1, but nothing like the other setup. Here is a video of the Arduino analyzing a clip of a soundtrack, with the setRGB lines commented out. The spectrum is correct, even catching the accented high-pitched note.
I appreciate any and all feedback.
EDIT: The FFT calculation sometimes stabilizes when I switch off the LEDs, not sure why.