Ok, I have condensed my code down to the bare minimum to help illustrate my question. Note, while condensing it, I found a few issues that probably added to the latency issue.
I changed the code to simply print the content of the FFT bins. Here, I am using the analogRead() method.
#define version_string "basic_fft_serial_AR.ino"
#include <arduinoFFT.h>
#define SAMPLES 32 //Must be a power of 2
double vReal[SAMPLES];
double vImag[SAMPLES];
int yvalue;
arduinoFFT FFT = arduinoFFT(); // FFT object
void setup() {
Serial.begin(115200); // 19200
// ADCSRA = 0b11100101; // set ADC to free running mode and set pre-scalar to 32 (0xe5)
// ADMUX = 0b00000000; // use pin A0 and external voltage reference
}
void loop() {
// ++ Sampling
for(int i=0; i<SAMPLES; i++)
{
// while(!(ADCSRA & 0x10)); // wait for ADC to complete current conversion ie ADIF bit set
// ADCSRA = 0b11110101 ; // clear ADIF bit so that ADC can do next operation (0xf5)
// int value = ADC - 512 ; // Read from ADC and subtract DC offset caused value
int value = analogRead(0) - 512 ; // Read from ADC and subtract DC offset caused value
vReal[i]= value/8; // Copy to bins after compressing
vImag[i] = 0;
}
// -- Sampling
// ++ FFT
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
// -- FFT
// ++ send to display according measured value
for(int i=0; i<SAMPLES/2; i++){
Serial.print(vReal[i]);
Serial.print('\t');
}
Serial.println();
// -- send to display according measured value
}
When I run it and inject a 2500 HZ sine wave into pin A0, I get this on the serial monitor.
1.59 1.13 1.34 2.15 1.43 0.72 0.23 1.47 2.73 41.97 55.28 14.21 3.20 2.20 3.52 2.14
3.34 1.99 1.46 1.35 2.91 3.33 1.22 1.75 7.76 46.54 49.11 8.95 1.98 3.45 1.81 0.40
1.91 0.99 2.09 1.99 1.14 1.58 0.94 0.30 3.85 41.32 54.48 12.41 1.10 1.03 1.21 3.40
3.64 2.81 0.20 2.56 3.54 2.81 0.65 1.49 8.23 47.49 51.14 8.34 1.46 2.38 1.78 3.91
2.74 3.27 0.77 2.33 1.89 4.04 1.91 2.66 2.87 44.11 54.79 14.44 1.40 1.64 0.85 1.08
This is inline with what I expected. Usable date in bins 9 and 10.
But, when make these changes
void setup() {
Serial.begin(115200); // 19200
ADCSRA = 0b11100101; // set ADC to free running mode and set pre-scalar to 32 (0xe5)
ADMUX = 0b00000000; // use pin A0 and external voltage reference
}
void loop() {
// ++ Sampling
for(int i=0; i<SAMPLES; i++){
while(!(ADCSRA & 0x10)); // wait for ADC to complete current conversion ie ADIF bit set
ADCSRA = 0b11110101 ; // clear ADIF bit so that ADC can do next operation (0xf5)
int value = ADC - 512 ; // Read from ADC and subtract DC offset caused value
// int value = analogRead(0) - 512 ; // Read from ADC and subtract DC offset caused value
vReal[i]= value/8; // Copy to bins after compressing
vImag[i] = 0;
}
// -- Sampling
I get this in the serial monitor (no matter what kind of signal I put on A0)
1059.66 470.28 10.45 3.83 2.01 1.23 0.83 0.58 0.43 0.32 0.24 0.18 0.13 0.10 0.06 0.03
1059.66 470.28 10.45 3.83 2.01 1.23 0.83 0.58 0.43 0.32 0.24 0.18 0.13 0.10 0.06 0.03
1059.66 470.28 10.45 3.83 2.01 1.23 0.83 0.58 0.43 0.32 0.24 0.18 0.13 0.10 0.06 0.03
1059.66 470.28 10.45 3.83 2.01 1.23 0.83 0.58 0.43 0.32 0.24 0.18 0.13 0.10 0.06 0.03
1059.66 470.28 10.45 3.83 2.01 1.23 0.83 0.58 0.43 0.32 0.24 0.18 0.13 0.10 0.06 0.03