Pages: [1] 2   Go Down
Author Topic: Code causes arduino to not work at all  (Read 1807 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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(smiley-cool;
  }
  
  //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);
  }
 
}
« Last Edit: January 13, 2011, 08:33:57 pm by nabiul » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25730
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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]
« Last Edit: January 14, 2011, 04:40:53 am by AWOL » Logged

"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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25730
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, it is not at the very limits, it is way beyond the limits, and halfway through the next county.
What board are you using?
Even halving will be too much for many boards.
« Last Edit: January 14, 2011, 08:17:13 am by AWOL » Logged

"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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Duemilanove with atmega328.

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25730
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Time to do the math(s):
328 has 2048 bytes of RAM.
A 500 element int array should be safe.
But certainly not 2000.
Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
A 500 element int array should be safe.
The safeness of a 500 element int array depends on what other variables, constant strings, and stack needs you have.

In the code you initially posted, it would seem that 500 is a safe size.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, that was not very clear when I bought it, I thought the MCU did everything in the 30kB space. So the 32kB is for the program only and 2kB is RAM?


I can lower the sample size, but for every halving of sample size, I loose one of the lower central frequencies; from my understanding. So for a 500 element array I will loose 32,64 and 120Hz, I can drop the sample size down even more, but for every lost central frequency, I have to filter the audio signal using an op amp and directly feed the frequency into one of the analog pins and use the bargraph example code to output it on the matrix.

With hmm 6 analog inputs I can get rid of 32,64,125,250,500 and 1000Hz... so that's a sample size of 125 approximately.

If the arduino had 10 inputs I could do this fully by using filters and not need to do the fourier transform.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25730
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
With hmm 6 analog inputs
...but only one analogue converter.
Logged

"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.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17261
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
OK, that was not very clear when I bought it, I thought the MCU did everything in the 30kB space. So the 32kB is for the program only and 2kB is RAM?

Yes, read up on the difference between Harvard Vs Von Neumann CPU design. All AVR processors are of the Harvard type, using separate program and data memories. You PC is using a Von Neumann type processor where program and data share the same memory.

Lefty
« Last Edit: January 14, 2011, 11:09:02 am by retrolefty » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
...but only one analogue converter.

Can you clarify on that? You mean the mcu can only read one analog pin at a time? Isn't that evident from the programming, ie it can only execute one command at a time?
« Last Edit: January 14, 2011, 11:20:28 am by nabiul » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25730
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The six inputs have an analogue mux behind them, feeding a single ADC.
If you want to use all six inputs, the sample rate on any one input will be 1/6th what you woukd get if you stuck to a single input.
'one command at a time' doesn't enter into the problem here - a conversion takes about the time you could execute over 150 'commands'.
Logged

"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.

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3219
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

For what you want, I think some sort of DSP with parallel analog converters would be best. Of course, it depends on how far in the frequency domain you want to go and also at what accuracy.

You have a 10 bit "analog" signal and you'll be using integer math with the 16 bits on an 8 bit machine. This will make it, although faster than using floating point, slower than ideal. But again, this all depends on the frequencies you want to work with and the desired accuracy.

There's a link to a 8-bit FFT library somewhere in the forum that may also be interesting for what you want.
Also, if you want to perform a FFT on 6 signals at the same time, that will further reduce the sampling interval that you can use. Since you need to assure that sample takes place at correct intervals, you'll need to calculate how long the AD takes to sample. Then multiply the number of channels you're using and that will be the lowest sample time you can use.

Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Pages: [1] 2   Go Up
Jump to: