#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#include "HX711.h"

Servo myservo;
const int pinDOUT = 2;
const int pinSCK = 4;
HX711 scale;
float kg ;
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(115200);
  myservo.attach(5);
  myservo.write(0);
  scale.begin(pinDOUT, pinSCK);
  scale.set_scale(0.42);
  scale.tare();

  lcd.init();
  lcd.backlight();
}

void loop() {
  if (scale.is_ready()) {
    long reading = scale.get_units(3);
    float kg = float(reading) / 1000;
    Serial.print("Berat: ");
    Serial.print(kg, 2);
    Serial.println(" kg");

    lcd.setCursor(0,0);
    lcd.print("Timbangan Digital");
    lcd.setCursor(0,1);
    lcd.print("Berat: " + String(kg, 2) + "kg");
    if(kg>=2){
      myservo.write(90);
    }else if(kg>=1){
      myservo.write(180);
    }else{
      myservo.write(0);
  }
  }

  delay(3000); 
}