 # How do I use FFT to read audio frequency and turn 6 normal LEDS ?

I’m relativey new to programming and arduino, and I’ve been investigating about FFT for so long, but I’ve been having a hard time figuring out how to make sense with my code.
I would appreciate a hint or an advice to have a better idea of how to do this efficiently. I’m very confused. I’m not making sense myself.
The FFT is supposed to separate the different frequencies of the sound detected from a microphone (A0), and depending on the frequency (lowest - highest), some LEDS (I’m using 6 leds) will turn on.

``````#include "arduinoFFT.h"
#define SAMPLES 128             //arduino uno only supports 128 samples
#define SAMPLING_FREQUENCY 1000
#include <math.h>
arduinoFFT FFT = arduinoFFT();
int val = 0;
unsigned int sampling_period_us;
unsigned long microseconds;

double vReal[SAMPLES];
double vImag[SAMPLES];
int mic = A0; //PWN analogread del mic
const  int one= 2; // for the lowest frequency
const int two= 3;
const int  three = 4;
const int four = 5;
const int five= 6;
const int six = 7; //for the highest frequency
void setup() {
Serial.begin(9600);

sampling_period_us = round(1000000*(1.0/SAMPLING_FREQUENCY));
pinMode(one, OUTPUT);
pinMode(two, OUTPUT);
pinMode(three, OUTPUT);
pinMode(four, OUTPUT);
pinMode(five, OUTPUT);
pinMode(six, OUTPUT);

}

void loop() {

for(int i=0; i<SAMPLES; i++)
{
microseconds = micros();
vImag[i] = 0;
while(micros() < (microseconds + sampling_period_us)){
}

}
}

for (int i = 35 ; i < 700 ; i++) { // 35 - 700 Hz mafnitude at each frequency

digitalWrite(one,  vReal);
digitalWrite(two, vReal);
}

for (int i = 700; i < 1400; i++) {
digitalWrite(three, vReal);
digitalWrite(four,vReal);
}

for (int i = 1400; i < 2170; i++) {
digitalWrite(five, vReal);
digitalWrite(six,vReal);
}
}
``````

What are the last three for loops in loop() doing?

Where is the FFT ?

my brain was collapsing, I'm not that good at programming. I'm solving it, I'll upload a better code in a moment

some LEDS (I'm using 6 leds) will turn on.

Just in case you're trying to make a guitar tuner with the Arduino, a lot of people have failed and maybe a few have succeeded. (And the rumor is, autocorrelation works better than FFT, if that's what you're trying to do...)

What do you mean by autocorrelation?

https://en.m.wikipedia.org/wiki/Autocorrelation

It is only useful when you are looking for a specific frequency in a signal you know might be there. Not so much looking to see what frequency you have.

Lu2829: ...to separate the different frequencies of the sound detected from a microphone (A0), and depending on the frequency (lowest - highest), some LEDS (I'm using 6 leds) will turn on.

Maybe easier to use a dedicated chip for this, like the MSGEQ7 (seven frequency bands). And a microphone with build-in preamp. Leo..

The big problem is the MSGEQ7 chip. It is hard to get one that works. A lot of the ones off eBay are fakes and do not work. The consensus seems to be the only genuine ones originate from Spark Fun, so direct from them, or their distributors.