const int trigPin = 2; // Trig pin of the ultrasonic sensor
const int echoPin = 3; // Echo pin of the ultrasonic sensor
const int ledPin = 13; // LED pin for feedback
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
long duration, distance;
// Trigger the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the echo duration
duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
distance = duration * 0.034 / 2;
// Print the distance to the serial monitor
Serial.print("Distance: ");
Serial.println(distance);
// Check if the toilet is occupied or vacant based on the distance
if (distance < 10) {
// Toilet is occupied
digitalWrite(ledPin, HIGH);
Serial.println("Toilet Occupied");
} else {
// Toilet is vacant
digitalWrite(ledPin, LOW);
Serial.println("Toilet Vacant");
}
delay(1000); // Delay for readability and to avoid rapid changes
}