---
# System prepended metadata

title: Untitled

---

#include <Wire.h> 
#include <LiquidCrystal_I2C.h> 
#include <Ultrasonic.h>

const int trig = 11;
const int echo = 12;

const int LED1 = 2;
const int LED2 = 3;
const int LED3 = 4;
const int buzzer = 5;

const int sda = 8;
const int scl = 9;

int duration = 0;
int distance = 0;

LiquidCrystal_I2C lcd(0x27, 16, 2); 

Ultrasonic ultrasonic(trig, echo);

void setup() {
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(buzzer, OUTPUT); 
  Serial.begin(9600);
  
  lcd.init();
  lcd.backlight();
}

void loop() {
  digitalWrite(trig, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trig, LOW);

  duration = pulseIn(echo, HIGH);
  distance = (duration / 2) / 28.5;
  Serial.println(distance);

  lcd.setCursor(0, 0); 
  lcd.print("Distance: ");
  lcd.print(distance);
  lcd.print(" cm");
 
  if (distance <= 50) {
    blinkLED(LED1, 100); 
    playTone(buzzer, 2000, 100); 
  } else if (distance > 50 && distance <= 100) {
    blinkLED(LED2, 500); 
    playTone(buzzer, 1500, 500); 
  } else if (distance > 100 && distance <= 200) {
    blinkLED(LED3, 1000); 
    playTone(buzzer, 1000, 1000); 
  } else {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    noTone(buzzer); 
  }
}

void blinkLED(int pin, int delayTime) {
  digitalWrite(pin, HIGH); 
  delay(delayTime); 
  digitalWrite(pin, LOW); 
  delay(delayTime); 
}

void playTone(int pin, int frequency, int duration) {
  tone(pin, frequency); 
  delay(duration); 
  noTone(pin); 
}



