Trouble with FFT

Hi, I'm relatively new to programming and I'm having some issues. I'm trying to make an audio spectrum analyser using a 16 x 32 bi colour led matrix and the new fft library. I have written code to send information to my led matrix without too many issues. The problem i am facing is that i don't understand where to access the data that is captured in the bins by the FFT. I also can't seen to find any documentation on what data is actually in the FFT bins or how i can use it for my purpose (at least not written in a way that i understand it). I have downloaded the example code for the FFT as a starting point, but unlike all other example code that i have downloaded for butchering purposes, this FFT code doesn't give me any ideas on how to start with it. Any pointers and hints will be appreciated. TVM

cuthbert_jellyhead:
... the new fft library.

Can you post a link to that library?

Link is here attachment:ArduinoFFT2.zip of ArduinoFFT - Open Music Labs Wiki

My question is why are you using FFT for your audio spectrum analyser?

FFT gives you amplitude (and phase if you care) information for the fundamental frequency you choose to measure at....

AND

all its harmonics up to fs/2....

BUT

We do not perceive sound like this. We do perceive sound exponentially (or logarithmycally depending on how you see it).

What that means is that your upper half of your FFT result is going to be in ONE octave and one (what I call) perception bin.

going down form there the next 1/4th will be in another then 1/8th etc.

So you're doing a lot of computation for not representing your perception intuitively.

You could just use 6-8 IIR filters with a moderate Q (actually 1) and be done in no time.

But if the purpose of your program is to learn about FFT, then of course all this is irrelevant.

cheers and let us know how it turns out.

OK, that's the openmusiclabs FFT library.
Have you written code that uses this library? Can you post it?

yvesbaggi:
My question is why are you using FFT for your audio spectrum analyser?

FFT gives you amplitude (and phase if you care) information for the fundamental frequency you choose to measure at....

AND

all its harmonics up to fs/2....

BUT

We do not perceive sound like this. We do perceive sound exponentially (or logarithmycally depending on how you see it).

What that means is that your upper half of your FFT result is going to be in ONE octave and one (what I call) perception bin.

going down form there the next 1/4th will be in another then 1/8th etc.

So you're doing a lot of computation for not representing your perception intuitively.

You could just use 6-8 IIR filters with a moderate Q (actually 1) and be done in no time.

But if the purpose of your program is to learn about FFT, then of course all this is irrelevant.

cheers and let us know how it turns out.

my goal was to do the analysis using software, rather than hardware filters, and with the little knowledge on the subject that i have i thought FFT was the best way to do this. A quick google of IIR filters brings me some good reading, so thank u very much for that. If there are any particularly useful sites you know of then i would appreciate the links. TVM

tmd3:
OK, that's the openmusiclabs FFT library.
Have you written code that uses this library? Can you post it?

No, i have only written some crude code for me to able to display data on a matrix display, i didn't have a clue o how to adapt the FFT for my purposes, but it seems i don't need FFT, but rather IIR.............the journey continues...

and with the little knowledge on the subject that i have i thought FFT was the best way to do this.

It is.
yvesbaggi - is misleading you.

I'll cast my vote with His Grumpiness. There's nothing wrong with using the FFT for spectrum analysis. That's what it's for.

If you decide to pursue an FFT solution, I'll recommend that you copy the openmusiclabs programming example here: Example - Open Music Labs Wiki, and modify it to operate on known data that you calculate in the sketch, that has a simple and readily identifiable Fourier transform - something like a simple sine function, with a period that divides evenly into FFT_N, the size of the FFT. If you decide to go with the FFT, let us know whether that's within your skillset.

At the same time, I'd recommend that you read about the Fourier transform, or maybe the discrete Fourier transform. You can google things like "Fourier transform" and "layman," and get a plethora of pages that claim to explain it it simple terms. You'll find that simplicity is in the eye of the beholder - some of them will seem obtuse to you, while others will be easier to understand. Remember that the fast Fourier transform is just a special case of the discrete Fourier transform, arranged for quick calculation. So, things that are conceptually true of the discrete Fourier transform are generally also true of the fast Fourier transform.

