Record wave files automatically using millis

Hi everyone,

I have a project of a sound recorder that i let in a specific place for a rather long duration.
The setup consist in microphone and a sd card reader connected to the arduino, a pushbutton to start and stop recording, and a battery (two 3.7v in series) to feed the system.

When pushing the button first, an empty wave file is created on the sd card, and when pushing again the file is completed.

My code is practically ready, but i still have a problem:
When the batteries goes off, all the system crashes down and the wave file is not completed.

I thought about reading the output voltage of the batteries and automatically stop recording at a specific threshold, but it seems kind of uncertain and rather complicated ( the analog read function with a voltage divider lacks precision)

The best solution would be to record one hour files all along. But i'm really not sure how to do it. Can someone tell me how to do that?

Here is the code:

#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 10
TMRpcm audio;
int audiofile = 0;
unsigned long i = 0;
bool recmode = 0;

void setup() {
  pinMode(A0, INPUT);
  pinMode(6, OUTPUT);
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), button, LOW);
  SD.begin(SD_ChipSelectPin);
  audio.CSPin = SD_ChipSelectPin;


}

void loop() {
}

void button() {
  while (i < 300000) {
    i++;
  }
  i = 0;
  if (recmode == 0) {
    recmode = 1;
    audiofile++;
    digitalWrite(6, HIGH);
    switch (audiofile) {
      case 1: audio.startRecording("1.wav", 16000, A0);break;
      case 2: audio.startRecording("2.wav", 16000, A0); break;
      case 3: audio.startRecording("3.wav", 16000, A0); break;
      case 4: audio.startRecording("4.wav", 16000, A0); break;
      case 5: audio.startRecording("5.wav", 16000, A0); break;
      case 6: audio.startRecording("6.wav", 16000, A0); break;
      case 7: audio.startRecording("7.wav", 16000, A0); break;
      case 8: audio.startRecording("8.wav", 16000, A0); break;
      case 9: audio.startRecording("9.wav", 16000, A0); break;
      case 10: audio.startRecording("10.wav", 16000, A0); break;
    }
  }
  else {
    recmode = 0;
    digitalWrite(6, LOW);
    switch (audiofile) {
      case 1: audio.stopRecording("1.wav"); 
      break;
      case 2: audio.stopRecording("2.wav"); break;
      case 3: audio.stopRecording("3.wav"); break;
      case 4: audio.stopRecording("4.wav"); break;
      case 5: audio.stopRecording("5.wav"); break;
      case 6: audio.stopRecording("6.wav"); break;
      case 7: audio.stopRecording("7.wav"); break;
      case 8: audio.stopRecording("8.wav"); break;
      case 9: audio.stopRecording("9.wav"); break;
      case 10: audio.stopRecording("10.wav"); break;
    }
  }
}

If you have any idea how to do this please tell me

Thanks

If you want to record for one hour, note the current time [startTime = millis()] when you begin recording and then check the elapsed time [millis() - startTime]. If the elapsed time if greater than or equal to an hour, stop recording.

I would also move all your functionality out of the button() function since that gets called as an interrupt service routine which should be short and sweet.

I would also not even use interrupts, just check the button state every time through loop().