#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define TRIG_PIN 18
#define ECHO_PIN 19
#define BUZZER 32
// Setup the LCD
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER, OUTPUT);
// Initialize the display and display the initial message
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Social Distancing");
LCD.setCursor(0, 1);
LCD.print("Alert System");
delay(1000);
}
void loop() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
int distance = duration * 0.034 / 2;
if (distance < 100) {
// Display warning on LCD
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Alert! Maintain");
LCD.setCursor(0, 1);
LCD.print("1m Distance!");
delay(2000); // Display warning for 2 seconds
// Sound the buzzer for 2 seconds
tone(BUZZER, 1000); // For example, 1000 Hz
delay(2000); // Buzzer sounds for 2 seconds
noTone(BUZZER);
} else {
// Display safe distance on LCD
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Safe Area");
LCD.setCursor(0, 1);
LCD.print("Distance: ");
LCD.print(distance);
LCD.print(" cm");
delay(1000); // Display safe distance for 1 second
// Turn off the buzzer
noTone(BUZZER);
}
// Wait for a moment before the next reading
delay(1000);
}