#include <Ultrasonic.h>
#define TRIGGER_PIN_1 12
#define ECHO_PIN_1 13
#define TRIGGER_PIN_2 14
#define ECHO_PIN_2 15
#define LED_PIN_1 2
#define LED_PIN_2 4
#define BUZZER_PIN_1 5
#define BUZZER_PIN_2 27
#define DISTANCE_THRESHOLD 50

//  ultrasonic sensors
Ultrasonic ultrasonic1(TRIGGER_PIN_1, ECHO_PIN_1);
Ultrasonic ultrasonic2(TRIGGER_PIN_2, ECHO_PIN_2);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Welcome to Car Parking System!");
  pinMode(LED_PIN_1, OUTPUT);
  pinMode(LED_PIN_2, OUTPUT);
  pinMode(BUZZER_PIN_1, OUTPUT);
  pinMode(BUZZER_PIN_2, OUTPUT);
  
}

void loop() {
 // Read the distance from ultrasonic sensor 1
  float distance1 = ultrasonic1.read();
  
  // Read the distance from ultrasonic sensor 2
  float distance2 = ultrasonic2.read();
  
  // Check if a car is detected in parking spot 1
  if (distance1 < DISTANCE_THRESHOLD) {
    digitalWrite(LED_PIN_1, HIGH);  // Turn on LED for parking spot 1
    digitalWrite(BUZZER_PIN_1,HIGH); // Turn on buzzer
    Serial.println("Car detected in Parking Spot 1");
  } else {
    digitalWrite(LED_PIN_1, LOW);  // Turn off LED for parking spot 1
    digitalWrite(BUZZER_PIN_1, LOW); // Turn off buzzer
  }
  
  // Check if a car is detected in parking spot 2
  if (distance2 < DISTANCE_THRESHOLD) {
    digitalWrite(LED_PIN_2, HIGH);  // Turn on LED for parking spot 2
    digitalWrite(BUZZER_PIN_2, HIGH); // Turn on buzzer
    Serial.println("Car detected in Parking Spot 2");
  } else {
    digitalWrite(LED_PIN_2, LOW);  // Turn off LED for parking spot 2
    digitalWrite(BUZZER_PIN_2, LOW); // Turn off buzzer
  }
  
  delay(500);  // Delay for stability
}