int uzaklik = 0;
int sure=0;
int cm = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
Serial.begin(9600);
pinMode(3, OUTPUT);
}
void loop()
{
cm = 0.01723 * readUltrasonicDistance(5, 4);
sure=cm*10;
Serial.println(cm);
if (cm <= 100) {
tone(3,500,100);
delay(cm*10);
noTone(3);
delay(cm*10);
}
else {
if (cm > 100 && cm <= 200) {
tone(3,250 ,100);
delay(cm*10);
noTone(3);
delay(cm*10);
}
else {
tone(3, 100,100);
delay(cm*10);
noTone(3);
delay(cm*10);
}
}
}