Skip to content

Instantly share code, notes, and snippets.

Created April 22, 2014 10:15
Show Gist options
  • Save anonymous/11173048 to your computer and use it in GitHub Desktop.
Save anonymous/11173048 to your computer and use it in GitHub Desktop.
Code for Arduino Spectrrum Analyzer
//#define LOG_OUT 1 // use the log output function
#define OCTAVE 1 // use the octave function
#define FFT_N 256 // set to 256 point fft
#define NBR_MTX 2 //Matrix
#include <FFT.h> // include the library
#include "LedControlMS.h"
int val[8];
LedControl lc=LedControl(7,6,5, NBR_MTX);//Configure pins for matrix // 12,11,10,NBR_MTX
void setup() {
for (int i=0; i< NBR_MTX; i++){
lc.shutdown(i,false);
lc.setIntensity(0,0);
lc.clearDisplay(i);
}
pinMode(A0,INPUT);
Serial.begin(115200); // use the serial port
TIMSK0 = 0; // turn off timer0 for lower jitter
ADCSRA = 0xe5; // set the adc to free running mode
ADMUX = 0x40; // use adc0
DIDR0 = 0x01; // turn off the digital input for adc0
lc.setLed(0,0,0,true);
}
void loop() {
while(1) { // reduces jitter
cli(); // UDRE interrupt slows this way down on arduino1.0
for (int i = 0 ; i < 512 ; i += 2) { // save 256 samples
while(!(ADCSRA & 0x10)); // wait for adc to be ready
ADCSRA = 0xf5; // restart adc
byte m = ADCL; // fetch adc data
byte j = ADCH;
int k = (j << 8) | m; // form into an int
k -= 0x0200; // form into a signed int
k <<= 6; // form into a 16b signed int
fft_input[i] = k; // put real data into even bins
fft_input[i+1] = 0; // set odd bins to 0
}
fft_window(); // window the data for better frequency response
fft_reorder(); // reorder the data before doing the fft
fft_run(); // process the data in the fft
//fft_mag_log(); // take the output of the fft
fft_mag_octave();
sei();
// lc.clearDisplay(0);
for(int idx=0; idx < 8; idx++)
{
val[idx] = fft_oct_out[idx]/32;
}
for(int idx=0; idx < 8; idx++)
{
for(int x = 0; x < 8; x++)
{
if(x <= val[idx])
lc.setLed(0,x,7-idx,true);
else
lc.setLed(0,x,7-idx,false);
}
}
//delay(5);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment