Stopping the loop in a distance sensor when the required distance has been met

I am making a sensor th alert when the car is parked at the required distance from the wall this action lights a led, my problem is that I want to turn off the led after a delay (in otherwise halt the loop) and reactivate it again once the sensor has determined that the set distance has altered (car position has altered) I have got everything working as it should but the LED stays on?
Could someone please help!
Thankyou

//Ultrasonic sensor HC-SR04 Dougs
//Pins connected to Sensor

const int trigPin = 2;
const int echoPin = 3;

//Led Pins
const int ledRed = 7;
const int ledYellow = 8;
const int ledGreen = 9;

//Define Variables
long duration;
long distance;
int range = 600; //Range in centimeters

void setup() {
//initialise serial communication
Serial.begin(9600);
//initialise sensor pins
pinMode (trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//Initialise Leds
pinMode(ledRed, OUTPUT);
pinMode(ledYellow, OUTPUT);
pinMode(ledGreen, OUTPUT);
//Set lEDs
digitalWrite(ledRed, LOW);
digitalWrite(ledYellow, LOW);
digitalWrite(ledGreen, LOW);
}
void loop() {
//Establish Variables for duration of the ping
// andthe distance result in cm
long duration, cm;
// The ping is triggered by a High Pulse of 2 or more microseconds
//Give a short low pulse beforehandhigh Pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);

delayMicroseconds(5);
digitalWrite(trigPin, LOW);
// Take reading on Echo Pin
duration = pulseIn(echoPin, HIGH);
distance = duration * .034 / 2;
//Convert Time to a Distance
//cm=Microseconds to Centimeters(duration)
Serial.print("Distance: ");
Serial.println(distance);
Serial.print(" cm");
Serial.println();

if (distance >= 300)// distance required to start operating

digitalWrite(ledGreen, HIGH);
digitalWrite(ledYellow, LOW);
digitalWrite(ledRed, LOW);
if (distance <=100) //approaching yellow caution
{
digitalWrite(ledYellow, HIGH);
//digitalWrite(ledRed,LOW);
digitalWrite(ledGreen, LOW);
}
if (distance <= 49)// stop distance
{
digitalWrite(ledRed, HIGH);
digitalWrite(ledYellow,LOW);
}
{
delay(20000);
digitalWrite(ledRed,LOW);
}
}

Post your code or we are all guessing.

Think "State Machine" Google that.

Your code has an if statement at the top:

If the led should be on, then turn it on and run whatever code should determine if it should be off.

Else turn it off and run whatever code checks to see if it should be on.

Maybe a flag to keep up with whether or not you've run that bit yet.

boolean lightRanAlready = false;

Then in here:

 if (distance <=100)  //approaching yellow caution
  {
    digitalWrite(ledYellow, HIGH);
    //digitalWrite(ledRed,LOW);
    digitalWrite(ledGreen, LOW);
    lightRanAlready = false;  // reset the flag here since we've opened up the distance again.  
  }
  if (!lightRanAlready && (distance <= 49))// stop distance 
  {
    digitalWrite(ledRed, HIGH);
    digitalWrite(ledYellow,LOW);
    lightRanAlready = true;   //  Will stop this from entering again. 
  }

Don't stop the loop. That's like asking a human to stop breathing.

Record the time when you switched the light on. Each time you go through the loop, check all the distances again. If you're still in that range and the time limit has passed, turn it off.

Have a look at Several things at a time. Note how each function runs very briefly and returns to loop() so the next one can be called. Long running processes are achieved a tiny piece at a time

That allows other checks to take place between functions so that, for example, a long running process could be stopped

...R

marco_c:
Post your code or we are all guessing.

Thank you for your info but my intention was, as the car was parked at distance 49 the red led would come on and stay on for a 2 minutes then the red led would go out and stay out until the car was distance was greater than 49cm, in other words you park the car in the evening the red led would be on for 2 min then you go out the following morning then come back home and when you get to 300cm the loop starts again,
.
Thanks again for your assistance as you can see it's all new to me

DougC:
Thank you for your info but my intention was, as the car was parked at distance 49 the red led would come on and stay on for a 2 minutes then the red led would go out and stay out until the car was distance was greater than 49cm, in other words you park the car in the evening the red led would be on for 2 min then you go out the following morning then come back home and when you get to 300cm the loop starts again,

That is a classic case for keeping track of the state of the system. These may be the possible states
Far away
Approaching wall
Correct distance
Too close
In position
Sleeping

You could have a variable that holds the first character of each of those conditions and work through them as the situation changes.

I have Correct distance and In position because there will probably be occasions when you come within the correct distance briefly while driving and they should not trigger the LED. I am assuming that Correct distance would only change to In position when Correct distance had existed for some minimum time. The LED would come on when In position and the timer to turn it off after 2 minutes would start. After the 2 minutes the state would change to Sleeping. And from Sleeping the state would change to Far away when the distance increased.

...R