#include <Keypad.h>
#include <LiquidCrystal.h>
// LCD pin setup
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Keypad setup
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
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int num1 = 0;
int num2 = 0;
int sum = 0;
bool firstNumberEntered = false;
void setup() {
lcd.begin(16, 2); // Initialize the LCD, 16 columns by 2 rows
lcd.print("Enter 1st num:");
}
void loop() {
char key = keypad.getKey(); // Get the key pressed
if (key) { // If a key is pressed
if (key >= '0' && key <= '9') { // If the key is a number
lcd.print(key); // Display the key on the LCD
if (!firstNumberEntered) {
num1 = num1 * 10 + (key - '0'); // Store the first number
} else {
num2 = num2 * 10 + (key - '0'); // Store the second number
}
} else if (key == '#') { // '#' to enter the next number or calculate sum
if (!firstNumberEntered) {
firstNumberEntered = true; // Indicate the first number is entered
lcd.setCursor(0, 1);
lcd.print("Enter 2nd num:");
} else {
sum = num1 + num2; // Calculate sum
lcd.clear();
lcd.setCursor(0, 0); // Set cursor to the first row, first column
lcd.print("Sum: "); // Display "Sum: "
lcd.print(sum); // Display the sum
}
} else if (key == '*') { // '*' to reset
num1 = 0;
num2 = 0;
sum = 0;
firstNumberEntered = false;
lcd.clear();
lcd.print("Enter 1st num:");
}
}
}