Is there a way to stop a program after a specific event???

So I have my code working where I'm happy with the exception that I need a way to stop the program after an event.

In this case I am recording to SD when analog value is over 500 and when under 500 I retrieve the data via Bluetooth. The issue I have is after the data on the SD has been sent the program starts writing again through the loop. How do I make it stop? I tried a delay of 10 minutes just so it gives time to turn it off. But I would much prefer a situation where

if the value is below threshold
write sd through bluetooth
AND STOP HERE..

else if the value is above threshold
run loop continuously until turned off.

I've looked all over the place for how I could do this. I tried while statements and if statements, I just cant find a way for it to stop other than a really long delay....which isn't really stopped

I tried while statements and if statements

Try:

  if(condition == true)
  {
    while (1)         //AND STOP HERE..
    {
    }
  }

Where is your code?

Well adding the following statement where ever you wish in your sketch will create an endless loop which is as effective as a true stop. The AVR chip has no halt instruction so a endless loop will serve just as well. Interrupts will still be processed which may or may not be an issue in your sketch.

while (1) { }  // stay looping in this statement until powered off or reset pushed.

void setup()
{
SD.begin(CS_pin);
Serial.begin(9600);
Serial1.begin(9600); // GPS check Baud rate...
Serial2.begin(57600);
pinMode(CS_pin, OUTPUT);
pinMode(pow_pin, OUTPUT);
digitalWrite(pow_pin, HIGH);
pinMode(26, OUTPUT); //gps power
digitalWrite(26, HIGH);
pinMode(30, OUTPUT); // BT power
// digitalWrite (30, HIGH);
//pinMode(RPM_pin, INPUT_PULLUP);
//revolutionsRPM = 0;
//rpmilliRPM = 0;
//timeoldRPM = 0;

int ABC = analogRead(1);
if (ABC < threshold) {
digitalWrite (30, HIGH);
delay(2500);

File logFile = SD.open("LOG.csv", FILE_READ);
if (logFile) {

Serial.println("Initializing Bluetooth...");
Serial2.println("Retrieving Data...");

while (logFile.available()) {
Serial2.write(logFile.read()); }

// close the file:
logFile.close();
if(SD.exists("LOG.csv"));
SD.remove ("LOG.csv");

}}

then I have my loop stuff here.. I tried putting in the if statements in the loop and couldn't get it to works as well as it does here..
the rest of the loop is long so I didn't post it as it has all the GPS stuff...

// if the file didn't open, print an error:
Serial.println("Starting Log...");

File logFile = SD.open ("LOG.csv", FILE_WRITE);
if (logFile)
{
String header = "ID, Date_Time, ABC, Latitude, Longitude, Altitude, Speed";
logFile.println(header);
logFile.close();
Serial.println(header);

}}

I inserted that loop comment in the wrong place...the loop is at the bottom of all that code

If processing interrupts is likely to be a problem, do it this way:

noInterrupts();
while(1);
// The CPU is effectively halted here

Much Appreciated!!! Thank you that works like a charm..so simple. I had a feeling it would be easy but being new I had no idea what to do.