#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HX711.h>
#define LOADCELL_DOUT_PIN 5
#define LOADCELL_SCK_PIN 4
LiquidCrystal_I2C lcd(0x27, 16, 2);
HX711 scale;
const int batteryAnalogPin = A0;
const int pHAnalogPin = A1;
float batteryVoltage = 0.0;
float maxBatteryVoltage = 12.6;
float minBatteryVoltage = 10.0;
float voltageDividerRatio = (10.0 + 4.7) / 4.7;
float pHValue = 0.0;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Batt Lvl");
lcd.setCursor(10, 0);
lcd.print("pH");
lcd.setCursor(13, 0);
lcd.print("W");
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(2280.f);
scale.tare();
}
void loop() {
// battery percentage
int batteryAnalogValue = analogRead(batteryAnalogPin);
float measuredVoltage = (batteryAnalogValue * 5.0) / 1023.0;
batteryVoltage = measuredVoltage * voltageDividerRatio;
float batteryPercentage = ((batteryVoltage - minBatteryVoltage) / (maxBatteryVoltage - minBatteryVoltage)) * 100;
if (batteryPercentage > 100) {
batteryPercentage = 100;
} else if (batteryPercentage < 0) {
batteryPercentage = 0;
}
lcd.setCursor(0, 1);
lcd.print(batteryVoltage,2);
lcd.print("V ");
lcd.setCursor(7, 1);
lcd.print((int)batteryPercentage);
lcd.print("% ");
//PH
int pHAnalogValue = analogRead(pHAnalogPin);
pHValue = (pHAnalogValue * 5.0) / 1023.0;
lcd.setCursor(10, 1);
lcd.print((int)pHValue);
//weight sensor
float weight = scale.get_units(10);
lcd.setCursor(13, 1);
lcd.print(weight, 2);
Serial.print("Weight: ");
Serial.print(weight, 2);
delay(1000); // Update every second
}