// Q2 write a program for the computation of your BMI (Body mass index):
// bmi = body mass/ body height
// conditions
// 1. body mass can be taken as a serial input
// 2. height can be computed through an appropriate sensor.
// 3. display bmi on lcd.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define TRIG_PIN 9
#define ECHO_PIN 10
LiquidCrystal_I2C lcd(0x27, 16, 2);
float bodyMass = 0;
float height = 0;
float bmi = 0;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.backlight();
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
lcd.setCursor(0, 0);
lcd.print("Enter mass:");
}
void loop() {
if (Serial.available() > 0) {
bodyMass = Serial.parseFloat();
Serial.println("Mass received.");
height = measureHeight();
bmi = computeBMI(bodyMass, height);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("BMI: ");
lcd.print(bmi);
lcd.setCursor(0, 1);
lcd.print("Mass: ");
lcd.print(bodyMass);
lcd.print(" kg");
delay(2000);
}
}
float measureHeight() {
long duration;
float distanceMeters;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distanceMeters = (duration * 0.0343) / 2 / 100;
return distanceMeters;
}
float computeBMI(float mass, float height) {
if (height > 0) {
return mass / (height * height);
} else {
return 0;
}
}