Run/Pause a program when a pushbutton is pressed/pressed again(without computer)

Hello everyone.
I'm sorry for the dumb question, but I tried many ways to solve this problem without success.
I'm building a shield to acquisition of bioelectric signals, which will be connected to the person during the day, storing the information (ECG, for example) in a SD card, then be plotted using a Python script.
the user must press a button to start recording, and the same button if you want to interrupt it. The system works perfectly, but only by Serial Monitor (USB). When Arduino is powered by a external source and I try to record, nothing happens.
I know it must be something simple, but did not find any topic or tutorial to solve my problem.
Thanks.
Here is the code:

// Datalogging ECG

#include <SD.h>
#include <SPI.h>         

//SPI Communication with UNO
//MOSI = Pin 11
//MISO = Pin 12
//SCLK = PIN 13
int CS_pin = 4;
int pino_ecg = 1;              // from conditioning circuit
float freq_amostragem = 1000;  // sampling frequency (Hz)
long id = 1;                   //sample

// pushbutton debouncing
int buttonPin = 5;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean rec_On = LOW;





void setup()
{
  Serial.begin(9600);
  
  pinMode(CS_pin, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(pino_ecg, INPUT);

 

  //Initializing Card
    Serial.println("\nInicializando cartao SD...\n");

  if (!SD.begin(CS_pin))
  {
      Serial.println("Erro na inicializacao do cartao.\n");
      return;
  }
  Serial.println("Inicializando realizada com sucesso.\n");
  
  // Acredito que não vá precisar de cabeçalho no arquivo texto:
  
  //File header
  File logFile = SD.open("ECG_log.txt", FILE_WRITE);
  if (logFile)
  {
    logFile.println("   "); //Blank Line
    String header = "id, tensao";
     logFile.println("ID , tensao");
    logFile.close();
    Serial.println("ID , tensao");

  }
  else
  {
    Serial.println("Nao foi possivel abrir o arquivo ECG_log\n");
    return;
  }
  
}

// Define debouncing function

boolean debounce(boolean last){
  boolean current = digitalRead(buttonPin); 
  if(last != current){
    delay(5);
    current = digitalRead(buttonPin);
  }
return current;
}




void loop()
{

  //Check ECG signal
  pino_ecg = analogRead(pino_ecg);
    
  //Creating a string with sample number and ECG voltage
  String stringECG = String(id) + "  " + String(pino_ecg); 

  //Button State
  currentButton = debounce(lastButton);

  if(lastButton == LOW && currentButton == HIGH){
      rec_On = !rec_On;
  }
  lastButton = currentButton;

 . 
 if(rec_On == HIGH){
  

    //Opening the File on SD
    File logFile = SD.open("ECG_log.txt", FILE_WRITE);
    if (logFile)
    {
    
      logFile.println(stringECG);
      logFile.close();
      Serial.println(stringECG);
    }
    else
    {
      Serial.println("Nao foi possivel gravar no arquivo ECG_log\n");
      
    }

    // increasing ID number
    id += 1;

    //Convertendo a frequencia de amostragem em periodo de amostragem [ms]
    float periodo_amostragem = (1 / freq_amostragem)*1000;

    delay(periodo_amostragem);
 }


}
    String header = "id, tensao";
     logFile.println("ID , tensao");

Piss away resources on the String class, and then don't even use the instance. Makes no sense to me.

How IS the switch wired? Why such a complicated way? Use INPUT_PULLUP as the mode, and connect one leg to ground and the other leg to a digital pin.

  pino_ecg = analogRead(pino_ecg);

It makes no sense at all to crap on your pin number.

 if(rec_On == HIGH){

booleans should use true and false, NOT HIGH and LOW.

In addition to what @PaulS has said ...

You seem to have a very raggedy way for reading your button with bits of code in a function and other bits in loop().

Just write a function readButton() which updates the rec_On variable. Then your code in loop() could be reduced to

void loop() {
   currentMillis = millis();
   readButton();
   if (rec_On == true) {
       readAndSaveData();
   }
}

You can see how to use millis() (rather than delay() ) to provide simple debouncing in several things at a time

...R

Then your code in loop() could be reduced to

Why update currentMillis in loop(), when loop() doesn't care?

PaulS:
Why update currentMillis in loop(), when loop() doesn't care?

I just like to take a snapshot of millis() once for every iteration of loop() and use it for all activities within that iteration.

In this case it will probably be needed for the debouncing code.

...R

Here's what it sounds like you're looking to do.. at least to me

int IntervalTime = 100; 

void Loop()
{
   static unsigned int NextIterationTime;
   unsigned int Now = Millis();

   static boolean Run;

   if (Now >= NextIterationTime){
     NextIterationTime = Now+IntervalTime;
    
     if (digitalRead(ButtonPin)==HIGH{
        Run = !Run; 
     }

     if(Run == true){
       DoStuff();

     }
   }
}


void DoStuff(){

}

And if I had the choice, I'd put the button on an interrupt falling edge instead which would eliminate having to poll the button all the time
Also, if you quickly hit the button, it may not register, and if you move the button polling outside of the timed area, it will toggle back and forth too fast

I think this is the better way, assuming you have an available interrupt input

int IntervalTime = 100; 
boolean Run;

void Setup(){
   pinMode(2,INPUT_PULLUP);
   attachInterrrupt(0,ButtonISR,FALLING); //This is interrupt 0, which is usually connected to digital pin 2

}
void Loop()
{
   static unsigned int NextIterationTime;
   unsigned int Now = Millis();

   

   if (Now >= NextIterationTime){
     NextIterationTime = Now + IntervalTime;
    
    if (Run){
       DoStuff();
     }
   }
}


void DoStuff(){

}

void ButtonISR(){
Run = !Run; //toggle the run mode
}

Thanks for all the answers.
I am very beginner in programming, I'm sure that my program is very ugly, so I I thank all the tips you have given me.