Hello everyone
I'm creating this topic because I'd like to optimise the code of my project.
I've created an "audio spectrum visualizer" out of an auto gain electret microphone, and a 16x16 flexible led matrix.
I'm using an arduino mega 2560
The wiring is in the attachement.
I'm using the GFX, NeoPixel and NeoMatrix libraries from Adafruit, and the fft library from Open Music Lab, which you can download here.
The first part which get the sound and sent it to the fft library was taken from an exemple. The part where I display the frequencies level is where I need help. I'd like the leds to update faster, but i don't really know how to make it.
Here's the code :
#define LOG_OUT 1 // Utilisation de la fonction LOG
#define FFT_N 32 // Etude sur 32 points
#include <FFT.h> // insert la bibliothèque FFT
#include <Adafruit_GFX.h> // insert la bibliothèque Adafruit GFX
#include <Adafruit_NeoMatrix.h> // insert la bibliothèque Adafruit NeoMatrix pr communiquer avec la matrice led
#include <Adafruit_NeoPixel.h> // insert la bibliothèque Adafruit Neopixel, complémentaire a NeoMatrix
#ifndef PSTR
#define PSTR // Make Arduino Due happy (je sais pas à quoi ça sert, mais c'est dans l'exemple de la bibliothèque NeoMatrix, donc je le met)
#endif
#define PIN 22 // définie la pin de la matrice
/* Définition de la matrice :
* Paramètre n°1 => Largeur de la matrice
* Paramètre n°2 => Longueur de la matrice
* Paramètre n°3 => Pin de la matrice
* Paramètre n°4 => Définition du type de la matrice. A ajouter :
* - Position du premier pixel : RIGHT / LEFT + TOP / BOTTOM
* - Agencement des pixels : ROWS / COLUMS
*/
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(16, 16, PIN,
NEO_MATRIX_TOP + NEO_MATRIX_RIGHT +
NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
NEO_GRB + NEO_KHZ800);
int eq[] = {190, 160, 90, 80, 50, 40, 30, 20, 20, 10, 5, 5, 5, 0, 0, 0};
int level[16];
void setup() {
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
matrix.begin();
matrix.setBrightness(10);
}
void loop() {
while(1) { // reduces jitter
cli(); // UDRE interrupt slows this way down on arduino1.0
for (int i = 0 ; i < 64 ; i += 2) { // save 32 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
sei();
//From here, the code is from me, that's the part that I'd like to work faster :
//matrix.writeFastHLine(0, 0, 16, matrix.Color(0, 0, 255));
byte i = 0;
while (i < FFT_N/2) {
level[i] = fft_log_out[i]; // - eq[i];
if (level[i] < 0)
level[i] = 0;
level[i] = map(level[i], 0, 255, 0, 15);
affichage(i, level[i]);
i++;
}
}
}
void affichage(int x, int j) {
int j = 1;
while (j <= 15) {
if (j > y) {
matrix.drawPixel(x, j, 0);
} else if (j < 2) {
matrix.drawPixel(x, j, matrix.Color(0, 0, 255));
} else if (j >= 2 && j <= 10) {
matrix.drawPixel(x, j, matrix.Color(0, 255, 0));
} else if (j > 10) {
matrix.drawPixel(x, j, matrix.Color(255, 0, 0));
}
matrix.show();
j++;
}
}
Some comments are in french, telle me if you need a translation.
Can someone help me make this work faster ?
Thanks
