#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

const byte ROWS = 4; //4 baris
const byte COLS = 4; //4 kolom

char keys [ROWS] [COLS] = {
  {'1', '2', '3', 'A'},
    {'4', '5', '6', 'B'}, 
      {'7', '8', '9', 'C'},
        {'*','0','#','D'}
        };

        byte colPins[COLS] = {5, 4, 3, 2};
        byte rowPins[ROWS] = {9, 8, 7, 6};

        Keypad keypad = Keypad (makeKeymap (keys), rowPins, colPins, ROWS, COLS);

        char stringAngka [17];
        int indexKeypad = 0;

        void setup(){
          Serial.begin(9600);
            lcd.init();
              lcd.begin(16,2);
                lcd.backlight();
                  lcd.setCursor(4,0);
                    lcd.print("Tutorial");
                      lcd.setCursor(0,1);
                        lcd.print("Keypad 4x4 & LCD");
                          delay(2000);
                            lcd.clear();
                            }


                            void loop(){
                              lcd.setCursor(4,0);
                                lcd.print("Keypad LCD");
                                  lcd.setCursor(0,1);
                                    lcd.print("Tombol keypad: ");

                                      char key = keypad.getKey();
                                        if (key) {
                                          Serial.println(key);
                                            switch (key)
                                            {
                                            case '0':
                                            case '1':
                                            case '2':
                                            case '3':
                                            case '4':
                                            case '5':
                                            case '6':
                                            case '7':
                                            case '8':
                                            case '9':
                                            case 'A':
                                            case 'B':
                                            case 'C':
                                            case 'D':
                                            case '#':
                                               if (!indexKeypad) 
                                               {
                                                 lcd.clear();
                                                   }
                                                     stringAngka [indexKeypad++] = key;
                                                       lcd.setCursor(15,1);
                                                         lcd.println(key);
                                                           break;
                                                             case '*':
                                                               lcd.clear();
                                                                 indexKeypad = 0;
                                                                   break;

                                                                   }
                                                                     }
                                                                     }