Yes/No Confirmation, wait for button press

Hi all,

I am working on a data logger that writes to an SD-card.
Before starting the logging, the program should verify if the file already exists. If so, it should ask me whether to erase it or not and wait for that keypress.
What would be the best way to implement this? I have worked out a solution, but it seems kludgier than needed (and checks if the file exists a second time and then erasing it when Y is pressed).

Thank you very much for any hints.

//check if file exists, ask whether to overwrite or exit function
if (SD.exists(filename)){
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.print("File exists.");
  lcd.setCursor(5,1);
  lcd.print("Overwrite?");
  lcd.setCursor(0,2);
  lcd.print("Y:Return / N:Escape");
  
  while(digitalRead(NO_BUTTON_PIN)==HIGH && digitalRead(YES_BUTTON_PIN)==HIGH){
    delay(100);
  }
  if(digitalRead(NO_BUTTON_PIN)==LOW){
    Serial.println("Escape, exit");
    return;
  }
  Serial.println("Continue, overwrite file");
  lcd.clear();
  lcd.setCursor(2,0);
  lcd.print("Deleting File");
  delay(2000);
}
//as it needs to pass above code to come here, verify again if file exists and delete
if (SD.exists(filename)){
  SD.remove(filename);
}

//..... datalogging routine......
while(digitalRead(NO_BUTTON_PIN)==HIGH && digitalRead(YES_BUTTON_PIN)==HIGH){
    delay(100);
  }

Why do you care how many times the loop iterates? Does it make any difference if it loops 100 times vs. 100000000 times? Lose the stupid delay();.

I have worked out a solution, but it seems kludgier than needed

It isn't (except for the delay() call).

and checks if the file exists a second time and then erasing it when Y is pressed

Why? You already know that the file exists. There is no need to check again.

Hi PaulS,

thank you very much for your help.
I moved the line "remove" in the verification instead of after; my second check was indeed unneccessary.