#include <HX711_ADC.h>
#include <EEPROM.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27, 2, 16);
const int HX711_dout = 4; //mcu > HX711 dout pin
const int HX711_sck = 3; //mcu > HX711 sck pin
HX711_ADC LoadCell(HX711_dout, HX711_sck);
const int calVal_eepromAdress = 0;
long t;


const byte ROWS = 4; //empat rows
const byte COLS = 4; //empat columns
char hexaKeys[ROWS][COLS] =
{ {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; //hubungkan ke row pinout keypad
byte colPins[COLS] = {12, 11, 10, 9}; //hubungkan ke column pinout keypad

int count = 0;

float Clear = 'A'; // tombol A untuk membersihkan input keypad
float Delete = 'B'; // tombol B untuk menghapus angka terakhir input keypad
float Tare = 'C'; // tombol C untuk membersihkan LCD
float Enter = 'D'; // tombol D untuk mengaktifkan motor servo
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS );


void setup() {
  Serial.begin(57600);
  delay(10);
  Serial.println();
  Serial.println("Starting...");
  pinMode(2, OUTPUT);
  pinMode(13, OUTPUT);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("WEIGHT BALANCE");
  lcd.setCursor(0, 1);
  lcd.print("let's measure");
  delay(2000);
  lcd.clear();
  LoadCell.begin(); // start connection to HX711
  LoadCell.start(500); // load cells gets 1000ms of time to stabilize
  LoadCell.tare();
  
}
void loop() {
 static boolean newDataReady = 0;
  const int serialPrintInterval = 0; //increase value to slow down serial print activity
  // check for new data/start next conversion:
  if (LoadCell.update()) newDataReady = true;
  // get smoothed value from the dataset:
  if (newDataReady)
  {
    
    if (millis() > t + serialPrintInterval) {
      float i = LoadCell.getData();
      Serial.print("Load_cell output val: ");
      Serial.println(i);
      newDataReady = 0;
      t = millis();

     

      lcd.setCursor(0, 1);
      lcd.print("weight :");
      lcd.setCursor(9, 1);
      lcd.print(i);
      lcd.setCursor(14, 1);
      lcd.print("GM");
    }
  } 
{
    char Key= customKeypad.getKey();
    if (Key)
    {
      lcd.setCursor (count, 0);
      lcd.print(Key);
      lcd.setCursor (10, 0);
      count ++;

      if (Key == Clear)   // Membersihkan LCd
      {
        lcd.clear();
        count = 0;
      }
      if (Key == Delete)  // Menghapus angka terakhir pada LCD
      {
        lcd.setCursor(count--, 10);
        lcd.print("  ");
        lcd.setCursor(count--, 10);
        lcd.print("   ");
      }
      if (Key == Tare)    // scale tare ke 0
      {
        lcd.clear();
        count = 0;
        LoadCell.tare();
      }
    
    }
 
}

    // check if last tare operation is complete:
  if (LoadCell.getTareStatus() == true) {
    lcd.clear();
    lcd.print("Tare complete");
    delay(1000);
    lcd.clear();
  }
  float i = LoadCell.getData();
  float k = count - i;
  if (i <= count)
  {
    digitalWrite (13, HIGH);
    digitalWrite (2, LOW);
  }
  if (i >= count)
  {
    digitalWrite (13, LOW);
    digitalWrite (2, HIGH);
  }
}