#define TRIG_PIN 23
#define ECHO_PIN 22
#define LED_PIN_1 19
#define LED_PIN_2 18
#define LED_PIN_3 5
#define LED_PIN_4 17
int BUZZER = 16;

void setup() {
  Serial.begin(9600);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(LED_PIN_1, OUTPUT);
  pinMode(LED_PIN_2, OUTPUT);
  pinMode(LED_PIN_3, OUTPUT);
  pinMode(LED_PIN_4, OUTPUT);
  pinMode(BUZZER, OUTPUT);
}

void loop() {
  long duration, distance;
  
  // Trigger ultrasonic sensor
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  
  // Read the echo pulse
  duration = pulseIn(ECHO_PIN, HIGH);
  
  // Convert pulse duration to distance
  distance = duration * 0.03405 / 2;
  
  // Print distance to serial monitor
  Serial.print("Distance: ");
  Serial.println(distance);

  // Control LEDs and Buzzer based on distance
  if (distance > 300) {
    digitalWrite(LED_PIN_1, HIGH);
    digitalWrite(LED_PIN_2, HIGH);
    digitalWrite(LED_PIN_3, HIGH);
    digitalWrite(LED_PIN_4, HIGH);
    tone(BUZZER, 262, 250);
    delay(300);
    tone(BUZZER, 294, 250);
    delay(300);
    tone(BUZZER, 330, 250);
    delay(300);
    tone(BUZZER, 349, 250);
    delay(300);
  } else if (distance > 200) {
    digitalWrite(LED_PIN_1, LOW);
    digitalWrite(LED_PIN_2, HIGH);
    digitalWrite(LED_PIN_3, HIGH);
    digitalWrite(LED_PIN_4, HIGH);
  } else if (distance > 100) {
    digitalWrite(LED_PIN_1, LOW);
    digitalWrite(LED_PIN_2, LOW);
    digitalWrite(LED_PIN_3, HIGH);
    digitalWrite(LED_PIN_4, HIGH);
  } else if (distance > 50) {
    digitalWrite(LED_PIN_1, LOW);
    digitalWrite(LED_PIN_2, LOW);
    digitalWrite(LED_PIN_3, LOW);
    digitalWrite(LED_PIN_4, HIGH);
  } else {
    digitalWrite(LED_PIN_1, LOW);
    digitalWrite(LED_PIN_2, LOW);
    digitalWrite(LED_PIN_3, LOW);
    digitalWrite(LED_PIN_4, LOW);
  }

  delay(500);  // Adjust the delay as needed
}