If you proceed with the Fourier transform and have specific questions about it, ask away.

Awesome, thanks for your advise, I will continue with an FFT solution. It's not currently within my skill set, but nothing is ever in someone's skill set until it's been learned. The road will be a rocky one, but I'm happy to know im actually on the correct road.

cuthbert_jellyhead:
It's not currently within my skill set ...

OK. Take a look at it, contemplate how you'd change it to operate on data that's calculated in the sketch. Try something, and let us know how it worked out. The forum is fairly brimming with people who would be delighted to help, especially if they see you doing actual work.

That example code is peculiar and complicated, and and it touches on concepts that may be quite advanced from your viewpoint. The simplest way to get your calculated data into the FFT is to let the original code establish the value of the next elements of the array fft_input, and then write your own data over them. The code is written for speed, but, in testing, we're not too concerned if you take a few more milliseconds to fill the array.

Note that the example code sets all of the odd-numbered elements of fft_input to zero. You want to be sure to do that, too. Those are the imaginary parts of complex numbers, and, since we we're working with real data, they're zero when we start. The FFT library will modify those values as the calculation progresses, and use their final values to calculate its output.

Here's another hint: The example is intended to transmit the output of the FFT to a separate computer for graphical display, and it's written to do that efficiently. To see the output in the serial terminal, you'll need to change the Serial.write() statements to Serial.print(), and you'll need to change them to print the individual elements of the output array to the terminal.

Thanks. Yeah I'd already changed to Serial.print, and I've got it to crudely (just half the screen, single colour and wrong orientation) display on my led matrix. Also put on a button that measures background noise and eliminates it from the display, so when music is muted it is a completely clean display (I was getting a lot of noise on the display). So it's very much a work in progress. Going to get it correctly orientated on the whole screen and in both colours before cleaning up the output:)

Great, now you have code. Now you can post it.

Before you do, be sure to read the sticky post, "How to use this forum - please read," by Nick Gammon, accessible at the top of the list of posts on any of the forum's many sections. Note that with the recent changes to the forum, the description of how to post code may be out of date - the icon that generates [ code ] tags looks different. See the attached picture.

Yes a lot of noise is to be expected.
Well done with what you have done so far.

tmd3:
Great, now you have code. Now you can post it.

Well this is my code, i know i have gone a long way around doing it, but i first started to learn programming when i taught myself PIC assembly language a few years back. This has left me with some habits that are hard to break. Its a steep learning curve.........at the moment!
This isn't all the code, i had to delete some as I'm only allowed 20000 characters lol

#define LIN_OUT 1 // use the lin output function
#define FFT_N 64 // set to 256 point fft

