Hello everyone, please tell me, I'm new to Arduino, I need to read data from an electret microphone during a timer interrupt, I found the TimerInterrupt_Generic.h library, tried to implement it with it, but nothing works, I understand the program just hangs. Here is a piece of my code:
NRF52_MBED_Timer ITimer0(NRF_TIMER_3);
void startRecording() {
recording = true;
startTime = millis();
String newFileName = generateFileName();
Serial.println(newFileName);
myFile = SD.open("/samples/" + path + newFileName + ".wav", FILE_WRITE);
path = "";
writeWavHeader();
ITimer0.attachInterruptInterval(1000000 / 16000, timerISR);
}
void timerISR() {
int micValue = analogRead(MIC_PIN);
int16_t audioSample = map(micValue, 0, 1023, -32768, 32767);
if (useSampleBuffer1) {
sampleBuffer1[sampleBufferIndex] = audioSample;
if (++sampleBufferIndex >= BUFFER_SIZE) {
sampleBuffer1Full = true;
sampleBufferIndex = 0;
useSampleBuffer1 = false;
}
} else {
sampleBuffer2[sampleBufferIndex] = audioSample;
if (++sampleBufferIndex >= BUFFER_SIZE) {
sampleBuffer2Full = true;
sampleBufferIndex = 0;
useSampleBuffer1 = true;
}
}
}
I checked and the hang is already happening on this line:
int micValue = analogRead(MIC_PIN);
I understand that interrupts are designed for short operations, but it is during the interruption that I need to read the value from the microphone and fill one of the two buffers, because I need to record data at a certain time interval, otherwise my data from the microphone will be recorded more often than necessary and as a result, the recording turns out to be incorrect if someone- anyone knows how to do this, please tell me, because I can't find anything about it. I saw a similar question on the forum and they wrote about low-level code, but I didn't understand how to do it anyway, thanks in advance, kind people