#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include "HX711.h"
// Initialize the RTC
RTC_DS3231 rtc;
// Initialize the LCD (ganti 0x27 dengan alamat I2C yang ditemukan dari scan)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Initialize the HX711
#define LOADCELL_DOUT_PIN 21
#define LOADCELL_SCK_PIN 22
HX711 scale;
// Buzzer pin
const int buzzer = 18;
// Set the weight threshold for empty
const float weightThreshold = 10.0; // Adjust this value as needed
void setup() {
// Start serial communication
Serial.begin(115200);
// Initialize the LCD
lcd.begin(16, 2); // Add the number of columns and rows
lcd.backlight();
// Initialize the RTC
if (!rtc.begin()) {
lcd.print("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Initialize the HX711
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale();
scale.tare();
// Initialize the buzzer pin as an output
pinMode(buzzer, OUTPUT);
}
void loop() {
// Get current time
DateTime now = rtc.now();
// Get the weight from the load cell
float weight = scale.get_units(10);
// Display the weight on the LCD
lcd.setCursor(0, 0);
lcd.print("Weight: ");
lcd.print(weight, 1);
lcd.print(" g");
// Check if the weight is below the threshold
if (weight < weightThreshold) {
// Sound the buzzer
digitalWrite(buzzer, HIGH);
// Display empty warning on the LCD
lcd.setCursor(0, 1);
lcd.print("Feed box empty!");
} else {
// Turn off the buzzer
digitalWrite(buzzer, LOW);
// Display the current time
lcd.setCursor(0, 1);
lcd.print("Time: ");
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
}
// Delay for a second
delay(1000);
}