int trigPin = 5;
int echoPin = 18;
int beep = 16;
//define sound speed in cm/uS
float SOUND_SPEED = 0.034;
long duration;
float afstand;
void setup() {
Serial.begin(115200); // Starts the serial communication
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}
void loop() {
digitalWrite(trigPin, LOW); // eerst de trigger afzetten
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); //10µs hoog zetten
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); //lees de tijd tot de echopin de hoog detecteerde
// Calculate the distance
afstand = duration * SOUND_SPEED/2;
// Prints the distance in the Serial Monitor
Serial.print("afstand (cm): ");
//Serial.println(afstand);
if(afstand<25)
{
Serial.println(afstand);
digitalWrite(beep,HIGH);
delay(1);
digitalWrite(beep,LOW);
delay(1);
}
if (afstand<50)
{
Serial.println(afstand);
for (i = 0;i<10,i++)
{
digitalWrite(beep,HIGH);
delay(1);
digitalWrite(beep,LOW);
delay(1);
}
delay (300);
if (afstand<100)
{
Serial.println(afstand);
for (i = 0;i<10,i++)
{
digitalWrite(beep,HIGH);
delay(1);
digitalWrite(beep,LOW);
delay(1);
}
delay (1000);
}
else
{
Serial.println(afstand);
}
//delay(1000);
}