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:
- Do you think turning these values back to default will work?
- 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);
}}}}