#include <FFT.h> // include the library
byte test1,test2,test3,test4,test5,test6,test7,test8,test9,test10,test11,test12,test13,test14,test15,test16,test17,test18,test19,test20,test21,test22,test23,test24,test25,test26,test27,test28,test29,test30,test31,test32;
byte del = 600;
byte eth = A2;
byte rdat = 22;
byte gdat = 23;
byte a0 = 24;
byte a1 = 25;
byte a2 = 26;
byte a3 = 27;
byte en = 28;
byte lat = 29;
byte clock = 30;
byte grnd = 31;
byte row;
byte bin[] = {24,25,26,27};
int zero=12;
// the setup function runs once when you press reset or power the board
void setup() {
  
 // Serial.begin(115200); // use the serial port
  TIMSK0 = 0; // turn off timer0 for lower jitter - delay() and millis() killed
  ADCSRA = 0xe5; // set the adc to free running mode
  ADMUX = 0x40; // use adc0
  DIDR0 = 0x01; // turn off the digital input for adc0
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
  pinMode(22, OUTPUT);
  pinMode(23, OUTPUT);
  pinMode(24, OUTPUT);
  pinMode(25, OUTPUT);
  pinMode(26, OUTPUT);
  pinMode(27, OUTPUT);
  pinMode(28, OUTPUT);
  pinMode(29, OUTPUT);
  pinMode(30, OUTPUT);
  pinMode(31, OUTPUT);
  digitalWrite (grnd,LOW);
  digitalWrite (en,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (clock,LOW);
 // raa=01;
  //gpd=128;
  pinMode (eth,OUTPUT);
  digitalWrite(eth,LOW);
  pinMode(zero,INPUT_PULLUP);
  
}

void loop(){
  fft();
  align();
  scan(); 
}

// the loop function runs over and over again forever
void scan() {
  linea();
  lineb();
  linec();
  lined();
  linee();
  linef();
  lineg();
  lineh();
  linei();
  linej();
  linek();
  linel();
  linem();
  linen();
  lineo();
  linep();
  
}

void linea(){
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  digitalWrite(a0,LOW);
  digitalWrite(a1,LOW);
  digitalWrite(a2,LOW);
  digitalWrite(a3,LOW);
  shiftOut(rdat,clock,LSBFIRST,raa);
  shiftOut(rdat,clock,LSBFIRST,rab);
  shiftOut(rdat,clock,LSBFIRST,rac);
  shiftOut(rdat,clock,LSBFIRST,rad);
  digitalWrite(rdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  shiftOut(gdat,clock,LSBFIRST,gaa);
  shiftOut(gdat,clock,LSBFIRST,gab);
  shiftOut(gdat,clock,LSBFIRST,gac);
  shiftOut(gdat,clock,LSBFIRST,gad);
  digitalWrite(gdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
}

void lineb(){
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  digitalWrite(a0,HIGH);
  digitalWrite(a1,LOW);
  digitalWrite(a2,LOW);
  digitalWrite(a3,LOW);
  delayMicroseconds (del);
  shiftOut(rdat,clock,LSBFIRST,rba);
  shiftOut(rdat,clock,LSBFIRST,rbb);
  shiftOut(rdat,clock,LSBFIRST,rbc);
  shiftOut(rdat,clock,LSBFIRST,rbd);
  digitalWrite(rdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  shiftOut(gdat,clock,LSBFIRST,gba);
  shiftOut(gdat,clock,LSBFIRST,gbb);
  shiftOut(gdat,clock,LSBFIRST,gbc);
  shiftOut(gdat,clock,LSBFIRST,gbd);
  digitalWrite(gdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
}

void linec(){
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  digitalWrite(a0,LOW);
  digitalWrite(a1,HIGH);
  digitalWrite(a2,LOW);
  digitalWrite(a3,LOW);
  delayMicroseconds (del);
  shiftOut(rdat,clock,LSBFIRST,rca);
  shiftOut(rdat,clock,LSBFIRST,rcb);
  shiftOut(rdat,clock,LSBFIRST,rcc);
  shiftOut(rdat,clock,LSBFIRST,rcd);
  digitalWrite(rdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  shiftOut(gdat,clock,LSBFIRST,gca);
  shiftOut(gdat,clock,LSBFIRST,gcb);
  shiftOut(gdat,clock,LSBFIRST,gcc);
  shiftOut(gdat,clock,LSBFIRST,gcd);
  digitalWrite(gdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
}

void lined(){
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  digitalWrite(a0,HIGH);
  digitalWrite(a1,HIGH);
  digitalWrite(a2,LOW);
  digitalWrite(a3,LOW);
 // delayMicroseconds (del);
  shiftOut(rdat,clock,LSBFIRST,rda);
  shiftOut(rdat,clock,LSBFIRST,rdb);
  shiftOut(rdat,clock,LSBFIRST,rdc);
  shiftOut(rdat,clock,LSBFIRST,rdd);
  digitalWrite(rdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  shiftOut(gdat,clock,LSBFIRST,gda);
  shiftOut(gdat,clock,LSBFIRST,gdb);
  shiftOut(gdat,clock,LSBFIRST,gdc);
  shiftOut(gdat,clock,LSBFIRST,gdd);
  digitalWrite(gdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
}

void linee(){
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  digitalWrite(a0,LOW);
  digitalWrite(a1,LOW);
  digitalWrite(a2,HIGH);
  digitalWrite(a3,LOW);
//  delayMicroseconds (del);
  shiftOut(rdat,clock,LSBFIRST,rea);
  shiftOut(rdat,clock,LSBFIRST,reb);
  shiftOut(rdat,clock,LSBFIRST,rec);
  shiftOut(rdat,clock,LSBFIRST,red);
  digitalWrite(rdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  shiftOut(gdat,clock,LSBFIRST,gea);
  shiftOut(gdat,clock,LSBFIRST,geb);
  shiftOut(gdat,clock,LSBFIRST,gec);
  shiftOut(gdat,clock,LSBFIRST,ged);
  digitalWrite(gdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
}

void linef(){
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  digitalWrite(a0,HIGH);
  digitalWrite(a1,LOW);
  digitalWrite(a2,HIGH);
  digitalWrite(a3,LOW);
 // delayMicroseconds (del);
  shiftOut(rdat,clock,LSBFIRST,rfa);
  shiftOut(rdat,clock,LSBFIRST,rfb);
  shiftOut(rdat,clock,LSBFIRST,rfc);
  shiftOut(rdat,clock,LSBFIRST,rfd);
  digitalWrite(rdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  shiftOut(gdat,clock,LSBFIRST,gfa);
  shiftOut(gdat,clock,LSBFIRST,gfb);
  shiftOut(gdat,clock,LSBFIRST,gfc);
  shiftOut(gdat,clock,LSBFIRST,gfd);
  digitalWrite(gdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
}

void lineg(){
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  digitalWrite(a0,LOW);
  digitalWrite(a1,HIGH);
  digitalWrite(a2,HIGH);
  digitalWrite(a3,LOW);
//  delayMicroseconds (del);
  shiftOut(rdat,clock,LSBFIRST,rga);
  shiftOut(rdat,clock,LSBFIRST,rgb);
  shiftOut(rdat,clock,LSBFIRST,rgc);
  shiftOut(rdat,clock,LSBFIRST,rgd);
  digitalWrite(rdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  shiftOut(gdat,clock,LSBFIRST,gga);
  shiftOut(gdat,clock,LSBFIRST,ggb);
  shiftOut(gdat,clock,LSBFIRST,ggc);
  shiftOut(gdat,clock,LSBFIRST,ggd);
  digitalWrite(gdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
}

void lineh(){
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  digitalWrite(a0,HIGH);
  digitalWrite(a1,HIGH);
  digitalWrite(a2,HIGH);
  digitalWrite(a3,LOW);
//  delayMicroseconds (del);
  shiftOut(rdat,clock,LSBFIRST,rha);
  shiftOut(rdat,clock,LSBFIRST,rhb);
  shiftOut(rdat,clock,LSBFIRST,rhc);
  shiftOut(rdat,clock,LSBFIRST,rhd);
  digitalWrite(rdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  shiftOut(gdat,clock,LSBFIRST,gha);
  shiftOut(gdat,clock,LSBFIRST,ghb);
  shiftOut(gdat,clock,LSBFIRST,ghc);
  shiftOut(gdat,clock,LSBFIRST,ghd);
  digitalWrite(gdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
}

void linei(){
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  digitalWrite(a0,LOW);
  digitalWrite(a1,LOW);
  digitalWrite(a2,LOW);
  digitalWrite(a3,HIGH);
 // delayMicroseconds (del);
  shiftOut(rdat,clock,LSBFIRST,ria);
  shiftOut(rdat,clock,LSBFIRST,rib);
  shiftOut(rdat,clock,LSBFIRST,ric);
  shiftOut(rdat,clock,LSBFIRST,rid);
  digitalWrite(rdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  shiftOut(gdat,clock,LSBFIRST,gia);
  shiftOut(gdat,clock,LSBFIRST,gib);
  shiftOut(gdat,clock,LSBFIRST,gic);
  shiftOut(gdat,clock,LSBFIRST,gid);
  digitalWrite(gdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
}

void linej(){
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  digitalWrite(a0,HIGH);
  digitalWrite(a1,LOW);
  digitalWrite(a2,LOW);
  digitalWrite(a3,HIGH);
 // delayMicroseconds (del);
  shiftOut(rdat,clock,LSBFIRST,rja);
  shiftOut(rdat,clock,LSBFIRST,rjb);
  shiftOut(rdat,clock,LSBFIRST,rjc);
  shiftOut(rdat,clock,LSBFIRST,rjd);
  digitalWrite(rdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  shiftOut(gdat,clock,LSBFIRST,gja);
  shiftOut(gdat,clock,LSBFIRST,gjb);
  shiftOut(gdat,clock,LSBFIRST,gjc);
  shiftOut(gdat,clock,LSBFIRST,gjd);
  digitalWrite(gdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
}
void linek(){
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  digitalWrite(a0,LOW);
  digitalWrite(a1,HIGH);
  digitalWrite(a2,LOW);
  digitalWrite(a3,HIGH);
 // delayMicroseconds (del);
  shiftOut(rdat,clock,LSBFIRST,rka);
  shiftOut(rdat,clock,LSBFIRST,rkb);
  shiftOut(rdat,clock,LSBFIRST,rkc);
  shiftOut(rdat,clock,LSBFIRST,rkd);
  digitalWrite(rdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  shiftOut(gdat,clock,LSBFIRST,gka);
  shiftOut(gdat,clock,LSBFIRST,gkb);
  shiftOut(gdat,clock,LSBFIRST,gkc);
  shiftOut(gdat,clock,LSBFIRST,gkd);
  digitalWrite(gdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
}


void linel(){
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  digitalWrite(a0,HIGH);
  digitalWrite(a1,HIGH);
  digitalWrite(a2,LOW);
  digitalWrite(a3,HIGH);
//  delayMicroseconds (del);
  shiftOut(rdat,clock,LSBFIRST,rla);
  shiftOut(rdat,clock,LSBFIRST,rlb);
  shiftOut(rdat,clock,LSBFIRST,rlc);
  shiftOut(rdat,clock,LSBFIRST,rld);
  digitalWrite(rdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  shiftOut(gdat,clock,LSBFIRST,gla);
  shiftOut(gdat,clock,LSBFIRST,glb);
  shiftOut(gdat,clock,LSBFIRST,glc);
  shiftOut(gdat,clock,LSBFIRST,gld);
  digitalWrite(gdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
}

void linem(){
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  digitalWrite(a0,LOW);
  digitalWrite(a1,LOW);
  digitalWrite(a2,HIGH);
  digitalWrite(a3,HIGH);
//  delayMicroseconds (del);
  shiftOut(rdat,clock,LSBFIRST,rma);
  shiftOut(rdat,clock,LSBFIRST,rmb);
  shiftOut(rdat,clock,LSBFIRST,rmc);
  shiftOut(rdat,clock,LSBFIRST,rmd);
  digitalWrite(rdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  shiftOut(gdat,clock,LSBFIRST,gma);
  shiftOut(gdat,clock,LSBFIRST,gmb);
  shiftOut(gdat,clock,LSBFIRST,gmc);
  shiftOut(gdat,clock,LSBFIRST,gmd);
  digitalWrite(gdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
}

void linen(){
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  digitalWrite(a0,HIGH);
  digitalWrite(a1,LOW);
  digitalWrite(a2,HIGH);
  digitalWrite(a3,HIGH);
 // delayMicroseconds (del);
  shiftOut(rdat,clock,LSBFIRST,rna);
  shiftOut(rdat,clock,LSBFIRST,rnb);
  shiftOut(rdat,clock,LSBFIRST,rnc);
  shiftOut(rdat,clock,LSBFIRST,rnd);
  digitalWrite(rdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  shiftOut(gdat,clock,LSBFIRST,gna);
  shiftOut(gdat,clock,LSBFIRST,gnb);
  shiftOut(gdat,clock,LSBFIRST,gnc);
  shiftOut(gdat,clock,LSBFIRST,gnd);
  digitalWrite(gdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
}

void lineo(){
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  digitalWrite(a0,LOW);
  digitalWrite(a1,HIGH);
  digitalWrite(a2,HIGH);
  digitalWrite(a3,HIGH);
 // delayMicroseconds (del);
  shiftOut(rdat,clock,LSBFIRST,roa);
  shiftOut(rdat,clock,LSBFIRST,rob);
  shiftOut(rdat,clock,LSBFIRST,roc);
  shiftOut(rdat,clock,LSBFIRST,rod);
  digitalWrite(rdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  shiftOut(gdat,clock,LSBFIRST,goa);
  shiftOut(gdat,clock,LSBFIRST,gob);
  shiftOut(gdat,clock,LSBFIRST,goc);
  shiftOut(gdat,clock,LSBFIRST,god);
  digitalWrite(gdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
}

void linep(){
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  digitalWrite(a0,HIGH);
  digitalWrite(a1,HIGH);
  digitalWrite(a2,HIGH);
  digitalWrite(a3,HIGH);
  //delayMicroseconds (del);
  shiftOut(rdat,clock,LSBFIRST,rpa);
  shiftOut(rdat,clock,LSBFIRST,rpb);
  shiftOut(rdat,clock,LSBFIRST,rpc);
  shiftOut(rdat,clock,LSBFIRST,rpd);
  digitalWrite(rdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
  shiftOut(gdat,clock,LSBFIRST,gpa);
  shiftOut(gdat,clock,LSBFIRST,gpb);
  shiftOut(gdat,clock,LSBFIRST,gpc);
  shiftOut(gdat,clock,LSBFIRST,gpd);
  digitalWrite(gdat,LOW);
  digitalWrite (lat,LOW);
  digitalWrite (en,LOW);
  delayMicroseconds (del);
  digitalWrite (en,HIGH);
  digitalWrite (lat,HIGH);
}

 


void fft() {
  while(1) { // reduces jitter
    cli();  // UDRE interrupt slows this way down on arduino1.0
    for (int i = 0 ; i < 128 ; i += 2) { // save 256 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
      fft_input[i] = k; // put real data into even bins
      fft_input[i+1] = 0; // set odd bins to 0
    }
    // window data, then reorder, then run, then take output
    fft_window(); // window the data for better frequency response
    fft_reorder(); // reorder the data before doing the fft
    fft_run(); // process the data in the fft
    fft_mag_lin(); // take the output of the fft
    sei(); // turn interrupts back on
 //   Serial.print(255); // send a start byte
   // Serial.write(fft_lin_out, 128); // send out the data
    test1=fft_lin_out[2];
    test2=fft_lin_out[4];
    test3=fft_lin_out[6];
    test4=fft_lin_out[8];
    test5=fft_lin_out[10];
    test6=fft_lin_out[12];
    test7=fft_lin_out[14];
    test8=fft_lin_out[16];
    test9=fft_lin_out[18];
    test10=fft_lin_out[20];
    test11=fft_lin_out[22];
    test12=fft_lin_out[24];
    test13=fft_lin_out[26];
    test14=fft_lin_out[28];
    test15=fft_lin_out[30];
    test16=fft_lin_out[32];
    test17=fft_lin_out[34];
    test18=fft_lin_out[36];
    test19=fft_lin_out[38];
    test20=fft_lin_out[40];
    test21=fft_lin_out[42];
    test22=fft_lin_out[44];
    test23=fft_lin_out[46];
    test24=fft_lin_out[48];
    test25=fft_lin_out[50];
    test26=fft_lin_out[52];
    test27=fft_lin_out[54];
    test28=fft_lin_out[56];
    test29=fft_lin_out[58];
    test30=fft_lin_out[60];
    test31=fft_lin_out[62];
    test32=fft_lin_out[64];
  /*  Serial.print(test1);
    Serial.print(" ");
    Serial.print(test2);
    Serial.print(" ");
    Serial.print(test3);
    Serial.print(" ");
    Serial.print(test4);
    Serial.print(" ");
    Serial.print(test5);
    Serial.print(" ");
    Serial.print(test6);
    Serial.print(" ");
    Serial.print(test7);
    Serial.print(" ");
    Serial.print(test8);
    Serial.print(" ");
    Serial.print(test9);
    Serial.print(" ");
    Serial.print(test10);
    Serial.print(" ");
    Serial.print(test11);
    Serial.print(" ");
    Serial.print(test12);
    Serial.print(" ");
    Serial.print(test13);
    Serial.print(" ");
    Serial.print(test14);
    Serial.print(" ");
    Serial.print(test15);
    Serial.print(" ");
    Serial.print(test16);
    Serial.print(" ");
    Serial.print(test17);
    Serial.print(" ");
    Serial.print(test18);
    Serial.print(" ");
    Serial.print(test19);
    Serial.print(" ");
    Serial.print(test20);
    Serial.print(" ");
    Serial.print(test21);
    Serial.print(" ");
    Serial.print(test22);
    Serial.print(" ");
    Serial.print(test23);
    Serial.print(" ");
    Serial.print(test24);
    Serial.print(" ");
    Serial.print(test25);
    Serial.print(" ");
    Serial.print(test26);
    Serial.print(" ");
    Serial.print(test27);
    Serial.print(" ");
    Serial.print(test28);
    Serial.print(" ");
    Serial.print(test29);
    Serial.print(" ");
    Serial.print(test30);
    Serial.print(" ");
    Serial.print(test31);
    Serial.print(" ");
    Serial.println(test32);*/
    ifzero();
    align();
    scan();
 }
  
}


void align(){
  
  raa = test1-off1; 
  rab = test2-off2; 
  rac = test3-off3; 
  rad = test4-off4; 
  rba = test5-off5; 
  rbb = test6-off6;
  rbc = test7-off7;
  rbd = test8-off8;
  rca = test9-off9;
  rcb = test10-off10;
  rcc = test11-off11;
  rcd = test12-off12;
  rda = test13-off13;
  rdb = test14-off14;
  rdc = test15-off15;
  rdd = test16-off16;
  rea = test17-off17;
  reb = test18-off18;
  rec = test19-off19;
  red = test20-off20;
  rfa = test21-off21;
  rfb = test22-off22;
  rfc = test23-off23;
  rfd = test24-off24;
  rga = test25-off25;
  rgb = test26-off26;
  rgc = test27-off27;
  rgd = test28-off28;
  rha = test29-off29;
  rhb = test30-off30;
  rhc = test31-off31;
  rhd = test32-off32;
  
}
  
  void ifzero(){
   if (digitalRead(zero)==0){
     off1=test1;
     off2=test2;
     off3=test3;
     off4=test4;
     off5=test5;
     off6=test6;
     off7=test7;
     off8=test8;
     off9=test9;
     off10=test10;
     off11=test11;
     off12=test12;
     off13=test13;
     off14=test14;
     off15=test15;
     off16=test16;
     off17=test17;
     off18=test18;
     off19=test19;
     off20=test20;
     off21=test21;
     off22=test22;
     off23=test23;
     off24=test24;
     off25=test25;
     off26=test26;
     off27=test27;
     off28=test28;
     off29=test29;
     off30=test30;
     off31=test31;
     off32=test32;
   }
    
    
  }

That program would be about 1/10 the size if you were to use arrays, instead of numbered variables.

jremington:
That program would be about 1/10 the size if you were to use arrays, instead of numbered variables.

I know, and I can struggle through writing one (only just), but I just can't seem to problem solve in the presence of arrays! I'm sure it will come in time tho!

i had to delete some as I'm only allowed 20000 characters

You can add an attachment with all your code in it from the reply box (not quick reply)

From a quick look I would say that you desperately need to learn how to use arrays.

http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

Grumpy_Mike:
From a quick look I would say that you desperately need to learn how to use arrays.

Yep, I completely agree. It's a work in progress, albeit a slow progress!

For example, you can replace all of this:

   //   Serial.print(255); // send a start byte
   // Serial.write(fft_lin_out, 128); // send out the data
    test1=fft_lin_out[2];
    test2=fft_lin_out[4];

 <snip out 30-odd lines>

    test31=fft_lin_out[62];
    test32=fft_lin_out[64];
  /*  Serial.print(test1);
    Serial.print(" ");
    Serial.print(test2);

 <snip out 30-odd lines>

    Serial.print(" ");
    Serial.print(test31);
    Serial.print(" ");
    Serial.println(test32);

with this:

 for(int i=2; i<33; i+=2) {
 Serial.print(" ");
 Serial.print(fft_lin_out[i]);
 }
 Serial.println("");