// +------------------------------------+
// |       Welcome To Yogii Team        |
// |                                    |
// |  +---------------------------+     |
// |  |      Team Information     |     |
// |  |                           |     |
// |  |    - Team Menbers         |     |
// |  |    - Yogeshwaran M        |     |
// |  |    - Pooja Sri            |     |
// |  |                           |     |
// |  |    Project Name           |     |__________________________________
// |  |    Intuitively Responsive Environments for HUMAN AND ANIMAL       |
// |  |                                                                   |
// |  |    - Hardware Components                                    |     |
// |  |    - MPU6050 (Accelerometer and Gyroscope)                  |     |
// |  |    - DHT22 (Temperature and Humidity Sensor)                |     |
// |  |    - HX711 (Load Cell Amplifier)                            |     |
// |  |    - HC-SR04 (Ultrasonic Sensor)                            |     |
// |  |    - PIR Sensor (Passive Infrared Sensor)                   |     |
// |  |    - Servo Motors                                           |     |
// |  |    - Buzzer                                                 |     |
// |  |    - LEDs                                                   |     |
// |  |    - Liquid Crystal Display (LCD) with I2C interface        |     |
// |  |                                 __________________________________|
// |  +------------------------- -+     |
// |                                    |
// |  +---------------------------+     |
// |  |       Initialization      |     |
// |  |                           |     |
// |  |  - LCD Setup              |     |
// |  |  - MPU6050 Setup          |     |
// |  |  - Servo Setup            |     |
// |  |  - DHT22 Setup            |     |
// |  +---------------------------+     |
// |                                    |
// |  +---------------------------+     |
// |  |       Main Loop           |     |
// |  |                           |     |
// |  |  - Accelerometer Reading  |     |
// |  |  - PIR Sensor Reading     |     |
// |  |  - Earthquake Detection   |     |
// |  |  - Animal Detection       |     |
// |  |  - Temperature Reading    |     |
// |  |  - Humidity Reading       |     |
// |  +---------------------------+     |
// +------------------------------------+

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <HX711.h>

#define DHTPIN 7
#define DHTTYPE DHT22

const int buzzerPin = 5;
const int ledPin1 = 35;
const int ledPin2 = 41;
const float threshold = 0.5;
const int pirPin = 8;
const int loadCellDoutPin = 2;
const int loadCellSckPin = 3;
const int trigPin = 37;
const int echoPin = 32;
int pirState = LOW;
int servoPin = 9;
int foodFeederServoPin = 10;
Servo myservo;
Servo foodFeederServo;

Adafruit_MPU6050 mpu;
LiquidCrystal_I2C lcd(0x27, 20, 4);
DHT dht(DHTPIN, DHTTYPE);
HX711 scale;

sensors_event_t event;
float ax_prev, ay_prev;
bool earthquakeDetected = false;
float temperature, humidity, dewPoint, heatIndex;
const unsigned long earthquakeAlertDuration = 5000;
unsigned long startTime;
unsigned long earthquakeDisplayDuration = 3000;  // Duration to display earthquake message (in milliseconds)

void welcomeMessage() {
  lcd.clear();
  lcd.setCursor(2, 1);
  lcd.print("Welcome to YOGII");
  lcd.setCursor(8, 2);
  lcd.print("Team");
  delay(500);
  lcd.clear();
  lcd.setCursor(1, 1);
  lcd.print("Initializing...");
  delay(500);
  lcd.clear();
  lcd.setCursor(2, 1);
  lcd.print("Environments ");
  delay(500);
  lcd.setCursor(3, 2);
  lcd.print("Unleashed");
  delay(500);
}

void displayEarthquakeMessage() {
  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print("** EARTHQUAKE! **");
  lcd.setCursor(0, 1);
  lcd.print("Move to a safe place");
  lcd.setCursor(3, 3);
  lcd.print("Hurry up...");
  tone(buzzerPin, 1000, 1000);
  delay(2000);
}

