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

const uint8_t ROWS = 4;
const uint8_t  COLS = 4;

uint8_t buttonPin = 10;
unsigned long lastButtonPress = 0;
const int debounceDelay = 200;

char leftNum[10] = {'\0'};
int sizeLeft = 0;
char rightNum[10] = {'\0'};
int sizeRight = 0;

char key;

bool operatorState = false;

char lastOperator = '\0';

long result = 0;
long numberLeft = 0;
long numberRight = 0;

LiquidCrystal_I2C lcd(0x27, 20, 4);

char keys[ROWS][COLS] = {
  {'1', '2', '3', '+'},
  {'4', '5', '6', '-'},
  {'7', '8', '9', '*'},
  {'c', '0', '=', '/'}
};

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

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

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  Wire.begin();
  lcd.init();
  lcd.backlight();
}

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

  if (key)
    switch (key) {
      case '+':
      case '-':
      case '*':
      case '/':
        if (operatorState == false) {
          numberLeft = atol(leftNum);  
          operatorState = true;
          lastOperator = key;  
        }
        break;

      case '=':
        if (operatorState) {
          numberRight = atol(rightNum); 
          mathOperator(lastOperator);    
          lcd.setCursor(0, 2);
          lcd.print(result);
          operatorState = false;  
        }
        break;
      case 'c':
        clearNum();
        lcd.clear();
        break;
      default:
        addNum(key);
    }

  lcd.setCursor(0, 0);
  lcd.print(leftNum);
  lcd.setCursor(0, 1);
  lcd.print(rightNum);
}

void backSpaceButton() {
  char* targetNum = operatorState ? rightNum : leftNum;
  int& size = operatorState ? sizeRight : sizeLeft;

  if (digitalRead(buttonPin) == LOW) {
    if (millis() - lastButtonPress > debounceDelay) {
      lastButtonPress = millis();
      lcd.clear();
      if (size > 0) {
        size--;
        targetNum[size] = '\0';
      } else
        targetNum[0] = '\0';
    }
  }
}

void clearNum() {
  sizeLeft = 0;
  leftNum[sizeLeft] = '\0';
  sizeRight = 0;
  rightNum[sizeRight] = '\0';
  result = 0;
  numberLeft = 0;
  numberRight = 0;
  operatorState = false;
}

void addNum(char key) {
  char* targetNum = operatorState ? rightNum : leftNum;
  int& size = operatorState ? sizeRight : sizeLeft;

  if (size < 9) {
    targetNum[size++] = key;
    targetNum[size] = '\0';
  }
}

void mathOperator(char oper) {
  switch (oper) {
    case '+':
      result = numberLeft + numberRight;
      break;
    case '-':
      result = numberLeft - numberRight;
      break;
    case '*':
      result = numberLeft * numberRight;
      break;
    case '/':
      if (numberRight != 0) {
        result = numberLeft / numberRight;
      } else {
        lcd.setCursor(0, 2);
        lcd.print("ERROR");
      }
      break;
  }
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
keypad1:R1
keypad1:R2
keypad1:R3
keypad1:R4
keypad1:C1
keypad1:C2
keypad1:C3
keypad1:C4
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
Backspace
lcd2:GND
lcd2:VCC
lcd2:SDA
lcd2:SCL