#include "HX711.h"
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
HX711 scale;
#define BTN_UP 2
#define BTN_DOWN 3
#define BTN_OK 4
float hargaPerKg = 0;
bool modeSetHarga = false;
unsigned long lastDebounce = 0;
const unsigned long debounceDelay = 200;
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(9600);
scale.begin(A1, A0);
scale.set_scale();
scale.tare();
pinMode(BTN_UP, INPUT_PULLUP);
pinMode(BTN_DOWN, INPUT_PULLUP);
pinMode(BTN_OK, INPUT_PULLUP);
}
void loop() {
long rawValue = scale.get_value();
float berat = rawValue / 420.0;
float totalHarga = berat * hargaPerKg;
if (millis() - lastDebounce > debounceDelay) {
if (digitalRead(BTN_OK) == LOW) {
modeSetHarga = !modeSetHarga;
lastDebounce = millis();
}
if (modeSetHarga) {
if (digitalRead(BTN_UP) == LOW && hargaPerKg < 50000) {
hargaPerKg += 500;
lastDebounce = millis();
}
if (digitalRead(BTN_DOWN) == LOW && hargaPerKg > 1000) {
hargaPerKg -= 500;
lastDebounce = millis();
}
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Berat : ");
lcd.print(berat);
lcd.print(" kg");
lcd.setCursor(0, 1);
lcd.print("Harga/kg : Rp");
lcd.print(hargaPerKg, 0);
lcd.setCursor(0, 2);
lcd.print("Total : Rp");
lcd.print(totalHarga, 0);
lcd.setCursor(0, 3);
if (modeSetHarga) {
lcd.print(" > MODE SET HARGA <");
} else {
lcd.print(" Tekan OK utk ubah ");
}
delay(1000);
}