Sampling at 10 kHz while outputting a 400 kHz square wave

Hello,

I'm attempting to make a cough detector. This requires to record data from a piezo transducer at a high sample rate (10kHz) and save it to an SD card, awhile simultaneously outputting a square wave (50% duty cycle) in order to set the cut off frequency of a hardware low pass filter.

I have managed to do each of these tasks independently; with the data acquisition using Timer 1 and the output square wave using Timer 2.

I now need to combine both these bits of code, i think i need to apply more interrupts in order to be able to use Timer 2 simultaneously. Has anyone had any experience doing anything similar?

For the data acquisition i am using the AnalogBinLogger code produced by Fat16Lib which is part of the SDFat library, the code can be seen here:
https://github.com/greiman/SdFat/blob/master/examples/AnalogBinLogger/AnalogBinLogger.ino

This is the square wave code:

// Setting ip clock to drive anti-aliasing filter

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
  pinMode(3, OUTPUT); // Output pin for OCR2B
 // put your main code here, to run repeatedly:
  TCCR2A = bit(COM2A1) | bit(COM2B1) | bit(WGM21) | bit(WGM20);
  TCCR2B = bit(WGM22) | bit(CS20);
  OCR2A = 40;
  OCR2B = 20;
 
}


void setup() {
  // put your setup code here, to run once:

}

void loop() {
 
}

Is it necessary that the output be software automated? That is to say, do you need to vary it? If not, I would just separate it and hardware automate it with a 555. Super simple and reliable. I guess I'm asking why you need to combine them. Help me understand.

Square waves can be output using the tone() function very easily. Set the tone on a pin and it will continue until stopped by notone().

Then a sampling rate of 10 kHz is slightly more than the standard rate for a freerunning ADC (let it convert and every time a new conversion is ready, taking just over 100 µs, an interrupt is triggered and you can write the data to your SD card - assuming that interface is fast enough to follow).

I was reading through the data sheet of the filter and saw this:

'When using these devices for anti-aliasing or DAC postfiltering, synchronize the DAC (or ADC) and the filter clocks. If the clocks are not synchronized, beat frequencies will alias into the desired passband.'

So i thought i could output the clock required for the filter from the arduino.

If i use a 555 how do i synchronise the arduino ADC and the filter?

I am not aware of any way of synchronising the Arduino ADC to any form of external clock signal.

Ross2511:
I have managed to do each of these tasks independently; with the data acquisition using Timer 1 and the output square wave using Timer 2.

I now need to combine both these bits of code, i think i need to apply more interrupts in order to be able to use Timer 2 simultaneously. Has anyone had any experience doing anything similar?

Since the square wave is whole generated in hardware with timer2, there is nothing difficult, just set up
timer2 and you'll get your squarewave out whatever else is happening on the microcontroller, so long
as nothing else touchs timer2's settings of course.