Please Help!!

I managed to FINALLY get it to do what i wanted! i have been using the buzzer but it still gives me a delay when goes off even though the if statement is still true.thank you very much for all the help and i feel that my lack of understanding and knowledge probably pissed you off so im sorry for that!

here is the final code, if you feel like you've not reached the end of your tether just yet then i'm sure you'll correct me :slight_smile: but if not the THANKS A LOT!

byte triggerright=2;  // declaring the pins which will be in use
byte echoright = 3;
float distance;
int buzzer = 10;
byte red=7;
byte yellow=8;
byte green=4;
float distanceread;
void setup (){
  pinMode(buzzer, OUTPUT);
  pinMode (triggerright, OUTPUT);
  pinMode (echoright, INPUT);
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
  Serial.begin (9600);    // set up the baud rate for serial printing 
}

void loop (){

  distanceread = getDistance ();   

  if(getDistance ()<=.25 && getDistance () >=0)
  {       
    digitalWrite(red, HIGH);
     
    analogWrite (buzzer,128);
    delay (100);
    analogWrite (buzzer,0);
    
  } 
else { 
  digitalWrite(red, LOW);
  analogWrite (buzzer,0);
  }
          

  if(getDistance ()<=.5 && getDistance () >=.25) 
  {       
    digitalWrite(yellow, HIGH); 
    getDistance(); 
  } 
else {  
digitalWrite(yellow, LOW); 
}
       

  
 if(getDistance ()<=1 && getDistance () >=0.5)
 {       
    digitalWrite(green, HIGH);
    getDistance(); 
  } 
else { 
  digitalWrite(green, LOW);
  }
     
  Serial.print(getDistance());     
  Serial.print("m");          
  Serial.println();            
  delay (2000);                




}

float getDistance ()
{
  digitalWrite(triggerright, LOW);    
  delayMicroseconds(2);
  digitalWrite(triggerright, HIGH);   
  delayMicroseconds(5);
  digitalWrite(triggerright, LOW);    


  float distance = pulseIn (echoright, HIGH);  

  return (distance) / 2938 / 2;
}