#include <Keypad.h>
#include <LiquidCrystal.h>
const byte ROW_NUM = 4;
const byte COLUMN_NUM = 4;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6};
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2};
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
const int rs = 12, en = 11, d4 = 10, d5 = A5, d6 = A4, d7 = A3;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
float height_ft = 0.0;
float weight = 0.0;
bool heightEntered = false;
bool weightEntered = false;
void setup() {
lcd.begin(16,2);
lcd.clear();
lcd.print("NCSM WELCOMES U");
delay(1000);
}
void loop() {
lcd.clear();
if (!heightEntered) {
lcd.print("Enter height (ft):");
lcd.setCursor(0, 1); // Move cursor to the second line
lcd.print(height_ft); // Print the current height
} else if (!weightEntered) {
lcd.print("Enter weight (kg):");
lcd.setCursor(0, 1); // Move cursor to the second line
lcd.print(weight); // Print the current weight
} else {
calculateBMI();
delay(2000); // Delay to display BMI result before resetting
height_ft = 0.0; // Reset height
weight = 0.0; // Reset weight
heightEntered = false; // Reset heightEntered flag
weightEntered = false; // Reset weightEntered flag
}
char key = keypad.getKey();
if (key) {
if (key == '#') {
if (!heightEntered) {
heightEntered = true;
} else if (!weightEntered) {
weightEntered = true;
}
} else if (key == '*') {
if (!heightEntered) {
height_ft /= 10;
} else if (!weightEntered) {
weight /= 10;
}
} else {
if (!heightEntered) {
height_ft = height_ft * 10 + (key - '0');
} else if (!weightEntered) {
weight = weight * 10 + (key - '0');
}
}
}
delay(100);
}
void calculateBMI() {
// Convert height from feet to meters
float height_m = height_ft * 0.3048; // 1 foot = 0.3048 meters
float bmi = weight / (height_m * height_m);
lcd.clear();
lcd.print("BMI: ");
lcd.print(bmi);
lcd.setCursor(0, 1); // Move cursor to the second line
if (bmi < 18.5) {
lcd.print("Underweight");
} else if (bmi >= 18.5 && bmi < 25) {
lcd.print("Fit n Fine");
} else {
lcd.print("Overweight");
}
}