I am trying to make an audio spectrum analyzer using fourier transformation and for some reason the code in my main loop causes the microcontroller to do nothing at all. I mean the setup code is not even read, I included some println and output to an LED matrix to check if the setup code was executed and nothing happens. However if I put the code inside the main loop inside comment brackets /* */ so that it is not read, the setup code is executed and i get an output on the serial monitor and the LED turns on.
What could be causing this? Also are there any non java based compilers for arduino? The existing one is extremely unstable and prone to infuriating graphical errors.
The code:
const int analogPin = A0;
const int ledCount = 10;
int ledPins[] = {
2, 3, 4, 5, 6, 7,8,9,10 };
int buffer[4000];
float realcomponent;
float imcomponent;
float magnitude[10];
float angularfreq[] = {201.0,389.6,785.4,1571,3141.0,6283.0,12566.0,25133.0,50265.0,100531.0};
void setup() {
Serial.begin(9600);
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
//checks if setup code is working
Serial.println(1,DEC);
//turns on first led in matrix to show that setup has completed, redundant
digitalWrite(2,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
digitalWrite(8,LOW);
digitalWrite(7,LOW);
digitalWrite(6,LOW);
digitalWrite(5,LOW);
digitalWrite(4,LOW);
digitalWrite(3,LOW);
}
void loop() {
//shows if loop is working
Serial.println(1,DEC);
int n=0;
// take a sample of the audio signal 1/31.25 of a second long
while (n<4000) {
Serial.println(1,DEC);
buffer[n] = analogRead(analogPin);
delayMicroseconds(8);
}
//shows if sample obtaining loop completed
Serial.println(1,DEC);
//perform the fourier transform and obtain magnitude at each frequency
n=0;
int w=0;
realcomponent=0.0;
imcomponent=0.0;
while (w<10) {
while (n<4000) {
realcomponent= ((8E-6)*(float)buffer[n]cos(angularfreq[w](n+1)8E-6))+realcomponent;
imcomponent= ((8E-6)(float)buffer[n]sin(angularfreq[w](n+1)*8E-6))+imcomponent;
n=n+1;
}
w=w+1;
magnitude[w]= sqrt(realcomponentrealcomponent + imcomponentimcomponent);
}
w=0;
while (w<10) {
Serial.println(magnitude[w],DEC);
delay(500);
}
}