#include "HX711.h"
const int pinDOUT = 2;
const int pinSCK = 4;
const int triggerPin = 5;
const int echoPin = 19;

int tinggiMax = 400; //400cm atau 4 m
long duration;
float distance;// Calculating the distance
float bmi;

HX711 scale;

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(115200);
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  scale.begin(pinDOUT, pinSCK);
  scale.set_scale(0.42);
  scale.tare();

  lcd.init();
  lcd.backlight();
  // UNTUK TEXT BOOTING AWAL

  lcd.setCursor(3,0);
  lcd.print("l");delay(500); lcd.print("o");delay(500);
  lcd.print("a");delay(500); lcd.print("d");delay(500);
  lcd.print("i");delay(500); lcd.print("n");delay(500);
  lcd.print("g");delay(500); lcd.print(".");delay(500);
  lcd.print(".");delay(500); lcd.print(".");delay(500);
  lcd.clear();
  lcd.setCursor(3,0);
  lcd.print("l");delay(500); lcd.print("o");delay(500);
  lcd.print("a");delay(500); lcd.print("d");delay(500);
  lcd.print("i");delay(500); lcd.print("n");delay(500);
  lcd.print("g");delay(500); lcd.print(".");delay(500);
  lcd.print(".");delay(500); lcd.print(".");delay(500);
  delay(1500);

  lcd.setCursor(2,0);
  lcd.print("ROBOT KOBONG");
  lcd.setCursor(6,1);
  lcd.print("SIJA");
  delay(3000);
  lcd.clear();

}

void loop() {

  int distance;
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(8);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = 0.0001*(duration * 343) / 2;

  if (scale.is_ready()) {
    long reading = scale.get_units(3);
    float kg = float(reading) / 1000;

    // Display weight and height on LCD
    lcd.setCursor(0,0);
    lcd.print("BB:");
    lcd.print(kg);
    lcd.print(" kg");
    lcd.setCursor(0,1);
    lcd.print("TB:");
    lcd.print(distance);
    lcd.print(" cm");

    // Calculate and display BMI
    float height = distance / 100;
    float bmi = kg / (height * height);
    // lcd.setCursor(12,1);
    Serial.print("\nBMI:");
    Serial.print(bmi, 2);

    // Check BMI range and display message
    if (bmi < 18.5) {
      // lcd.setCursor(12,0);
      Serial.print("Kurus");
    } else if (bmi > 18.5 && bmi < 24.9) {
      // lcd.setCursor(12,0);
      Serial.print("Sehat");
    } else if (bmi > 25 && bmi < 29.9) {
      // lcd.setCursor(12,0);
      Serial.print("Obesitas");
    }
  }

}