#include <HX711.h>
#include <NewPing.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DT_PIN 4
#define SCK_PIN 5
#define TRIG_PIN 7
#define ECHO_PIN 6
HX711 scale;
NewPing sonar(TRIG_PIN, ECHO_PIN);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
scale.begin(DT_PIN, SCK_PIN);
scale.set_scale(420);
scale.tare();
sonar.ping();
lcd.begin(16, 2);
lcd.backlight();
delay(1000);
}
void loop() {
float weight = scale.get_units(10);
unsigned int uS = sonar.ping();
float height = uS / US_ROUNDTRIP_CM / 100.0;
if (height > 0) {
float bmi = weight / (height * height);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Weight: ");
lcd.print(weight, 2);
lcd.print(" kg");
lcd.setCursor(0, 1);
lcd.print("Height: ");
lcd.print(height, 2);
lcd.print(" m");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("BMI:");
lcd.setCursor(0, 1);
lcd.print(bmi, 1);
lcd.print(" kg/m^2");
delay(3000);
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Height error");
delay(3000);
}
delay(5000);
}