#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// Initialize LCD
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Define input pins
const int h1p1 = 2, h1p2 = 3, h1p3 = 4;
const int h2p1 = 5, h2p2 = 6, h2p3 = 7;
const int h3p1 = 8, h3p2 = 9, h3p3 = 10;
const int h4p1 = 11, h4p2 = 12, h4p3 = 13;
const int buzzer = 1;
// Initialize home states
int homeStates[4] = {0, 0, 0, 0};
int prevHomeStates[4] = {0, 0, 0, 0};
// Buzzer timing
unsigned long buzzerStartTime = 0;
bool buzzerActive = false;
const unsigned long buzzerDuration = 2000; // 2 seconds
// Define messages
const char* welcomeMsg[] = {"WELCOME TO EMERGENCY", " HEADQUARTER "};
const char* emergencyMsg = " EMERGENCY ALERT ";
const char* homePrefix = "Home ";
const char* medicalEmerg = "MEDICAL_EMERG";
const char* rescueEmerg = "RESCUE_EMERG";
const char* securityEmerg = "SECURITY_EMER";
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
// Initialize input pins
pinMode(h1p1, INPUT_PULLUP);
pinMode(h1p2, INPUT_PULLUP);
pinMode(h1p3, INPUT_PULLUP);
pinMode(h2p1, INPUT_PULLUP);
pinMode(h2p2, INPUT_PULLUP);
pinMode(h2p3, INPUT_PULLUP);
pinMode(h3p1, INPUT_PULLUP);
pinMode(h3p2, INPUT_PULLUP);
pinMode(h3p3, INPUT_PULLUP);
pinMode(h4p1, INPUT_PULLUP);
pinMode(h4p2, INPUT_PULLUP);
pinMode(h4p3, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(welcomeMsg[0]);
lcd.setCursor(0, 1);
lcd.print(welcomeMsg[1]);
}
void loop() {
// Read input states
int h1S1 = digitalRead(h1p1);
int h1S2 = digitalRead(h1p2);
int h1S3 = digitalRead(h1p3);
int h2S1 = digitalRead(h2p1);
int h2S2 = digitalRead(h2p2);
int h2S3 = digitalRead(h2p3);
int h3S1 = digitalRead(h3p1);
int h3S2 = digitalRead(h3p2);
int h3S3 = digitalRead(h3p3);
int h4S1 = digitalRead(h4p1);
int h4S2 = digitalRead(h4p2);
int h4S3 = digitalRead(h4p3);
// Determine home states
homeStates[0] = getHomeState(h1S1, h1S2, h1S3);
homeStates[1] = getHomeState(h2S1, h2S2, h2S3);
homeStates[2] = getHomeState(h3S1, h3S2, h3S3);
homeStates[3] = getHomeState(h4S1, h4S2, h4S3);
// Check if home states changed
bool statesChanged = false;
for (int i = 0; i < 4; i++) {
if (homeStates[i] != prevHomeStates[i]) {
statesChanged = true;
break;
}
}
if (statesChanged) {
// Copy current states to previous states
for (int i = 0; i < 4; i++) {
prevHomeStates[i] = homeStates[i];
}
// Display emergency alert or welcome message
bool allZero = true;
for (int i = 0; i < 4; i++) {
if (homeStates[i] != 0) {
allZero = false;
break;
}
}
if (allZero) {
// Display welcome message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(welcomeMsg[0]);
lcd.setCursor(0, 1);
lcd.print(welcomeMsg[1]);
} else {
// Display emergency alert
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(emergencyMsg);
// Display home states
int lineNumber = 1;
int messageIndex = 0;
for (int i = 3; i >= 0; i--) {
if (homeStates[i] != 0) {
lcd.setCursor(0, lineNumber);
lcd.print(homePrefix);
lcd.print(i + 1);
lcd.print(" ");
if (homeStates[i] == 1) lcd.print(medicalEmerg);
else if (homeStates[i] == 2) lcd.print(rescueEmerg);
else if (homeStates[i] == 3) lcd.print(securityEmerg);
lineNumber++;
messageIndex++;
if (messageIndex >= 3) break;
}
}
// Activate buzzer
buzzerActive = true;
buzzerStartTime = millis();
digitalWrite(buzzer, HIGH);
}
}
// Handle buzzer timing
if (buzzerActive) {
if (millis() - buzzerStartTime >= buzzerDuration) {
digitalWrite(buzzer, LOW);
buzzerActive = false; // Reset buzzer status
}
}
delay(500);
}
// Function to determine home state
int getHomeState(int s1, int s2, int s3) {
if (s1 == LOW && s2 == HIGH && s3 == HIGH) return 1;
else if (s1 == HIGH && s2 == LOW && s3 == HIGH) return 2;
else if (s1 == HIGH && s2 == HIGH && s3 == LOW) return 3;
else return 0;
}