#include <Keypad.h> // library for keypad
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const byte ROWS = 4; //initializing the rows of keypad
const byte COLS = 4;//initializing the columns of keypad
char keys [ROWS] [COLS] = { // giving values to each key of the keypad
{'1', '2', '3', '&'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '=', 'D'}
};
byte rowPins[ROWS] = {23,13,19,18}; //Arduino pins for rows of keypad
byte colPins[COLS] = {5,4,2,15}; // Arduino pins for columns of keypad
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); /* function for mapping the values on the keys*/
// assigning boolean data type to the variables and initializing them with zero
boolean present = false;
boolean next = false;
boolean final = false;
String num1, num2; // variables to display the keypad integer input
float ans;// variable for storing the result of the BMI
char op;// assigning character data type for the character used for BMI
int ldr = 12;
int hasil, hasilMap;
void setup()
{
Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(ldr, INPUT);
lcd.setCursor(5,0); // setting the place for displaying the first data line
lcd.print("KELOMPOK 5"); //data to be displayed
lcd.setCursor(5,1);//setting the place for displaying the second data line
lcd.print("GlucoMeter");// data to be displayed
delay(2000);// time for which the data will be displayed on LCD
lcd.clear();// clearing the LCD
lcd.setCursor(6,0);// setting the place for displaying the second data line
lcd.print("Masukkan");//data to be displayed
lcd.setCursor(2,1);
lcd.print("Berat dan Tinggi:");
}
void loop(){
hasil = analogRead(ldr);
hasilMap = map(hasil, 0, 4095, 0, 350);
Serial.println(hasilMap);
// using the getkey function to get the value of the key pressed
char key = myKeypad.getKey();
if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0'))// check which key is pressed by checking its integer value
{
if (present != true)
{
num1 = num1 + key; // storing the value of key pressed in num1
float numLength = num1.length();
lcd.setCursor(0, 2); /* decaling the place where the first entry will be displayed*/
lcd.print(num1); // printing the first number entered
}
else
{
num2 = num2 + key;//storing the value of second key pressed in num2
float numLength = num2.length();
lcd.setCursor(4, 2);/*decaling the place where the second entry will be displayed*/
lcd.print(" ");
lcd.print(num2); //printing the second number entered
final = true;
}
}
// condition if key having the assigned operator for BMI calculation is pressed
else if (present == false && key != NO_KEY && (key == '&'))
{
if (present == false)
{
present = true;
op = key; // saving the key pressed for calculating the BMI
lcd.setCursor(3,2); // setting the place for the data to be displayed
lcd.print(op); // displaying the character used for BMI
}
}
// conditions to calculate the BMI
else if (final == true && key != NO_KEY && key == '='){
if (op == '&'){
ans = (num1.toFloat() / (num2.toFloat()*num2.toFloat()))*10000;// formula to calculate the BMI
}
// lcd.setCursor(0,2); // setting the place for the data to be displayed
// lcd.print("Your BMI is:");// data to be displayed
// lcd.print(ans); // displaying the answer for BMI calculation
lcd.clear();// clearing the LCD
if (ans<18.5){ // if BMI is less than 18.5 then you are under weight
if(hasilMap >= 70 && hasilMap <= 150){
lcd.setCursor(3,0);
lcd.print("Anda kekurangan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("0.4 dosis");}
else if(hasilMap >= 151 && hasilMap <= 175){
lcd.setCursor(3,0);
lcd.print("Anda kekurangan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("1.4 dosis");}
else if(hasilMap >= 176 && hasilMap <= 200){
lcd.setCursor(3,0);
lcd.print("Anda kekurangan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("2.4 dosis");}
else if(hasilMap >= 201 && hasilMap <= 225){
lcd.setCursor(3,0);
lcd.print("Anda kekurangan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("3.4 dosis");}
else if(hasilMap >= 226 && hasilMap <= 250){
lcd.setCursor(3,0);
lcd.print("Anda kekurangan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("5.5 dosis");}
else if(hasilMap >= 251 && hasilMap <= 275){
lcd.setCursor(3,0);
lcd.print("Anda kekurangan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("7.5 dosis");}
else if(hasilMap >= 276 && hasilMap <= 300){
lcd.setCursor(3,0);
lcd.print("Anda kekurangan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("9.4 dosis");}
else if(hasilMap > 300){
lcd.setCursor(3,0);
lcd.print("Anda kekurangan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("12.4 dosis");}
else {
lcd.setCursor(3,0);
lcd.print("Anda kekurangan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Tidak perlu insulin");
}
}
else if(ans<25){// if BMI is less than 25 then you have normal weight
if(hasilMap >= 70 && hasilMap <= 150){
lcd.setCursor(0,0);
lcd.print("Berat badan normal");
lcd.setCursor(0,2);
lcd.print("Anda Butuh Insulin:");
lcd.setCursor(0,3);
lcd.print("0.4 dosis");}
else if(hasilMap >= 151 && hasilMap <= 175){
lcd.setCursor(0,0);
lcd.print("Berat badan normal");
lcd.setCursor(0,2);
lcd.print("Anda Butuh Insulin:");
lcd.setCursor(0,3);
lcd.print("1.4 dosis");}
else if(hasilMap >= 176 && hasilMap <= 200){
lcd.setCursor(0,0);
lcd.print("Berat badan normal");
lcd.setCursor(0,2);
lcd.print("Anda Butuh Insulin:");
lcd.setCursor(0,3);
lcd.print("2.4 dosis");}
else if(hasilMap >= 201 && hasilMap <= 225){
lcd.setCursor(0,0);
lcd.print("Berat badan normal");
lcd.setCursor(0,2);
lcd.print("Anda Butuh Insulin:");
lcd.setCursor(0,3);
lcd.print("3.4 dosis");}
else if(hasilMap >= 226 && hasilMap <= 250){
lcd.setCursor(0,0);
lcd.print("Berat badan normal");
lcd.setCursor(0,2);
lcd.print("Anda Butuh Insulin:");
lcd.setCursor(0,3);
lcd.print("5.5 dosis");}
else if(hasilMap >= 251 && hasilMap <= 275){
lcd.setCursor(0,0);
lcd.print("Berat badan normal");
lcd.setCursor(0,2);
lcd.print("Anda Butuh Insulin:");
lcd.setCursor(0,3);
lcd.print("7.4 dosis");}
else if(hasilMap >= 276 && hasilMap <= 300){
lcd.setCursor(0,0);
lcd.print("Berat badan normal");
lcd.setCursor(0,2);
lcd.print("Anda Butuh Insulin:");
lcd.setCursor(0,3);
lcd.print("9.4 dosis");}
else if(hasilMap > 300){
lcd.setCursor(0,0);
lcd.print("Berat badan normal");
lcd.setCursor(0,2);
lcd.print("Anda Butuh Insulin:");
lcd.setCursor(0,3);
lcd.print("12.4 dosis");
}
else {
lcd.setCursor(0,0);
lcd.print("Berat badan normal");
lcd.setCursor(0,2);
lcd.print("Tidak perlu insulin");
}
}
else if (ans<30){ //if BMI is less than 30 then you are over weight
if(hasilMap >= 70 && hasilMap <= 150){
lcd.setCursor(3,0);
lcd.print("Anda kelebihan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("0.5 dosis");}
else if(hasilMap >= 151 && hasilMap <= 175){
lcd.setCursor(3,0);
lcd.print("Anda kelebihan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("2.5 dosis");}
else if(hasilMap >= 176 && hasilMap <= 200){
lcd.setCursor(3,0);
lcd.print("Anda kelebihan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("4.4 dosis");}
else if(hasilMap >= 201 && hasilMap <= 225){
lcd.setCursor(3,0);
lcd.print("Anda kelebihan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("6.6 dosis");}
else if(hasilMap >= 226 && hasilMap <= 250){
lcd.setCursor(3,0);
lcd.print("Anda kelebihan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("8.5 dosis");}
else if(hasilMap >= 251 && hasilMap <= 275){
lcd.setCursor(3,0);
lcd.print("Anda kelebihan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("10.5 dosis");}
else if(hasilMap >= 276 && hasilMap <= 300){
lcd.setCursor(3,0);
lcd.print("Anda kelebihan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("12.5 dosis");}
else if(hasilMap > 300){
lcd.setCursor(3,0);
lcd.print("Anda kelebihan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("14.5 dosis");}
else {
lcd.setCursor(3,0);
lcd.print("Anda kelebihan");
lcd.setCursor(4,1);
lcd.print("berat badan");
lcd.setCursor(0,2);
lcd.print("Tidak perlu insulin");
}
}
else{ // else you are obese
if(hasilMap >= 70 && hasilMap <= 150){
lcd.setCursor(3,0);
lcd.print("Anda obesitas");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("0.6 dosis");}
else if(hasilMap >= 151 && hasilMap <= 175){
lcd.setCursor(3,0);
lcd.print("Anda obesitas");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(3,6);
lcd.print("2.5 dosis");}
else if(hasilMap >= 176 && hasilMap <= 200){
lcd.setCursor(3,0);
lcd.print("Anda obesitas");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("6.6 dosis");}
else if(hasilMap >= 201 && hasilMap <= 225){
lcd.setCursor(3,0);
lcd.print("Anda obesitas");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("7.6 dosis");}
else if(hasilMap >= 226 && hasilMap <= 250){
lcd.setCursor(3,0);
lcd.print("Anda obesitas");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("9.6 dosis");}
else if(hasilMap >= 251 && hasilMap <= 275){
lcd.setCursor(3,0);
lcd.print("Anda obesitas");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("11.6 dosis");}
else if(hasilMap >= 276 && hasilMap <= 300){
lcd.setCursor(3,0);
lcd.print("Anda obesitas");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("14.6 dosis");}
else if(hasilMap > 300){
lcd.setCursor(3,0);
lcd.print("Anda obesitas");
lcd.setCursor(0,2);
lcd.print("Anda butuh insulin:");
lcd.setCursor(0,3);
lcd.print("18.6 dosis");}
else {
lcd.setCursor(3,0);
lcd.print("Anda obesitas");
lcd.setCursor(0,2);
lcd.print("Tidak perlu insulin");
}
}
delay(2000);// time or which the data will be displayed
}
// condition for clearing the LCD or starting the Arduino code again
else if (final == true && key != NO_KEY && key == '*'){
lcd.clear();
lcd.setCursor(1,1); // setting the place for the data to be displayed
lcd.print("BMI anda: ");// data to be displayed
lcd.print(ans); // displaying the answer for BMI calculation
lcd.setCursor(1,2); // setting the place for the data to be displayed
lcd.print("Glukosa: ");// data to be displayed
lcd.print(hasilMap);
lcd.print("mg/dL");
}
else if (key != NO_KEY && key == 'C'){
lcd.clear();
present = false;
final = false;
setup();// calling the setup function to start the code again
num1 = "";
num2 = "";
ans = 0;
op = ' ';
}
}