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

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); // Initialize 16x2 LCD
 
#define DEBUG_HX711
#define CALIBRATION_FACTOR 2000

byte pinData = 6;
byte pinClk = 7;

const int TRIGPIN = 9;
const int ECHOPIN = 8;
long timer;
int jarak,tinggi;

const int buttonTare = 10;                                                                                        ;     // the number of the pushbutton pin

int buttonState = 0;         // variable for reading the pushbutton status

HX711 scale;
 
void setup() {
  pinMode(13,OUTPUT);
  pinMode(buttonTare,INPUT_PULLUP);
  pinMode(ECHOPIN, INPUT);
  pinMode(TRIGPIN, OUTPUT);
  lcd.begin(16, 2);
  lcd.init();
  lcd.backlight();
  lcd.print( "" );

  scale.begin(pinData, pinClk);
  scale.set_scale(CALIBRATION_FACTOR);
  scale.tare();
}
 
void loop() {
#ifdef DEBUG_HX711
 
  digitalWrite(TRIGPIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);
  timer = pulseIn(ECHOPIN, HIGH);
  jarak = timer / 58;
  tinggi=(200-jarak);
  
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("TINGGI=");  
  lcd.setCursor(7, 0);  
  lcd.print(tinggi);
  lcd.setCursor(12, 0);
  lcd.print("CM");
  
  lcd.setCursor(0, 1);
  lcd.print( "BERAT =" );
  lcd.setCursor(7,1);
  lcd.print(scale.get_units(),2);  
  lcd.setCursor (12, 1);
  lcd.print("KG");
#endif

//buttonState = digitalRead(buttonTare);

//if (buttonState == LOW) {    
//      scale.tare();
//      buttonState= 0;
//      digitalWrite(13,HIGH); 
//  } else {   
//      digitalWrite(13,LOW);
//  }
  delay(1000);
}