Empty while loops

Hey all, I'm busy making a small program that interacts with an SD. A part of it uses the card detect to alert the user when the card is taken out. If this occurs, the program should wait until the interrupt triggers again and sets the error variable to false. The problem is that the while loop is just skipped, which can cause problems later on. Is there a way to simply have the arduino be stuck in a loop until the loop condition is false?

Here's the code that I've tried using so far:

  if(error == true)
  {
    Serial.println("CARD DETECT ERROR, PLEASE INSERT CARD");  // print error on serial monitor (card has been removed)
    
    while(error == true)  // this loop gets skipped, even with a delay(1) in it
    {
      delay(1);
    }
  }

void detect(){
  error = digitalRead(CD);
}

Where the void detect is my ISR for a change in the pin state of CD. Now it just keeps spamming the error message on the serial monitor until a card is inserted, which means that it just keeps skipping the while loop.

Any help is appreciated

You should post your entire sketch, but I suspect you don't have the error variable declared with the volatile keyword.

ToddL1962:
You should post your entire sketch, but I suspect you don’t have the error variable declared with the volatile keyword.

Here’s all I currently have, although it’s not finished yet.

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

#define SSPin 23
#define RST   22
#define CD    2   // INT1
#define TXTO  19
#define TXTI  18
#define DEL   20

volatile bool error = false;
unsigned int maxpos = 0;
File myFile;

void detect();

void setup() {
  SD.begin(SSPin);

  attachInterrupt(digitalPinToInterrupt(CD), detect, CHANGE);

  pinMode(RST, OUTPUT);
  pinMode(CD,  INPUT);

  pinMode(TXTO, INPUT);
  pinMode(TXTI, INPUT);
  pinMode(DEL,  INPUT);
  
  Serial.begin(9600);
}

void loop() {

  if(PINF & 0b01000000)
  {
    myFile = SD.open("Test0.txt", FILE_WRITE);  // open file for reading and writing, beginning at end of file
    maxpos = myFile.position(); // file begins on end
    if(myFile)
    {
      myFile.seek(0);
      Serial.println(" ");
      Serial.print(myFile.name());
      Serial.print("\t");
      while(myFile.available())
      {
        Serial.write(myFile.read());
      }

      myFile.seek(maxpos);
      myFile.println("De test is gelukt!");
    }

    else
    {
      Serial.print("Error opening file");
    }

    myFile.close();
    delay(500);
    // display name + value of file 1 on serial monitor
  }

  if(TXTI)
  {
    // display name + value of file 2 on serial monitor
  }
  
  if(DEL)
  {
    // delete file 1 first, then 2, then 3, display OK on serial monitor if it worked
  }

  if(error == true)
  {
    Serial.println("CARD DETECT ERROR, PLEASE INSERT CARD");  // print error on serial monitor (card has been removed)
    
    while(true)  // this loop gets skipped, even with a delay(1) in it
    {
      if(error == false)
      {
        break;
      }
    }
  }
}

void detect(){
  error = digitalRead(CD);
}

Is there a way to simply have the arduino be stuck in a loop until the loop condition is false

while (thisTest == true)
  {
    //code here to change the value of thisTest to false when something occurs
  }

UKHeliBob:

while (thisTest == true)

{
    //code here to change the value of thisTest to false when something occurs
  }

Although I appreciate your quick response, I'm doing the code to change the value of the variable in the ISR. I'm asking if I can keep it in the ISR instead of putting it in the loop

Do NOT attempt to keep the program in an ISR if that is what you have in mind. An ISR should be as quick as possible. Change the value of a variable, maybe save the time then exit

One primary purpose of using an interrupt is to allow code to run freely so that it can do other things. You could, of course, have the ISR change the value of the thisTest variable (declare it volatile) and thus exit a while loop in the main code but this would still be regarded as bad practice, particularly as the loop() function is designed to do exactly what its name implies so you probably don't need the while loop in the first place