#include <Keypad.h>
#include <LiquidCrystal_I2C.h> // If using I2C LCD
// Initialize the LCD (I2C address 0x27 might vary, check your display address)
LiquidCrystal_I2C lcd(0x27, 16, 2); // 16x2 LCD display
// Define the keypad layout
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'}, // Use '*' for multiplication
{'C', '0', '=', 'X'} // 'C' for decimal point, 'X' for division
};
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 object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Variables for calculator
String input = "";
float num1 = 0, num2 = 0;
char operation;
bool resultDisplayed = false; // Track if a result is displayed
// Push button pin for erase
const int eraseButtonPin = 10; // Pin for the erase button
void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight for the LCD
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0); // Set the cursor to the first row, first column
lcd.print("Calculator"); // Display initial text
delay(2000); // Wait for 2 seconds
lcd.clear(); // Clear the screen
// Setup the button as input
pinMode(eraseButtonPin, INPUT_PULLUP); // Use internal pull-up resistor
}
void loop() {
char key = keypad.getKey(); // Get the key pressed on the keypad
// Check if the erase button is pressed
if (digitalRead(eraseButtonPin) == LOW) {
input = ""; // Clear the input string
resultDisplayed = false; // Reset the result flag
lcd.clear(); // Clear the screen
lcd.setCursor(0, 0);
lcd.print("Cleared"); // Indicate cleared
delay(300); // Debounce delay for the button
}
if (key) {
// If result was displayed and a number is pressed, start a new calculation
if (resultDisplayed && (key >= '0' && key <= '9')) {
input = ""; // Reset input for a new calculation
resultDisplayed = false;
lcd.clear(); // Clear the screen for new input
}
// Handling number input
if (key >= '0' && key <= '9') {
input += key; // Append the pressed key to the input string
lcd.setCursor(0, 1); // Move to the second row
lcd.print(input); // Display the input on the LCD
}
// Handling decimal point input
else if (key == 'C') {
if (input.indexOf('.') == -1) { // Prevent multiple decimal points
input += '.'; // Append the decimal point
lcd.setCursor(0, 1);
lcd.print(input);
}
}
// Handling operator input (+, -, *, X)
else if (key == 'X' || key == '+' || key == '-' || key == '*') {
if (!resultDisplayed) {
num1 = input.toFloat(); // Convert the input string to a float
}
operation = key; // Store the operation
input = ""; // Clear the input for the next number
lcd.clear(); // Clear the screen
lcd.print(num1); // Display the first number
lcd.print(operation); // Display the operation
}
// Handling equal sign and calculation
else if (key == '=') {
num2 = input.toFloat(); // Convert the second number
lcd.clear();
lcd.print(num1);
lcd.print(operation);
lcd.print(num2);
// Perform the calculation
float result;
if (operation == '+') {
result = num1 + num2;
} else if (operation == '-') {
result = num1 - num2;
} else if (operation == '*') {
result = num1 * num2;
} else if (operation == 'X') {
if (num2 != 0) {
result = num1 / num2;
} else {
lcd.clear();
lcd.print("Error: Div/0");
return;
}
}
// Display the result with full precision
lcd.setCursor(0, 1); // Move to the second row
lcd.print("= ");
lcd.print(result, 6); // Print the result with up to 6 decimal places (maximum precision for float)
num1 = result; // Store result as num1 for further calculations
resultDisplayed = true; // Set the flag to indicate the result is displayed
}
}
}