Sharp 2D120X, piezo speaker, park assistent

First "Hello" to everyone.

I'm working on my technician thesis currently. One part is a project in which a piezo speaker should report the distance to the IR sensor by bleeping in intervals, getting faster the nearer the obstacle is.

Unfortunately my sketch works contrariwise - the farther the obstacle is, the faster the piezo speeker bleeps.

I experimented the whole day, but I can't find a solution.

I would be very grateful, if you could help me.

Greetings.

const int SensorPin = 0; //SensorPin als 0 gesetzt
int Piezo = 2; //Piezo als 2 gesetzt
long ZeitletzteMessung = 0; //ZeitletzteMessung als 0 gesetzt
void setup()
{
pinMode(Piezo, OUTPUT); //Piezo als Output gesetzt
Serial.begin(9600); //Initalisiert den seriellen Port mit der geforderten Übertragungsrate
}

void loop()
{

pinMode(SensorPin, INPUT); //SensorPin als Eingang gesetzt
int SensorWert=analogRead(0); //der Wert des analogen Eingangs wird der Variablen SensorWert übergeben
Serial.println(SensorWert); //Ausgabe des empfangenen Wertes an die serielle Schnittstelle
int Dauer = map(SensorWert, 0, 1023, 0, 5000); //Messwertskalierung/-umrechnung
unsigned long ZeitaktuelleMessung = millis(); //Zeit seit Start des Programms wird in der Variablen ZeitaktuelleMessung gespeichert

if(ZeitaktuelleMessung - ZeitletzteMessung > Dauer)
{
ZeitletzteMessung = ZeitaktuelleMessung;
if (Dauer!=0)
{
if (Dauer<2000)
{
for (int i=0; i<500; i++)
{
digitalWrite(Piezo, HIGH);
delayMicroseconds(150);
digitalWrite(Piezo, LOW);
delayMicroseconds(150);
}
}
}
}
delay(100);
}

Unfortunately my sketch works contrariwise - the farther the obstacle is, the faster the piezo speeker bleeps.

Nothing in the code that toggles the speaker off or on has any relation to distance. The speaker is turned on and off a fixed number of times at fixed intervals. If you are hearing otherwise, you need to post some other code or get your hearing checked.

Thanks for your answer.

The sketch is working perfect, but in the wrong direction.

I've tried it why my hardware and as long as i hold my hand over the sensor at a certain distance, the interval remains the same.
When i move my hand (nearer or farther), the interval changes. So my ears are fine.

Greetings.