ADC Sampling Rate

@Garrett, as @ScruffR pointed out, you include the SparkIntervalTimer.h file but you don’t actually use any portion of that library. Also, I agree with his suggested fix for the for-loop as your index will go out of bounds since the array is declared as microMonitor[49], with an index range of 0 to 48.

Your loop() code has two elements that slow it down. The first being tcpPrint() and and the second being the system firmware background task which runs whenever loop() completes. As @ScruffR pointed out, that can take less than 1ms to possibly 5ms depending on the SYSTEM_MODE() and the status of the wifi/cloud connection.

High speed ADC sampling has to be decoupled from any “uncontrolled” timing conditions to work. As such, you can use hardware timer interrupts (via SparkIntervalTimer) coupled with a sampling queue/buffer that gets “serviced” in loop(). It can be a fixed buffer that once it is filled, ADC sampling stops until the buffer is serviced or a circular buffer which holds the latest N samples. This allows continuous sampling at the expense of possible loss of contiguous samples if loop() is not servicing the buffer fast enough.

I believe there are topics dealing with ADC sampling so a Community search is recommended. :grinning:

1 Like

Thanks @peekay123 you are definitely correct on both accounts, I removed the SparkIntervalTimer.h library before pasting in my code. You are also correct about the calls to the Spark Cloud slowing down user code within the main loop.

I followed your advice (and as you mentioned, advice in other threads as well) and was able to setup two SparkIntervalTimers on TIMER4 and TIMER7 at 20uS intervals to sample the ADC. One function samples and stores stereo microphone input, the other function compares the audio signals against a threshold to detect a step function. Once the step function is detected on both microphones (approx ~50dBA) then I perform Time Difference of Arrival to find the degree of arrival of the audio signal.

Right now I wrote architecturally poor code triggered off the second timer (although it currently works) that has too much logic in the interrupt functions, but I will solve this when I move on to doing convolution of two (stereo microphones) circular buffers.

Thanks again both of you, I apologize for the array-index-out-of-bounds code. That code was quickly thrown in to demonstrate the sampling speed problem I was trying to solve.

3 Likes