FFT and additional functions

Hi all,

I’m using FFT to sort out a few frequency bins and perform a pitch match on voice data. All’s working well and good until I try to add my touch-sensor circuit to the same loop (they both work individually, but neither circuit will function if the code is one string). I’m pretty sure it has to do with this part of the code:

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

Because if I move it out of the setup and into the loop with an if-clause I regain the sound function. (but it remains independent of the if-clause, so I think it’s getting stuck within the clause?) I’m figuring if I can reset these values to the non-hijacked versions, i might be able to run both my functions at once. Only I can find reference to what those values might be! SO, I have two questions for you:

  1. Do you think turning these values back to default will work?
  2. Can you tell me how to do that?

Full code follows :o)
Thanks,
Max.

/*
fft_adc_serial.pde
guest openmusiclabs.com 7.7.14
example sketch for testing the fft library.
it takes in data on ADC0 (Analog0) and processes them
with the fft. the data is sent out over the serial
port at 9.6kb.
*/

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

#include <FFT.h> // include the library
int n;
int average;
int touch;
int buzzPin = 2;

void setup() {
  Serial.begin(9600); // use the serial port
  pinMode (buzzPin, OUTPUT);
}

void loop() {

  touch = analogRead(A1);
  
  if (touch <= 50)
  {digitalWrite (buzzPin, HIGH);
  delay(1000); 
  digitalWrite (buzzPin, LOW);
  delay(5);  
  }
  
 if (touch > 50)
{
  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
while(1) 
  { // reduces jitter
    cli();  // UDRE interrupt slows this way down on arduino1.0
    for (int i = 0 ; i < 512 ; 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
    }
    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();
   for (byte i = 0 ; i < 10; i++) { 
   average = (fft_lin_out[2] + fft_lin_out[3] + fft_lin_out[4] + fft_lin_out[5] + fft_lin_out[6] + fft_lin_out[7] + fft_lin_out[8])/7;
   if (fft_lin_out[2] > average && fft_lin_out[2] >= 110)
   {
   tone (9,75,50);
   }
   if (fft_lin_out[3] > average && fft_lin_out[3] >= 110)
   {
   tone (9,112,50);
   }
   if (fft_lin_out[4] > average && fft_lin_out[4] >= 100)
   {
   tone (9,150,50);
   }
   if (fft_lin_out[5] > average && fft_lin_out[5] >= 100)
   {
   tone (9,187,50);
   }
   if (fft_lin_out[6] > average && fft_lin_out[6] >= 90)
   {
   tone (9,225,50);
   }
   if (fft_lin_out[7] > average && fft_lin_out[7] >= 90)
   {
   tone (9,262,50);
   }
   if (fft_lin_out[8] > average && fft_lin_out[8] >= 90)
   {
   tone (9,262,50);
   }
   delay(500);
  }}}}

Please read the sticky post, "How to use this forum - please read," shown at or near the top of the topic listing for each section of the forum. In particular, please note item 7, "If you are posting code or error messages, use "code" tags." Code tags tell the forum software to display the text exactly as you entered it, and to ignore anything that looks like it might be a formatting tag.

The posted code shows a couple of oddities. The line beginning, "int k =" shows a smiley face with sunglasses. I don't think that's standard C++. Also, the line beginning "fft_input " is lacking an array index, and the text becomes italic after that point. I think there's really an "i" in brackets at that location, and that the forum software interprets that as a directive to switch to italics. Anything I might do to reconstruct your code is probably going to be a waste of time, since I can't easily tell what you really intended.

You'd also do well to read the rest of "How to use this forum - please read." In particular, read item 11, "Tips for getting the most out of your post." Your description of the problem basically says, "It doesn't work." It would be more helpful to know what you expected, and what you actually saw.

With regard to your question,

feathersinthread:

  1. Do you think turning these values back to default will work?
  2. Can you tell me how to do that?
  • I think that it could help, but it won't fix everything.
  • A quick way to find out what those values are would be to write a sketch that reads them and prints them to the serial terminal.
    An even better resolution for both questions would be for you to read the datasheet and find out what those paramaters actually do, and determine how you'd like them set. You can access the datasheet from the Uno's product page, here.