So recently I decided to create a spectrum analyzer using FHT to transform an audio input through a 3.5mm jack to a digital signal with the onboard ADC. I tried using the sample code provided from the ArduinoFHT Wiki, but I can't seem to adjust the range of frequencies. I'm new to this stuff with a little programming experience. I read online that it is easier to use analogRead() instead of ADMUX to change the range of frequencies, but I don't know how. My question is: how do I use analogRead() instead of ADMUX = 0x40?
My Current Code:
#define LIN_OUT8 1 // use the log output function
#define FHT_N 16 // set to 256 point fht
#include <FHT.h> // include the library
void setup() {
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
int analogPin = 0;
int val = 0;
}
void loop() {
while(1) { // reduces jitter
cli(); // UDRE interrupt slows this way down on arduino1.0
for (int i = 0 ; i < FHT_N ; i++) { // save 16 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
fht_input[i] = k; // put real data into bins
}
fht_window(); // window the data for better frequency response
fht_reorder(); // reorder the data before doing the fht
fht_run(); // process the data in the fht
fht_mag_lin8(); // take the output of the fht
sei();
for(int i = 0; i < 16; i++){
Serial.println(fht_lin_out8[i]);
delay(15000);
}
//Serial.println(fht_lin_out8[7]);
}
}
I am using the serial monitor to print the magnitude of the frequency bins at a slow and readable rate. However, most of the magnitudes of my frequency bins are returning as 0. I assume this is because of a frequency range that is too large. In the serial monitor, I get the following:
28
16
0
0
0
0
0
0
0
0
0
0
0
0
0
0
119
65
3
0
0
0
0
0
0
0
0
0
0
0
0
0
119
65
3
0
0
0
0
0
0
0
0
0
0
0
0
0
119
65
3
0
0
0
0
0
0
0
0
0
0
0
0
0
119
65
3
0
0
0
0
0
0
0
0
0
0
0
0
0
119
65
3
0
0
0
0
0
0
0
0
0
0
0
0
0
And it keeps updating. When I place my thumb on a wire plugged into the A0 port on the Arduino Uno, I get the following:
119
65
3
0
0
0
0
0
0
0
0
0
0
0
0
0
84
46
3
0
0
0
0
0
0
0
0
0
0
0
0
0
38
21
0
0
0
0
0
0
0
0
0
0
0
0
0
0
8
6
1
0
0
0
0
0
0
0
0
0
0
0
0
0
50
28
0
0
0
0
0
0
0
0
0
0
0
0
0
0
82
44
1
0
0
0
0
0
0
0
0
0
0
0
0
0
98
54
3
0
0
0
0
0
0
0
0
0
0
0
0
0
110
60
1
1
0
0
0
0
0
0
0
0
0
0
0
0
Can someone please explain what I am doing wrong and how I can fix it?