#include <Keypad.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27
#define LCD_COLUMNS 16
#define LCD_LINES   2

uint8_t state;
uint8_t valIndex;
uint8_t cursorPos;
char entered_value [6];
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 1, 0, 3, 2 };
uint8_t rowPins[ROWS] = { 4, 5, 6, 7 };

LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);

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

String po="";
String ga="";
int spo=0;
int sga=0;
int sopo=10000;
int soga=10000;

void setup() 
{
  
  Wire.begin(18, 19);
  lcd.init();
  lcd.backlight();
  lcd.clear();
  lcd.print("Bo: ");
  lcd.setCursor(0, 1);
  lcd.print("Ga: ");
}

void loop() 
{
  char key = keypad.getKey();

switch (key) {
  case 'A':
    spo = 1;
    sga = 0;
    break;
  case 'B':
    spo = 0;
    sga = 1;
    break;
  case 'C':
    po = "";
    ga = "";
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Bo: ");
    lcd.print(po);
    lcd.setCursor(0, 1);
    lcd.print("Ga: ");
    lcd.print(ga);
    break;
  case 'D':
    lcd.clear();
    spo = 0;
    sga = 0;
    if(po==""||ga=="")
    {
      lcd.clear();
      lcd.setCursor(1, 0);
      lcd.print("Vui long nhap");
      lcd.setCursor(0, 1);
      lcd.print("du lieu day du!");
    }
    else
    {
      lcd.clear();
      lcd.setCursor(2, 0);
      lcd.print("Luu du lieu");
      lcd.setCursor(2, 1);
      lcd.print("thanh cong!");
      sopo = po.toInt();
      soga = ga.toInt();      
    }
    delay(3000);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Bo: ");
      lcd.print(po);
      lcd.setCursor(0, 1);
      lcd.print("Ga: ");
      lcd.print(ga);
    break;
  case '1':
  case '2':
  case '3':
  case '4':
  case '5':
  case '6':
  case '7':
  case '8':
  case '9':
  case '0':
    if (spo == 1) {
      po += key;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Bo: ");
      lcd.print(po);
      lcd.setCursor(0, 1);
      lcd.print("Ga: ");
      lcd.print(ga);
    }
    if (sga == 1) {
      ga += key;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Bo: ");
      lcd.print(po);
      lcd.setCursor(0, 1);
      lcd.print("Ga: ");
      lcd.print(ga);
    }
    break;
  default:
    // Không làm gì cả
    break;
}

}