Led Blinking

#define trigPin 13
#define echoPin 12
#define BPin 7
#define piezoPin 9
#define RPin 4
#define GPin 8

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(BPin, OUTPUT);
pinMode(piezoPin, OUTPUT);
pinMode(RPin, OUTPUT);
pinMode(GPin, OUTPUT);
}

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println(“Out of range”);
}

else {
Serial.print(distance);
Serial.println(" cm");
}
delay(1000);
{if (distance <= 25 || distance <= 0){
digitalWrite(RPin, HIGH);
}
else{
digitalWrite(RPin, LOW);

}

}
{
if (distance >= 25 && distance <= 50){
digitalWrite(GPin, HIGH);
}
else {
digitalWrite(GPin, LOW);
}

{ if (distance >= 50 && distance <= 75){
digitalWrite(GPin, HIGH);
}
else {
digitalWrite(BPin, LOW);
}
{if (distance >= 75 && distance <= 100){
digitalWrite(BPin, HIGH);
}
else {
digitalWrite(GPin, LOW);
{
{if (distance >= 100 && distance <= 200){
digitalWrite(piezoPin, HIGH);
}
else {
digitalWrite(piezoPin, LOW);
}
}
}
}
}
}
}
}

I made this project everything is working fine but the G led is blinkingon the given range please help me

I made this project everything is working fine but the G led is blinkingon the given range please help me

Do you want the green LED to blink when the target is in range instead of just being on ? If so, then write a function that changes the state of the LED if a period of time has elapsed and call it when the target is in range. The timing cannot use the delay() function as it pauses the program during the delay period

You might find these threads helpful Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

You did not properly nest the if statements, so they all get evaluated each time through the loop. Whichever if statement is turning on the GPin LED, there is always going to be another if statement that turns it back off for the same distance.

  if (distance >= 25 && distance <= 50) { 
    digitalWrite(GPin, HIGH);  // turn GPin ON from 25 - 50            
  } else {
    digitalWrite(GPin, LOW);  // turn GPin OFF outside of 25-50
  }

  if (distance >= 50 && distance <= 75) {
    digitalWrite(GPin, HIGH); // turn GPin ON from 50 - 75
  } else {
    digitalWrite(BPin, LOW); 
  }

  if (distance >= 75 && distance <= 100) {
    digitalWrite(BPin, HIGH);
  } else {
    digitalWrite(GPin, LOW);  // turn GPin OFF outside of 75-100
    {
      if (distance >= 100 && distance <= 200) {
        digitalWrite(piezoPin, HIGH);
      }
      else {
        digitalWrite(piezoPin, LOW);
      }
    }
  }

You also have another problem, at certain distances there will be more than one if statement that is true. Because you are always testing for <= or >= , at 25, 50, and 75 you will have a true condition for both <= and >= .