/*
  Arduino | hardware-help
  Keypad calculator
  Zeruxe — 6/16/24 at 7:25 PM

  From this not-so-good example:
  https://www.hackster.io/nolimitcircuit/arduinolator-an-arduino-calculator-b544ff
*/

#include <Keypad.h>
#include <LiquidCrystal.h>

const byte ROWS = 4;
const byte COLS = 4;
const char keys [ROWS] [COLS] = {
  {'1', '2', '3', '+'},
  {'4', '5', '6', '-'},
  {'7', '8', '9', '*'},
  {'C', '0', '=', '/'}
};
const byte rowPins[ROWS] = {23, 25, 27, 29};
const byte colPins[COLS] = {31, 33, 35, 37};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

boolean presentValue = false;
boolean next = false;
boolean final = false;
String num1, num2;
float answer;
char op;

bool isNumber = false;
bool isOperand = false;

void setup()  {
  Serial.begin(9600);
  lcd.begin(16, 2); //LCD starts
  lcd.setCursor(4, 0);
  lcd.print("Arduino");
  lcd.setCursor(3, 1);
  lcd.print("Calculator");
  delay(1000);
  lcd.clear();

}

void loop() {

  char key = keypad.getKey();

  if (key != NO_KEY) {
    Serial.print(key);
    if (key >= '0' && key <= '9') {
      Serial.println(" is a number.");
      lcd.print(key);
      isNumber = true;
      /*
        if (presentValue != true)
        {
        //num1 = num1 + key;
        num1 = num1 + String(key);
        int numLength = num1.length();
        lcd.setCursor(15 - numLength, 0); //to adjust one whitespace for operator
        lcd.print(num1);
        }
        else
        {
        num2 = num2 + String(key);
        int numLength = num2.length();
        lcd.setCursor(15 - numLength, 1);
        lcd.print(num2);
        final = true;
        }
      */

    } else {
      Serial.println(" is an operator.");
      isOperand = true;
      switch (key)  {
        case 'C':
          Serial.println("Pressed Clr");
          lcd.clear();
          presentValue = false;
          final = false;
          num1 = "";
          num2 = "";
          answer = 0;
          op = ' ';
          break;
        case '=':
          Serial.println("Pressed =");
          //showAnswer();
          //float dVal = atof("122");
          //float eVal = atof("11");
          //float ansVal = dVal / eVal;
          //Serial.println(ansVal);

          lcd.clear();
          lcd.setCursor(10, 0);
          lcd.print(answer);
          break;
        case '+':
          Serial.println("Pressed +");
          answer = num1.toInt() + num2.toInt();
          break;
        case '-':
          Serial.println("Pressed -");
          answer = num1.toInt() - num2.toInt();
          break;
        case '*':
          Serial.println("Pressed *");
          answer = num1.toInt() * num2.toInt();
          break;
        case '/':
          Serial.println("Pressed /");
          answer = num1.toInt() / num2.toInt();
          break;
        default:
          Serial.println("Huh?");
          break;
      }
    }

    /*
        //if (key != NO_KEY && (key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' || key == '7' || key == '8' || key == '9' || key == '0'))
        if (isNumber)
        {
          if (presentValue != true)
          {
            //num1 = num1 + key;
            num1 = num1 + String(key);
            int numLength = num1.length();
            lcd.setCursor(15 - numLength, 0); //to adjust one whitespace for operator
            lcd.print(num1);
          }
          else
          {
            num2 = num2 + String(key);
            int numLength = num2.length();
            lcd.setCursor(15 - numLength, 1);
            lcd.print(num2);
            final = true;
          }
        }
        if (isOperand)
          //else if (presentValue == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+'))
        {
          if (presentValue == false)
          {
            presentValue = true;
            op = key;
            lcd.setCursor(15, 0);
            lcd.print(op);
          }
        }
    */
    /*
        else if (final == true && key != NO_KEY && key == '=') {
          Serial.println("Equal pressed");
          if (op == '+') {
            answer = num1.toInt() + num2.toInt();
          }
          else if (op == '-') {
            answer = num1.toInt() - num2.toInt();
          }
          else if (op == '*') {
            answer = num1.toInt() * num2.toInt();
          }
          else if (op == '/') {
            answer = num1.toInt() / num2.toInt();
            //float dVal = atof("122");
            //float eVal = atof("11");
            //float ansVal = dVal / eVal;
            //Serial.println(ansVal);
          }
          lcd.clear();
          //lcd.setCursor(0, 0);
          lcd.setCursor(10, 0);
          //lcd.autoscroll();
          lcd.print(answer);
          //lcd.noAutoscroll();
        }
        else if (key != NO_KEY && key == 'C') {
          lcd.clear();
          presentValue = false;
          final = false;
          num1 = "";
          num2 = "";
          answer = 0;
          op = ' ';
        }
      }
    */
  }
}
$abcdeabcde151015202530354045505560fghijfghij