void setup() {
  Serial.begin(115200);

  lcd.init();
  lcd.backlight();
  welcomeMessage();
  while (!mpu.begin()) {
    delay(500);
  }
  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  myservo.attach(servoPin);
  myservo.write(90);
  foodFeederServo.attach(foodFeederServoPin);
  dht.begin();
  pinMode(buzzerPin, OUTPUT);
  pinMode(pirPin, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  ax_prev = 0.0;
  ay_prev = 0.0;
  scale.begin(loadCellDoutPin, loadCellSckPin);
}

void loop() {
  mpu.getAccelerometerSensor()->getEvent(&event);
  pirState = digitalRead(pirPin);

  temperature = dht.readTemperature();
  humidity = dht.readHumidity();

  dewPoint = calculateDewPoint(temperature, humidity);

  heatIndex = dht.computeHeatIndex(temperature, humidity, false);

  float loadCellWeight = scale.read();

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  unsigned long duration = pulseIn(echoPin, HIGH);
  float distance = (duration * 0.0343) / 2;

  if (distance < 10) {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    tone(buzzerPin, 1000, 200);
    lcd.setCursor(0, 3);
    lcd.print("Hairpin: Detected");
  } else {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
    noTone(buzzerPin);
    lcd.setCursor(0, 3);
    lcd.print("Hairpin: Clear");
  }

  if (loadCellWeight < 1000) {
    foodFeederServo.write(0);
    delay(500);
  } else {
    foodFeederServo.write(90);
  }

  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print("Temp:");
  lcd.setCursor(7, 0);
  lcd.print(temperature);
  lcd.print(" *C");

  lcd.setCursor(1, 2);
  lcd.print("Humid:");
  lcd.setCursor(8, 2);
  lcd.print(humidity);
  lcd.print("%");

  ax_prev = event.acceleration.x;
  ay_prev = event.acceleration.y;

  if (!earthquakeDetected && (abs(event.acceleration.x - ax_prev) > threshold ||
                               abs(event.acceleration.y - ay_prev) > threshold)) {
    earthquakeDetected = true;
    displayEarthquakeMessage();
    startTime = millis();
  }

  if (pirState == HIGH) {
    myservo.write(0);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("* Animal on road! *");
    lcd.setCursor(0, 1);
    lcd.print("Roads closed!");
    delay(200);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("WAIT UNTIL THE       ANIMAL CROSSES");
    delay(500);
    unsigned long crossingStartTime = millis();
    while (pirState == HIGH && millis() - crossingStartTime < 30000) {
      delay(200);
      pirState = digitalRead(pirPin);
    }
  } else if (pirState == LOW && !earthquakeDetected) {
    myservo.write(90);
    delay(2000);
  }

  // Clear earthquake message after the specified duration
  if (earthquakeDetected && (millis() - startTime >= earthquakeDisplayDuration)) {
    earthquakeDetected = false;
    noTone(buzzerPin);
    lcd.clear();
  }

  delay(200);
}

float calculateDewPoint(float temperature, float humidity) {
  // Add your dew point calculation logic here if needed
  // For now, the function returns 0
  return 0;
}
$abcdeabcde151015202530fghijfghij
mega:SCL
mega:SDA
mega:AREF
mega:GND.1
mega:13
mega:12
mega:11
mega:10
mega:9
mega:8
mega:7
mega:6
mega:5
mega:4
mega:3
mega:2
mega:1
mega:0
mega:14
mega:15
mega:16
mega:17
mega:18
mega:19
mega:20
mega:21
mega:5V.1
mega:5V.2
mega:22
mega:23
mega:24
mega:25
mega:26
mega:27
mega:28
mega:29
mega:30
mega:31
mega:32
mega:33
mega:34
mega:35
mega:36
mega:37
mega:38
mega:39
mega:40
mega:41
mega:42
mega:43
mega:44
mega:45
mega:46
mega:47
mega:48
mega:49
mega:50
mega:51
mega:52
mega:53
mega:GND.4
mega:GND.5
mega:IOREF
mega:RESET
mega:3.3V
mega:5V
mega:GND.2
mega:GND.3
mega:VIN
mega:A0
mega:A1
mega:A2
mega:A3
mega:A4
mega:A5
mega:A6
mega:A7
mega:A8
mega:A9
mega:A10
mega:A11
mega:A12
mega:A13
mega:A14
mega:A15
imu1:INT
imu1:AD0
imu1:XCL
imu1:XDA
imu1:SDA
imu1:SCL
imu1:GND
imu1:VCC
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
bz1:1
bz1:2
pir1:VCC
pir1:OUT
pir1:GND
servo1:GND
servo1:V+
servo1:PWM
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND
servo2:GND
servo2:V+
servo2:PWM
cell1:VCC
cell1:DT
cell1:SCK
cell1:GND
servo3:GND
servo3:V+
servo3:PWM
ultrasonic1:VCC
ultrasonic1:TRIG
ultrasonic1:ECHO
ultrasonic1:GND
led1:A
led1:C
led2:A
led2:C