#include <LiquidCrystal.h>
#include <Servo.h>
#include "HX711.h" // Include the HX711 library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Initialize the LCD with the pins connected to it
Servo servoMotor;
int pirPin = 7; // PIR motion sensor pin
int ledPin = 8; // Indicator LED pin
const int loadCellDoutPin = 9;
const int loadCellSckPin = 10;
float calibration_factor = 100.0; // Adjust this value based on your load cell's calibration
HX711 scale; // Declare the HX711 object
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
lcd.begin(20, 4); // Initialize the LCD
lcd.setCursor(0, 0);
lcd.print("WEIGHT: ");
servoMotor.attach(6); // Attach servo to pin 6
servoMotor.write(0); // Initialize servo at 0 degrees
// Initialize the HX711 library and set the calibration factor
scale.begin(loadCellDoutPin, loadCellSckPin);
scale.set_scale(calibration_factor);
}
void loop() {
if (digitalRead(pirPin) == HIGH) {
// PIR sensor detected motion, close servo motor
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WEIGHT: ");
lcd.setCursor(0, 1);
lcd.print("");
lcd.setCursor(0, 2);
lcd.print("");
lcd.setCursor(0, 2);
lcd.print("Motion detected");
servoMotor.write(0); // Close the servo
delay(500);
} else {
// Read the weight from the load cell
float weight = scale.get_units();
lcd.setCursor(8, 0);
lcd.print(weight, 2);
lcd.print(" kg ");
// Check weight conditions and control the servo and LED
if (weight >= 2 && weight <= 3) {
servoMotor.write(180); // Open the servo
digitalWrite(ledPin, HIGH); // Turn on indicator LED
} else if (weight > 4) {
servoMotor.write(0); // Close the servo
lcd.setCursor(0, 2);
lcd.print("berat lebih or");
lcd.setCursor(0, 3);
lcd.print("ganti pcs");
}
}
}