#include <Keypad.h>
#include <LiquidCrystal.h>
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns
const int rs = A5, en = A4, d4 = 13, d5 = 12, d6 = 11, d7 = 10;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3', '+'},
{'4','5','6', '-'},
{'7','8','9', '*'},
{'.','0','=', '/'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
String input = "";
float num1 = 0;
float num2 = 0;
char op = 0;
boolean newInput = false;
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key >= '0' && key <= '9' || key == '.') {
input += key;
Serial.print(key);
lcd.print(key);
} else if (key == '+' || key == '-' || key == '*' || key == '/') {
num1 = input.toFloat();
op = key;
input = "";
newInput = true;
// lcd.setCursor(0, 1);
Serial.print(key);
lcd.print(key);
} else if (key == '=') {
if (newInput) {
num2 = input.toFloat();
float result;
switch (op) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
lcd.setCursor(0, 1);
Serial.print(" = " + String(result) + "\n");
lcd.print("= " + String(result));
newInput = false;
input = "";
}
}
}
}