#include <Keypad.h>
#include <LiquidCrystal.h>
int num1add;
int sumadd;
int num2add;
int num1sub;
int sumsub;
int num2sub;
int num1mul;
int num2mul;
int summul;
float num1div;
int num2div;
float sumdiv;
int rs=10;
int en=11;
int d4=12;
int d5=13;
int d6=A0;
int d7=A1;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
String prevOp="q";
// Define the keypad
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // Connect to the column pinouts of the keypad
// Create the Keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String inputString = ""; // String to hold the number
void setup() {
Serial.begin(9600); // Start the serial communication
Serial.println('hi');
lcd.begin(16,2);
}
void loop() {
char key = keypad.getKey(); // Get the key pressed
inputString.trim();
int inputNumbar =inputString.toInt();
if (key) {
if (key == '#'&&prevOp=="q") { // If '#' is pressed, print the number and reset the string
Serial.println(inputNumbar);
inputString = "";
lcd.clear();
lcd.print(inputNumbar);
// Clear the input string
}
if(key=='#'&& prevOp=="+"){
num2add=inputNumbar;
sumadd=num1add+num2add;
Serial.println(sumadd);
prevOp="q";
inputString="";
lcd.clear();
lcd.print(sumadd);
}
if(key=='#' && prevOp=="-"){
num2sub=inputNumbar;
sumsub=num1sub-num2sub;
Serial.println(sumsub);
prevOp="q";
inputString="";
lcd.clear();
lcd.print(sumsub);
}
if(key=='#' && prevOp=="*"){
num2mul=inputNumbar;
summul=num1mul*num2mul;
Serial.println(summul);
inputString="";
lcd.clear();
lcd.print(summul);
}
if(key=='#' && prevOp=="/"){
num2div=inputNumbar;
sumdiv=num1div/num2div;
Serial.println(sumdiv);
inputString="";
lcd.clear();
lcd.print(sumdiv);
}
else if (key >= '0' && key <= '9') { // If a number is pressed, add it to the string
inputString += key;
lcd.print(key);
}
else if(key=='A'){
num1add=inputNumbar;
inputString="";
prevOp="+";
lcd.clear();
}
else if(key=='B'){
num1sub=inputNumbar;
inputString="";
prevOp="-";
lcd.clear();
}
else if(key=='C'){
num1mul=inputNumbar;
inputString="";
prevOp="*";
lcd.clear();
}
else if(key=='D'){
num1div=inputNumbar;
inputString="";
prevOp="/";
lcd.clear();
}
else if(key=='*'){
inputString="";
prevOp="q";
num1add=0;
sumadd=0;
num2add=0;
num1sub=0;
sumsub=0;
num2sub=0;
num1mul=0;
num2mul=0;
summul=0;
num1div=0;
num2div=0;
sumdiv=0;
lcd.clear();
}
}
}