#define TRIGGER_PIN 12
#define ECHO_PIN 14
#define OCCUPANCY_THRESHOLD 50 // Adjust this value based on your setup
void setup() {
Serial.begin(9600);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
long duration, distance;
// Trigger ultrasonic sensor
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
// Read the duration of the echo
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate distance in centimeters
distance = duration * 0.034 / 2;
// Print distance to serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Check if parking space is occupied
if (distance < OCCUPANCY_THRESHOLD) {
Serial.println("Parking space is occupied");
// Add your code to perform actions when the parking space is occupied
} else {
Serial.println("Parking space is vacant");
// Add your code to perform actions when the parking space is vacant
}
delay(1000); // Adjust the delay based on your requirements
}