Go Down

Topic: Code causes arduino to not work at all (Read 2 times) previous topic - next topic

nabiul

Jan 14, 2011, 02:33 am Last Edit: Jan 14, 2011, 02:33 am by nabiul Reason: 1
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(realcomponent*realcomponent + imcomponent*imcomponent);
}  

w=0;
while (w<10) {
  Serial.println(magnitude[w],DEC);
  delay(500);
 }

}

PaulS

Code: [Select]
int buffer[4000];
Not even the Mega has room to store 4000 ints.

nabiul

Seriously? I didn't bother doing the math, but I thought that 4000 integers would be under 30kB. That's not a problem, I can decrease the sample size as necessary, the main reason for the large sample is to make sure  the low range frequencies are captured. But that shouldn't be what is causing the problem as the memory isn't written to when the program is compiled.

AWOL

#3
Jan 14, 2011, 08:26 am Last Edit: Jan 14, 2011, 10:40 am by AWOL Reason: 1
Quote
I didn't bother doing the math, but I thought that 4000 integers would be under 30kB


It is, but you can't (easily) store variables in program memory.
Check the difference between program memory (flash) and variable memory (RAM).
Quote
But that shouldn't be what is causing the problem as the
memory isn't written to when the program is compiled.

No, but it is reserving a block of non-existent memory that is also shared by the stack...

[edit] smacks forehead - it is written to, when it is initialised to zero, before 'main' runs!  [/edit]
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

nabiul

Ok so then the problem is that the 4000 int is at the very limits of memory, so that setup works when the code in the main loop is not included, but when it is, setup gets overwritten by the large array?

I'll try halving the array size.

Go Up