PING))) Sensor Alarm

I am making an alarm system using a PING))) sensor. I have it set where if something is less than 30 inches away, an led turns on. How do I make it stay on after the sensor is back at 30 inches?

const int pingPin =7 ;
const int ledPin = 13;

void setup() {
    Serial.begin(9600);
    pinMode(ledPin, OUTPUT);
}

void loop() {
    long duration, inches, cm;

    pinMode(pingPin,OUTPUT);
    digitalWrite(pingPin,LOW);
    delayMicroseconds(2);
    digitalWrite(pingPin,HIGH);
    delayMicroseconds(5);
    digitalWrite(pingPin,LOW);

    pinMode(pingPin,INPUT);
    duration =pulseIn(pingPin,HIGH);

    inches = microsecondsToInches(duration);
    cm = microsecondsToCentimeters(duration);

    Serial.print(inches);
    Serial.print("in, ");
    Serial.print(cm);
    Serial.print("cm");
    Serial.println();
    delay(100);

     
        if (inches <= 30) {
            digitalWrite(ledPin, HIGH);
            alarm();
        }
        else {
            digitalWrite(ledPin, LOW);
        }
    }


long microsecondsToInches(long microseconds)
{
    return microseconds /74/2;
}

long microsecondsToCentimeters(long microseconds)
{
    return microseconds /29/2;
}


void alarm() {
  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
  delay(500);
}

How do I make it stay on after the sensor is back at 30 inches?

It would seem to me that the obvious answer is "once you've turned it on, don't turn it off", but I guess you've considered that.

When the LED turns on set a boolean variable, let's call it ledOn, to true.
Then if ledOn is true do not check the distance and the LED will stay on.

That seems too complicated - if you make sure it is OFF in "setup()", then you only set it ON in "loop()".
No need for a flag.

True.
I was building on the code he had already but it would be better to start again.

Thanks, guys! I used the boolean idea. does this look correct to you?

if (inches <= 30) {
            boolean ledON = true;
        }
        
        if (ledON == true) {
          digitalWrite(ledPin, HIGH);
        }

Never mind, I got it to work without boolean. Thanks for your help!

does this look correct to you?

No, absolutely not.

The scope of your boolean flag was limited to the block in which it was declared, so it was very short-lived.