I have the code from Magician's Audio Input to Arduino http://coolarduino.wordpress.com/2012/06/22/audio-input-to-arduino/ and I have a problem with it. The timer1 and the audio sampling are correct, but I have this weird "bounce" effect that happens whenever I change the value of the analog input.
I have a switch hooked up to the input now just to change the extremes from 0 - 255, but I know that this "bounce effect" is not caused by the switch itself because the Arduino does this when I change the audio input using a potentiometer.
All I have hooked up is a 10K pulldown resistor and the input from the switch going to pin A0. Here is the code I used. I stripped it down to the bare minimum to isolate the problem. I also have a 8X8 LED Matrix hooked up via 74HC595N shift registers. The problem is not in them though because I have the Arduino Serial output into a processing bar graph, which shows the sudden spikes in the values, which look just like a switch de-bouncing. I think the problem is in my code, but I have absolutely no idea where it could be. I don't think magician's original code is supposed to do this.
#include "TimerOne.h"
#define FFTSIZE 16
#define ROWS 8
int input[FFTSIZE];
float output[FFTSIZE / 2];
float sine;
float cosine;
boolean finished;
static uint8_t n_sampl;
int16_t temp;
int counter;
int k;
int threshold;
int rowCounter;
const int latchPin = 8;
const int dataPin = 12;
const int clkPin = 11;
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clkPin, OUTPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
ADCSRA = 0x87; // freq = 1/128 125 kHz. 13 cycles x 8 usec = 104 usec.
//ADCSRA = 0x86; // freq = 1/64 250 kHz. 13 cycles x 4 usec = 52 usec.
//ADCSRA = 0x85; // freq = 1/32 500 kHz. 13 cycles x 2 usec = 26 usec.
//ADCSRA = 0x84; // freq = 1/16 1 MHz. 13 cycles x 1 usec = 13 usec.
//ADCSRA = 0x83; // freq = 1/8 2 MHz. 13 cycles x 0.5 usec = 6.5 usec.
//ADCSRA = 0x82; // freq = 1/4 4 MHz. 13 cycles x 0.25 usec = 3.25 usec.
ADMUX = 0x40;
ADCSRA |= (1 << ADSC);
calibAudio();
Timer1.initialize(150000);
Timer1.attachInterrupt(audio);
}
void loop() {
LEDDisplay(); // to be filled in later
finished = true;
}
void calibAudio() {
for(counter = 0; counter < 32; counter++) {
if(ADCSRA & 0x10) {
temp = ADCL;
temp += (ADCH << 8);
ADCSRA |= (1 << ADSC);
threshold += temp;
}
}
threshold /= 32;
}
void audio() {
digitalWrite(13, HIGH);
if(finished == true) {
if(ADCSRA & 0x10) {
temp = ADCL;
temp += (ADCH << 8);
ADCSRA |= (1 << ADSC);
input[n_sampl] = temp - threshold;
}
if(++n_sampl >= FFTSIZE) {
n_sampl = 0;
}
finished = false;
}
digitalWrite(13, LOW);
}
void LEDDisplay() {
for(counter = 0; counter < FFTSIZE; counter++) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clkPin, MSBFIRST, B11111111);
shiftOut(dataPin, clkPin, MSBFIRST, 255 - input[counter]);
shiftOut(dataPin, clkPin, MSBFIRST, 255 - input[counter]);
shiftOut(dataPin, clkPin, MSBFIRST, 255);
digitalWrite(latchPin, HIGH);
Serial.println(input[counter]);
}
}
Any help would be great.
Thanks,
Judd