#include <Arduino.h>
#include <HX711.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int ld_cell = 2;
const int ld_sck = 4;
const int TRIG_PIN = 25; // GPIO 25 for TRIG
const int ECHO_PIN = 26; // GPIO 26 for ECHO
const int pirPin = 35; // Pin connected to PIR sensor OUT
const int MAX_DISTANCE = 300; // Maximum distance threshold in cm
const float MAX_WEIGHT = 4.0;
bool motionDetected = false;
HX711 scale;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(115200);
Wire.begin();
lcd.init();
lcd.backlight();
scale.begin(ld_cell, ld_sck);
scale.set_scale();
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(pirPin, INPUT);
}
void loop() {
long duration;
int distance;
int pirValue = digitalRead(pirPin);
// Ultrasonic sensor measurements
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2; // Calculate distance in cm
float reading = scale.get_units(10); // Read the average of 10 readings
if (pirValue == HIGH) {
// PIR sensor detected motion
Serial.println(reading/419.8);
// Check if weight exceeds maximum threshold
if (reading/419.8 > MAX_WEIGHT) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Weight Max");
lcd.setCursor(0, 1);
lcd.print("Distance : ");
lcd.print(distance);
lcd.print(" cm");
}
else if (distance > MAX_DISTANCE) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Weight : ");
lcd.print(reading/419.8);
lcd.print(" kg");
lcd.setCursor(0, 1);
lcd.print("Distance Max");
} else if(distance > MAX_DISTANCE && reading/419.8 > MAX_WEIGHT){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Weight Max");
lcd.setCursor(0, 1);
lcd.print("Distance Max");
}
// Check if distance exceeds maximum threshold
// Display weight and distance only if neither threshold was exceeded
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Weight : ");
lcd.print(reading/419.8);
lcd.print(" kg");
lcd.setCursor(0, 1);
lcd.print("Distance : ");
lcd.print(distance);
lcd.print(" cm");
}
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No motion detected");
}
delay(1000); // Adjust delay as needed
}