i need help with "parking sensor" using HC-SR04

i wanted to try and make my own project because i had the parts needed for it.

i want to build a parking assistant that blinks faster the closer you get. on top of it i want a speaker to make a sound when the LED blinks.

my current code makes it so it constantly is on and makes noise even when i remove distance.

code: https://paste.ofcode.org/UV7HeD69Bz74fU2kynfVHu?fbclid=IwAR24fyEsnsJM_JuP0AI1ZcX3o1HcxbdE-JkxPYkFbNquSeTuhMKaxFmCtUY

circuit: https://i.imgur.com/bOkvBmV.png

Parksensor_-Sketch_kagelar.ino (1.15 KB)

@Kagelar

Your topic was Moved to it's current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Please post your code.

TheMemberFormerlyKnownAsAWOL:
Please post your code.

// 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() {
long duration (20);
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 <= 5){
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);
}

You're pinging too frequently.
Slow it down to no more than about 20Hz

Please remember to use code tags when posting code.