HELP

i am writing a code which will hopefully replicate a parking sensor using ultrasonic sensor, it is really similar to the PING tutorial. i am trying to add a buzzer which should start to beep more frequent as the distance to an object reduces. the buzzer needs to beep at 4cm and then more frequent at 3cm and even more frequent at 2cm and pretty much flatline at 1cm. this is a mini project i am being assessed on so i'm going all out and have even added LED's for visual aid. the LED's go from green to amber to red as the distance increases. the problem is that there seems to be really long delay when reading the distance and the buzzer should be constantly on when the if statement applies but it comes on then off then on again.please help me, its probably very simple.THE PROBLEM IS USING THE BUZZER WHEN DISTANCE < 0.25,0.5 AND 1.PLEASE HELP SOMEONE!!

byte triggerright=2;  // declaring the pins which will be in use
byte echoright = 3;
int redled=13;
byte yellowled=12;
byte greenled=11;



float distance, m;    // used a float function because of output value given
                      // as a decimal

byte buzzer=10;      // buzzer using the PWM pin 

void setup (){
  
// declaring what the pins are used as

pinMode(buzzer, OUTPUT);
pinMode (triggerright, OUTPUT);
pinMode (echoright, INPUT);
pinMode (redled, OUTPUT);


  
Serial.begin (9600);    // set up the baud rate for serial printing
  
 
}

void loop (){
  
digitalWrite(triggerright, LOW);    // start of with no pulse being sent
delayMicroseconds(2);
digitalWrite(triggerright, HIGH);   // then 2 microseconds later a pulse is sent out for a 15 microsecond delay
delayMicroseconds(15);
digitalWrite(triggerright, LOW);    // then the pulse is stopped being sent for 20 microseconds
delayMicroseconds(20);




  
  
  
distance = pulseIn (echoright,HIGH);  //Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, 
                                      //starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds
                                      //Gives up and returns 0 if no pulse starts within a specified time out.
  
m = microsecondsToMetres(distance);   // setting up the distance function to be microseconds per metre

  
distance = (m);                      // declaring that distance is function m



 if(distance < 1)                   // using if statement to declare that if distance<1 then buzzer will be used
{
  analogWrite(10, 128);             // writes an analog value to pin 11.the pin will 
                                    // generate a steady square wave of the specified duty cycle 
  delay(700);                       // specified duty cycle of 0.7 seconds
  
  analogWrite(10, LOW);            // pin 11 will be assigned 0v for a specified time
  delay(700);                       // pin 11 will stay at 0v for 0.7 seconds
  analogWrite(10, 128);   
  delay(700);                  
  digitalWrite(10, LOW);  
  delay(700); 
  
  digitalWrite (11,HIGH);
}
else if(distance < 0.5)             // using if statement to declare that if distance<0.5 then buzzer will be used
{
  analogWrite(10, 128);   
  delay(400);                  
  digitalWrite(10, LOW);  
  delay(400); 
  analogWrite(10, 128);   
  delay(400);                  
  digitalWrite(10, LOW);  
  delay(400); 
   digitalWrite (13,);
  
  digitalWrite (12,HIGH);
  
}
else if(distance < 0.25)           // using if statement to declare that if distance<0.25 then buzzer will be used
{
  analogWrite(10, 128);   
  delay(100);                  
  digitalWrite(10, LOW);  
  delay(100); 
  analogWrite(10, 128);   
  delay(100);                  
  digitalWrite(10, LOW);  
  delay(100); 
  
  digitalWrite (13,HIGH);
}
else if (distance>5)            // if distance is over 5 then the serial will print "the distance is over 5"
{
  
  Serial.println("the distance is over 5m...");
  delay (1500);
}
 else {}

 digitalWrite (13,LOW);
 digitalWrite (13,LOW);
 digitalWrite (13,LOW);

  Serial.print(distance);      // serial prints the distance funtion which is declared prior to this line
  Serial.print("m");           // distance is printed in metres
  Serial.println();            // inserts gap line in the print
  delay (500);                // distance will print every 2 seconds



}
float microsecondsToMetres(float microseconds)
{
  // The speed of sound is 340 m/s or 2938 microseconds per metre.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 2938 / 2;
}
distance = (m);                      // declaring that distance is function m

"distance" isn't a function of "m", it is "m".

 analogWrite(10, LOW);

Bonus marks for your assessment - you've declared "buzzer", so why not use it?

 analogWrite(10, 128);             // writes an analog value to pin 11.the pin will

Comment should match code.

You've got delays adding up to much more than a second - if you're moving quickly, you're not going to make enough readings.
Have a look at using the blink without delay tutorial.

thanks for the input, i have been messing around with the code and haven't changed the comments evidently! can you see my problem in regards to my delays on the buzzers??

can you see my problem in regards to my delays on the buzzers??

Yes.

Your indentation could do with some work.
The IDE ctrl-T auto format will help you there.

I'll ignore your language for the moment, and refer you to the blink without delay tutorial.

If you restructure your code so that you take a reading every time through "loop", then modify the blink without delay tutorial, so that instead of blinking the LED, you sound the buzzer, you can vary the tutorial's "interval" variable to change the pulse rate of the buzzer.

Edit: You posted this question yesterday. Why repeat it?
If you've got a problem with coding, why are you making it more difficult for yourself by adding the LEDs?
Start simple and when you've got that working, start looking at making stuff more complicated.

i need to somehow re read the distance while in this if statement but that is my problem!!

No you don't.

Pseudo code:

void loop ()
{
  float distance = readDistance ();

  if (distance < 0.25) {
    interval = 100; // whatever
  }

  if (distance < 0.5) {
    interval = 400; // whatever
  }

  if (distance < 1.0) {
    interval = 700; // whatever
  }

  if (millis() - previousMillis > interval) {
    previousMillis = millis();   
    soundBuzzer ();
  }
}

float distance = readDistance ();

do i need to define that function?

if (millis() - previousMillis > interval) {
previousMillis = millis();
soundBuzzer ();
}
}

is that fine as it is or do i need to add to it?