// +------------------------------------+
// | 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;
}