int t_p = 5;
int e_p = 18;
int buzzerPin = 2; // Connect the buzzer to digital pin 8
float ss = 0.034;
long durn;
float dist_cm;
unsigned long objectDetectionStartTime = 0; // To track the start time of object detection
bool objectDetected = false;
void setup() {
Serial.begin(115200);
pinMode(t_p, OUTPUT);
pinMode(e_p, INPUT);
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as an output
}
void loop() {
digitalWrite(t_p, LOW);
delayMicroseconds(2);
digitalWrite(t_p, HIGH);
delayMicroseconds(10);
digitalWrite(t_p, LOW);
durn = pulseIn(e_p, HIGH);
dist_cm = durn * ss / 2;
// Check if an object is detected
if (dist_cm < 10.0) { // Adjust the distance threshold as needed
if (!objectDetected) {
objectDetected = true;
objectDetectionStartTime = millis(); // Start tracking the time
} else {
unsigned long currentTime = millis();
if (currentTime - objectDetectionStartTime >= 5000) { // 5000 milliseconds = 5 seconds
Serial.println("Hello"); // Print "Hello" if object detected for more than 5 seconds
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
}
}
} else {
objectDetected = false; // Reset the object detection flag when no object is detected
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
}
delay(1000); // this speeds up the simulation
}