Hi guys!
I'm making a little spectrum analyzer using an Arduino Mega and 4x MAX7219 8x8 LED display modules. My plan is to assign a LED bar for each frequency band (30 60 120 240 480 1000 2000 8000 16000 Hz) just as commercial spectrum analyzers do.
My first attempt was replicating this project: https://create.arduino.cc/projecthub/shajeeb/32-band-audio-spectrum-visualizer-analyzer-902f51 which kinda works, but sadly the code is very hard to modify / maintain.
After many hours of searching I found this another code, which is great to get the amplitude of a specific frequency.
https://www.norwegiancreations.com/2017/08/what-is-fft-and-how-can-you-implement-it-on-an-arduino/
I tried to adapt it to work with my LED matrix display, and at first glance looks like each bar corresponds to the amplitude. However the latency is extremely high, making the visualizer useless.
Please, could you help me to find out if I'm going on the right direction?
Thank you very much
#include "LedControl.h"
#include "arduinoFFT.h"
LedControl lc = LedControl(12, 11, 10, 4);
arduinoFFT FFT = arduinoFFT();
//Original code stolen from https://www.norwegiancreations.com/2017/08/what-is-fft-and-how-can-you-implement-it-on-an-arduino/
//Original samples was 128
#define SAMPLES 256 //Must be a power of 2
int SAMPLING_FREQUENCY; //Original code warns agains using more than 10000 due to ADC, this is ignored
unsigned long microseconds;
double vReal[SAMPLES];
double vImag[SAMPLES];
//Each LED bar would correspond to a band
//30 60 120 240 480 1000 2000 4000 8000 16000
int frecuenciasDeseadas[] = { 60, 120, 240, 480, 960, 2000, 4000, 8000, 16000, 32000 };
int arrayPosEquivX[32];
int ultimoValorCol[33];
void setup()
{
int devices = lc.getDeviceCount();
for (int address = 0; address < devices; address++)
{
lc.shutdown(address, false);
lc.setIntensity(address, 3);
lc.clearDisplay(address);
}
//Since the LED matrix is inverted (ex: 0 corresponds to the 32th led,
//and 32 corresponds to the first led, I made this little look up table
//to drive it on a more intuitive way
int xEquiv = 0;
int index = 1;
for (int i = 32; i >= 1; i--)
{
arrayPosEquivX[i] = xEquiv;
if (xEquiv < 7)
{
xEquiv++;
}
else
{
xEquiv = 0;
}
index++;
}
}
void loop()
{
//Make the analysis for each band
for (int i = 0; i < 10; i++)
{
SAMPLING_FREQUENCY = frecuenciasDeseadas[i];
/*SAMPLING*/
for (int i = 0; i < SAMPLES; i++)
{
vReal[i] = analogRead(0);
vImag[i] = 0;
}
/*FFT*/
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
int constrainedInput = constrain(vReal[127], 0, 80);
int mappedImput = map(constrainedInput, 0, 60, 0, 8);
animBarra(i, mappedImput);
}
}
//This section of the code makes the animation for the bar movement
void animBarra(int col, int nuevoValDeseado)
{
int valorActualCol = ultimoValorCol[col];
//if the bar needs to go up
if (nuevoValDeseado > valorActualCol)
{
for (int i = valorActualCol; i <= nuevoValDeseado; i++)
{
accionaLed(col, i, true);
}
ultimoValorCol[col] = nuevoValDeseado;
return;
}
//If the bar needs to go down
if (nuevoValDeseado < valorActualCol)
{
for (int i = valorActualCol; i >= nuevoValDeseado; i--)
{
accionaLed(col, i, false);
}
ultimoValorCol[col] = nuevoValDeseado;
return;
}
}
//Turns on or off each LED on the matrix
void accionaLed(int x, int y, bool statusDeseado)
{
int cuadrante;
if (x >= 0 && x <= 8)
{
cuadrante = 0;
}
if (x > 8 && x <= 16)
{
cuadrante = 1;
}
if (x > 16 && x <= 24)
{
cuadrante = 2;
}
if (x > 24)
{
cuadrante = 3;
}
lc.setLed(cuadrante, y - 1, arrayPosEquivX[x], statusDeseado);
}