Seeking Help for my recording project.

Hi. I am very new to Arduino and I am here to seek for help.

I am currently working on a recording machine that record when it reaches some sound level and record for 10 seconds and saves it in SD card.

I have manage to save the first file in SD Card but after first recording, the sensor value become 0 so it can not detect the sound level anymore.

I think I have a problem in the loop but can't figure it out.

This image shows that after recording the sensor value become 0;

Please help me thank you!

#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 10
TMRpcm audio;
int audiofile = 0;
unsigned long i = 0;
bool recmode = 1;
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
int Reset = 4;

void setup() 
{
   Serial.begin(9600);
   pinMode(A0, INPUT);
   digitalWrite(Reset, HIGH);
   pinMode(Reset, OUTPUT);   
   //pinMode(6, OUTPUT);
   //pinMode(2, INPUT_PULLUP);
   //attachInterrupt(0, button, LOW);
   SD.begin(SD_ChipSelectPin);
   audio.CSPin = SD_ChipSelectPin;
}


void loop() 
{
   unsigned long startMillis= millis();  // Start of sample window
   unsigned int peakToPeak = 0;   // peak-to-peak level

   unsigned int signalMax = 0;
   unsigned int signalMin = 1024;

   // collect data for 50 mS
   while (millis() - startMillis < sampleWindow)
   {
      sample = analogRead(0);
      if (sample < 1024)  // toss out spurious readings
      {
         if (sample > signalMax)
         {
            signalMax = sample;  // save just the max levels
         }
         else if (sample < signalMin)
         {
            signalMin = sample;  // save just the min levels
         }
      }
   }
   peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
   double volts = (peakToPeak * 5.0) / 1024;  // convert to volts

   Serial.println(volts);
   //This section is showing the sound level in the serial monitor.



if(volts>0.3){ 
   
       audio.startRecording("1.wav", 16000, A0); 
           recmode ==1;
           delay(10000);
          audio.stopRecording("1.wav");
          recmode ==0;
          Serial.print('Y') ; // print to show the recording is done
          delay(10000);

          }
        

}

You are not setting signalMax and signalMin back to their initial values after the first recording

Ctrl-T in the IDE to fix the indentation of your code.

From the TMRpcm advanced features page:

Recording Audio

Audio recording is still being tested and may not perform as expected.

Specifically, in the file TMRpcm.cpp in the stopRecording() function, you can see this:

void TMRpcm::stopRecording(char *fileName){
...
 ADCSRA = 0;
    ADCSRB = 0;
...
 }
}

The ADCSRA register is where the ADEN flag is set to enable the ADC. The stop function disables the ADC entirely. Since the ADC is enabled in the Arduino's init() function, it stays disabled.

So, this is a bug in the TMRpcm library which you should report.

As a (untested) work-around, you could change the ADCSRA = 0; line to ADCSRA &= ~(_BV(ADATE));

Actually, no, that's not going to work since the interrupt would still be enabled and the pre-scaler values might be different.
It might be better to save the ADCSRA and ADCSRB registers before calling startRecording() and then restoring their values after calling stopRecording(). That saves changing the library and should work better I think.

Really Appreciate your reply but I am very new to coding so I am not sure how to
" better to save the ADCSRA and ADCSRB registers before calling startRecording() and then restoring their values after calling stopRecording()."

SO does it mean I have to change in the TMRpcm.cpp

Void TMRpcm::stopRecording(char *fileName){

	*TIMSK[tt] &= ~(_BV(OCIE1B) | _BV(OCIE1A));
	ADCSRA = 0;
    ADCSRB = 0;

Here changing ADCSRA and ADCSRB values?

Again Thank you so much

Something like this:

  if (volts > 0.3) {

    byte adcsra = ADCSRA;
    byte adcsrb = ADCSRB;

    audio.startRecording("1.wav", 16000, A0);
    recmode = 1;
    delay(10000);
    audio.stopRecording("1.wav");
    recmode = 0;

    ADCSRB = adcsrb;
    ADCSRA = adcsra;

    Serial.print('Y') ; // print to show the recording is done
    delay(10000);
  }

oh wow its actually working
Really appreciate your help
so i can finish this project!
i should search for those register stuff
thank you so much!