#include "HX711.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
#define BUTTON_PIN 2
#define DOUT 4
#define CLK 5
HX711 scale;
int buttonState = 0;
int lastButtonState = 0;
float calibration_factor = 420; // Adjust according to your load cell
float tareValue = 0; // Variable to store the tare value
float density = 1.0; // Density of water in g/ml
float containerVolume = 1000; // Volume of the container in ml
void setup() {
scale.begin(DOUT, CLK);
lcd.begin(16, 2);
pinMode(BUTTON_PIN, INPUT);
delay(2000);
lcd.clear();
lcd.print("Volume: ");
scale.set_scale(calibration_factor);
scale.tare();
tareValue = scale.get_units();
}
void loop() {
buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
scale.tare();
tareValue = scale.get_units();
}
delay(50 ); // Debounce delay
}
lastButtonState = buttonState;
float weight = scale.get_units() - tareValue;
float volume = (weight / density) * containerVolume; // Convert weight to volume
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Water Dispenser");
lcd.setCursor(0, 1);
lcd.print("Volume: ");
lcd.print(volume, 2);
lcd.print(" ml");
delay(1000);
}