Timing a sensor and/or buzzer

Hi everyone! I'm brand new to the community, so please forgive me if anything is unclear.

So my group's project is making a hat that senses incoming obstacles that are 40 cm away (or closer). We used the uno microcontroller and the hat has a sensor, buzzer, and LED light (and it is battery-powered and everything is hooked up to the microcontroller). Our code runs smoothly- the hat beeps when the sensor senses an obstacle. However, we found that even after moving the sensor away from the obstacle, the buzzer continues to beep. We were wondering if there was a way to sort of time the sensor/buzzer? So basically now we want:
sensor senses obstacle --> buzzer buzzes --> after 10 seconds buzzer will stop buzzing (and then repeat for when a new obstacle arises).

I've attached our code below. Thank you so much in advance!

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;

// defines variables
long duration;
int distance;
int safetyDistance;


void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}


void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

safetyDistance = distance;
if (safetyDistance <= 40){
  digitalWrite(buzzer, HIGH);
  digitalWrite(ledPin, HIGH);
}
else{
  digitalWrite(buzzer, LOW);
  digitalWrite(ledPin, LOW);
}

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

First, you need to slow down the ranger's ping rate to no more than about 20 or 25Hz - at the moment, the main limiter is the serial line speed.

Fix that, and then get back.

Uncompiled, untested

// defines pins numbers
const byte trigPin = 9;
const byte echoPin = 10;
const byte buzzer = 11;
const byte ledPin = 13;

void setup() {
  pinMode(trigPin, OUTPUT);
  digitalWrite (trigPin, LOW);
  pinMode(echoPin, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);
}

void loop() 
{
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  unsigned long duration = pulseIn(echoPin, HIGH, 40000);

  int safetyDistance = duration*0.034/2;

  if (safetyDistance <= 40){
    digitalWrite(buzzer, HIGH);
    digitalWrite(ledPin, HIGH);
  }
  else{
    digitalWrite(buzzer, LOW);
    digitalWrite(ledPin, LOW);
  }

  Serial.print("Distance: ");
  Serial.println(safetyDistance);
  
  delay (40);  // Of course, there's a better way than this.
}

Thank you so much for your input! Really, really appreciate it!!

Time variables should all be unsigned.

Unsigned 8-bit is byte, 16 bit is word, 32 bit is unsigned long, 64 bit is unsigned long-long.

Speed of sound in air varies mostly with temperature.

Mach 1 = ( 331.3 + .606 * deg C ) m/s.

When you have 64-bit or more FP and an FPU then making and using fractional factors is fine,
but if you don't and you want precision then use 2 integer scaling, multiply by the 1st, divide by the 2nd.

In 0 C air timing in us to get um distance without a decimal point.

um = micrometer, us = microsecond

um = ( durationUs * 3313 / 20 ) + ( durationUs * 606 * deg C / 2000 ) --- 20 and 2000 to count round trip

mm = um / 1000 (loses fraction of 1 mm)

I would use unsigned longs in the calculations.