analog read in the background

Hi Guys,

I have a microphone connected to my Pro Mini device and I’d like to read the values and save the last X samples in a buffer. Obviously I can do that in the loop function, but I have other things to do there as well, and it would mess up my sampling frequency. It is very important to me to read the analog input with a constant Fs (40 kHz at least). Is there a way to do that in the backgound, outside of the loop funtion?

I tried this, but I hope, there’s a better solution

...

void loop()
{
  for (int i=0; i<SIZE_OF_BUFFER; i++)
    puffer[i] = analogRead(A0);

  ...

}

Thanks,
Adam

t is very important to me to read the analog input with a constant Fs (40 kHz at least)

You'll only be able to do that at reduced resolution (8 bit), if at all. You can use the end-of-conversion interrupt to store the most recent value, and initiate the next read. (the word is "buffer", not "puffer")

"read the analog input with a constant Fs (40 kHz at least). Is there a way to do that in the backgound, outside of the loop funtion? " You can't do that and have time to do much else. The sketch here http://forum.arduino.cc/index.php?topic=180769.0 did 16-bit samples from a fast external SPI interface ADC and stored them to SD card, and that used most of the CPU cycles. Promini only has 2K SRAM, 2048 bytes, 1024 ints. At 40 KHz, that's a 25mS sample. Not much good for audio.

What other things did you want to do?

I can sample over 40 kHz with reduced resolution, that's OK.

I have to send the sampled datas through an A-Weighting filter, but to make it work I need to read the samples very precisely, that's why I can't sample in the loop funtion. I thought maybe there's a way to collect data outside of the loop, so my other functions won't affect the sampling rate. If this is not possible, I'll use my origial code..

It's hard to get good audio quality with a board like the Pro Mini. You might want to take a look at the Teensy boards, they have dedicated hardware and software for audio capture and processing: http://www.pjrc.com/teensy/td_libs_Audio.html

Not only will the bit depth be much higher, will it have less noise, it also has much more processing power for whatever you want to do with your captured data, like Fourier transforms, Constant-Q transforms, A-weighting ...

If you just want to record audio and process it later, an Arduino is not a good choice. Your computer's sound card or even really cheap SD card audio recorders will do a much better job.

Maybe a Raspberry Pi is the best solution: you can connect a cheap USB sound card, and either use the SD card to store the recorded data, or use the fast, multicore processor to process the audio. It has GPIO pins as well, but unlike the Arduino's I/O, they aren't real time programmable ( in a non-real time environment. But if you don't need accurate timing for your other peripherals like sensors or NeoPixels, you probably won't even notice that it's not real time.

Pieter

It has GPIO pins as well, but unlike the Arduino's I/O, they aren't real time programmable.

Of course they are. However Linux is not a real time environment, if you use some other operating system on it you can do what you,like with those pins.

A 40KHz sample rate in the Arduino is not going to happen without messing about with the internal timers, a simple analogRead will give you just under 10K sample rate. To get faster you have to change the clock prescaller, and put it into free running mode and a have an ISR gather your data. Not a lot of time left for doing other stuff like writing it to an SD card.