difficulty getting timing to work

My program is supposed to make my buzzer sound a tone whenever my ultrasonic sensor detects an object less than 5 cm away for more than 5 seconds. However, i'm having difficulties getting the program to run consistently. After fulfilling my requirement the first time, the timing of it all gets messed up. I'm new to programming so any help would be much appreciated.

this is my program:

unsigned long delaystart = 0;
unsigned long delaytime = 5000;

int trigPin = 22 ; // Trigger
int echoPin = 23; // Echo
float duration, cm; // Ultrasonic sensor measurements
int buzzer = 12 ; //buzzer to arduino pin 12

void setup()
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT); //ultrasonic sensor
pinMode(echoPin, INPUT); //ultrasonic sensor
pinMode(buzzer, OUTPUT);

delaystart = millis();

}

void loop(){

digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = ((duration/2) / 29.1);

if(cm>=5)
{
noTone(buzzer);
}

if(cm<5)
{
if((millis() - delaystart) >= delaytime)
{
delaystart += (delaytime);
tone(buzzer, 500);
}
}

}

Duplicate - reported

Edit your post and insert code tags!

What's the problem actually? What does the sketch do and what did you expect?

Most probably you need to change this line

 delaystart += (delaytime);

to

 delaystart = millis();