#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27,20,4);
int r1 = 13;
int r2 = 12;
int r3 = 14;
int r4 = 27;
int c1 = 26;
int c2 = 25;
int c3 = 33;
int c4 = 32;

void setup() {
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("You press key : ");

  pinMode(r1, OUTPUT);
  pinMode(r2, OUTPUT);
  pinMode(r3, OUTPUT);
  pinMode(r4, OUTPUT);
  pinMode(c1, INPUT_PULLUP);
  pinMode(c2, INPUT_PULLUP);
  pinMode(c3, INPUT_PULLUP);
  pinMode(c4, INPUT_PULLUP);
  digitalWrite(c1, HIGH);
  digitalWrite(c2, HIGH);
  digitalWrite(c3, HIGH);
  digitalWrite(c4, HIGH);
}

void loop() {
  row1();
  row2();
  row3();
  row4();
}

void row1() {
  lcd.setCursor(0,0);
  lcd.print("You press key : ");
  digitalWrite(r1, LOW);
  digitalWrite(r2, HIGH);
  digitalWrite(r3, HIGH);
  digitalWrite(r4, HIGH);

  if (digitalRead(c1) == LOW) {
    lcd.print("1");
    delay(100);
  }
  else if (digitalRead(c2) == LOW) {
    lcd.print("2");
    delay(100);
  }
  else if (digitalRead(c3) == LOW) {
    lcd.print("3");
    delay(100);
  }
  else if (digitalRead(c4) == LOW) {
    lcd.print("A");
    delay(100);
  }
}

void row2() {
  lcd.setCursor(0,0);
  lcd.print("You press key : ");
  digitalWrite(r1, HIGH);
  digitalWrite(r2, LOW);
  digitalWrite(r3, HIGH);
  digitalWrite(r4, HIGH);

  if (digitalRead(c1) == LOW) {
    lcd.print("4");
    delay(100);
  }
  else if (digitalRead(c2) == LOW) {
    lcd.print("5");
    delay(100);
  }
  else if (digitalRead(c3) == LOW) {
    lcd.print("6");
    delay(100);
  }
  else if (digitalRead(c4) == LOW) {
    lcd.print("B");
    delay(100);
  }
}

void row3() {
  lcd.setCursor(0,0);
  lcd.print("You press key : ");
  digitalWrite(r1, HIGH);
  digitalWrite(r2, HIGH);
  digitalWrite(r3, LOW);
  digitalWrite(r4, HIGH);

  if (digitalRead(c1) == LOW) {
    lcd.print("7");
    delay(100);
  }
  else if (digitalRead(c2) == LOW) {
    lcd.print("8");
    delay(100);
  }
  else if (digitalRead(c3) == LOW) {
    lcd.print("9");
    delay(100);
  }
  else if (digitalRead(c4) == LOW) {
    lcd.print("C");
    delay(100);
  }
}

void row4() {
  lcd.setCursor(0,0);
  lcd.print("You press key : ");
  digitalWrite(r1, HIGH);
  digitalWrite(r2, HIGH);
  digitalWrite(r3, HIGH);
  digitalWrite(r4, LOW);

  if (digitalRead(c1) == LOW) {
    lcd.print("*");
    delay(100);
  }
  else if (digitalRead(c2) == LOW) {
    lcd.print("0");
    delay(100);
  }
  else if (digitalRead(c3) == LOW) {
    lcd.print("#");
    delay(100);
  }
  else if (digitalRead(c4) == LOW) {
    lcd.print("D");
    delay(100);
  }
}