```cpp
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 11, 10, 9, 8); // Initialize the LCD screen
// Variables to store the two numbers and the calculation result
long first_number = 0;
long second_number = 0;
double total_result = 0;
// Define the keypad size
char key_pressed;
const byte ROWS = 4;
const byte COLS = 4;
// Define the keys on the keypad
char keys[ROWS][COLS] = {
{'1','2','3','+'},
{'4','5','6','-'},
{'7','8','9','*'},
{'C','0','=','/'}
};
byte rowPins[ROWS] = {7,6,5,4}; // Connect to the row pins of the keypad
byte colPins[COLS] = {3,2,1,0}; // Connect to the column pins of the keypad
Keypad customKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
lcd.begin(16, 2); // Start the LCD
for(int i = 0; i <= 3; i++);
lcd.setCursor(0,0); // Move cursor to the beginning of the screen
lcd.print("Calculator");
lcd.setCursor(0,1);
lcd.print("Arduino Factory");
delay(4000);
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0);
}
void loop() {
key_pressed = customKeypad.getKey();
switch(key_pressed) {
case '0' ... '9': // Keep building the first number until an operator is pressed
lcd.setCursor(0,0);
first_number = first_number * 10 + (key_pressed - '0');
lcd.print(first_number);
break;
case '+': // Addition
first_number = (total_result != 0 ? total_result : first_number);
lcd.setCursor(0,1);
lcd.print("+");
second_number = getSecondNumber(); // Get the second number
total_result = first_number + second_number;
lcd.setCursor(0,3);
lcd.print(total_result);
first_number = 0; second_number = 0; // Reset for next calculation
break;
case '-': // Subtraction
first_number = (total_result != 0 ? total_result : first_number);
lcd.setCursor(0,1);
lcd.print("-");
second_number = getSecondNumber(); // Get the second number
total_result = first_number - second_number;
lcd.setCursor(0,3);
lcd.print(total_result);
first_number = 0; second_number = 0; // Reset for next calculation
break;
case '*': // Multiplication
first_number = (total_result != 0 ? total_result : first_number);
lcd.setCursor(0,1);
lcd.print("*");
second_number = getSecondNumber(); // Get the second number
total_result = first_number * second_number;
lcd.setCursor(0,3);
lcd.print(total_result);
first_number = 0; second_number = 0; // Reset for next calculation
break;
case '/': // Division
first_number = (total_result != 0 ? total_result : first_number);
lcd.setCursor(0,1);
lcd.print("/");
second_number = getSecondNumber(); // Get the second number
lcd.setCursor(0,3);
if (second_number == 0) {
lcd.print("Invalid");
} else {
total_result = (float)first_number / (float)second_number;
lcd.print(total_result);
}
first_number = 0; second_number = 0; // Reset for next calculation
break;
case 'C': // Reset the calculator
total_result = 0;
lcd.clear();
break;
}
}
long getSecondNumber() { // Function to capture the second number input
while (1) {
key_pressed = customKeypad.getKey();
if (key_pressed >= '0' && key_pressed <= '9') {
second_number = second_number * 10 + (key_pressed - '0');
lcd.setCursor(0,2);
lcd.print(second_number);
}
if (key_pressed == '=') break; // Finish entering second number
}
return second_number;
}
```