#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HX711.h>

HX711 scale;
LiquidCrystal_I2C lcd(0x27, 16, 2); // initialize the LCD with the I2C address and dimensions

const int buzzerPin = 2; // connect the buzzer to digital pin 2
const float emptyWeight = 0.1; // set the weight of an empty IV bottle in kilograms
const float fullWeight = 0.5; // set the weight of a full IV bottle in kilograms
const float threshold = (fullWeight + emptyWeight) / 2; // set the threshold value for the empty IV bottle

void setup() {
  pinMode(buzzerPin, OUTPUT); // set the buzzer pin to output mode
  scale.begin(4, 5); // initialize the HX711 module with load cell connected to pins 3 and 4
  scale.set_scale((fullWeight - emptyWeight) / 1000.0); // set the calibration factor for your specific load cell
  lcd.init(); // initialize the LCD display
  lcd.backlight(); // turn on the backlight for the LCD display
  lcd.setCursor(0, 0); // set the cursor to the top left corner of the LCD display
  lcd.print("IV Weight:"); // print the label for the weight reading
}

void loop() {
  float weight = scale.get_units(); // get the weight reading in the units you calibrated for
  weight /= 1000; // convert the weight reading from grams to kilograms
  lcd.setCursor(10, 0); // set the cursor to the position after the label
  lcd.print(weight, 1); // print the weight reading to the LCD display with one decimal place

  if (weight < threshold) {
    tone(buzzerPin, 255); // turn on the buzzer
    lcd.setCursor(0, 1); // set the cursor to the second row of the LCD display
    lcd.print("IV Empty!"); // print a warning message to the LCD display
  } else {
    tone(buzzerPin, 0); // turn off the buzzer
    lcd.setCursor(0, 1); // set the cursor to the second row of the LCD display
    lcd.print("              "); // clear the warning message from the LCD display
  }
}