ADC - 16MHz Clock for Sample Timer

Hi,

I want to create an ADC using the arduino. I am setting the reference voltage to 0.5V to have a 10 bit resolution for a line-in audio signal. The signal will be coming into an analog pin that reads the value and stores it into an array. The time at which that sample was taken will also be stored into a different array. These values will be processed in pure data. All the arrays and setups have been completed before the adc function is called.

Heres my problem...

I know how to use a for loop, and reading values from the input into an array (I'm an intermediate C programmer). My sampling frequency is 40KHz which means the sample interval will be in the microseconds region. How can I go about writing an adc function that samples the time and amplitude values with a 25us interval? I'm assuming that there will need to be a delay between the analogRead() and storing operations.

Thanks for any help in advanced.

Some suggestions: - you need DC offset to sample audio; - "for" loop is not right approach in your application, as you noticed it getting too complicated (practically impossible) to calculated timing for one cycle of the loop, which would include analogRead, store sample, some math, overhead; - better way is interrupt driven application, using Timer or ADC own interrupt "conversion completed"; - you don't need a timestamp for regular equally distanced samples in second array;

Look here: http://interface.khm.de/index.php/lab/experiments/arduino-realtime-audio-processing/ or here: http://fftarduino.blogspot.com/2011/02/color-organ-spectrum-analyzer-on.html

Ah of course I don't need a timestamp haha bit silly of me. I also completely forgotten about offsetting the input. Thanks for the heads up on that, saved me some debugging.

Sorry to be a nuisance but I don't really understand the idea of interrupt, it's something I haven't covered before. Is this done in a low level assembler language?

There are libraries to set Timer/Interrupt : http://arduino.cc/en/Reference/Libraries Metro and MsTimer2. AFAIK, no libraries for ADC "conversion complete". Anyway, you can't sample much with arduino memory constrain, what you gonna to do with array after that? I mean, it can't be real time application with 40 ksps, (may be 16 ksps at max) , so "for" loop could be in use to sample "periodically" , just not very accurate.