#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address and LCD size (20x4)
// Define button pins
const int buttonPin0 = 2;
const int buttonPin1 = 15;
const int buttonPin2 = 4;
const int buttonPin3 = 5;
const int buttonPin4 = 14;
const int buttonPin5 = 27;
const int buttonPin6 = 26;
const int buttonPin7 = 25;
const int buttonPin8 = 33;
const int buttonPin9 = 32;
const int buttonPlus = 12;
const int buttonMinus = 13;
const int buttonMul = 18;
const int buttonDiv = 19;
const int buttonPercentage = 23;
const int buttonEnter = 35; // GPIO pin for the enter button
const int buttonreset = 34; // GPIO pin for the reset button
String inputString = "";
String expression = "";
bool inputReady = false;
bool isOperator(char c)
{
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '%');
}
double calculate(double num1, double num2, char operation)
{
switch (operation)
{
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '%':
return num2 * (num1 / 100);
case '/':
if (num2 != 0)
return num1 / num2;
else
{
// Handle division by zero
lcd.setCursor(0, 3);
lcd.print("Error: Division by zero.");
delay(200);
return 0;
}
default:
// Handle invalid operation
lcd.setCursor(0, 3);
lcd.print("Error: Invalid operation.");
delay(200);
return 0;
}
}
void setup()
{
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Calculator:");
// Set button pins as INPUT_PULLUP
pinMode(buttonPin0, INPUT_PULLUP);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(buttonPin5, INPUT_PULLUP);
pinMode(buttonPin6, INPUT_PULLUP);
pinMode(buttonPin7, INPUT_PULLUP);
pinMode(buttonPin8, INPUT_PULLUP);
pinMode(buttonPin9, INPUT_PULLUP);
pinMode(buttonPlus, INPUT_PULLUP);
pinMode(buttonMinus, INPUT_PULLUP);
pinMode(buttonMul, INPUT_PULLUP);
pinMode(buttonDiv, INPUT_PULLUP);
pinMode(buttonPercentage, INPUT_PULLUP);
pinMode(buttonEnter, INPUT_PULLUP);
pinMode(buttonreset, INPUT_PULLUP);
}
void loop()
{
checkButtons();
if (inputReady)
{
// Evaluate the expression and display the result
double result = evaluateExpression(expression);
lcd.setCursor(0, 2);
lcd.print("Result: " + String(result));
// Clear the input and expression for the next calculation
inputString = "";
expression = "";
inputReady = false;
}
}
void checkButtons()
{
while (digitalRead(buttonEnter) == HIGH)
{
// Wait for the "Enter" button to be pressed
}
if (digitalRead(buttonreset) == LOW)
{
// Reset button is pressed, clear input and expression
inputString = "";
expression = "";
lcd.setCursor(0, 1);
lcd.print("Input: "); // Clear input display
lcd.setCursor(0, 2);
lcd.print("Result: "); // Clear result display
lcd.setCursor(0, 3);
lcd.print("Expression: "); // Clear expression display
delay(200);
}
// Capture the entire expression
inputString = "";
while (digitalRead(buttonEnter) == LOW)
{
if (digitalRead(buttonPin0) == LOW)
{
inputString += "0";
}
if (digitalRead(buttonPin1) == LOW)
{
inputString += "1";
}
if (digitalRead(buttonPin2) == LOW)
{
inputString += "2";
}
if (digitalRead(buttonPin3) == LOW)
{
inputString += "3";
}
if (digitalRead(buttonPin4) == LOW)
{
inputString += "4";
}
if (digitalRead(buttonPin5) == LOW)
{
inputString += "5";
}
if (digitalRead(buttonPin6) == LOW)
{
inputString += "6";
}
if (digitalRead(buttonPin7) == LOW)
{
inputString += "7";
}
if (digitalRead(buttonPin8) == LOW)
{
inputString += "8";
}
if (digitalRead(buttonPin9) == LOW)
{
inputString += "9";
}
if (digitalRead(buttonPlus) == LOW)
{
inputString += "+";
}
if (digitalRead(buttonMinus) == LOW)
{
inputString += "-";
}
if (digitalRead(buttonMul) == LOW)
{
inputString += "*";
}
if (digitalRead(buttonDiv) == LOW)
{
inputString += "/";
}
if (digitalRead(buttonPercentage) == LOW)
{
inputString += "%";
}
// Update the LCD while capturing the expression
lcd.setCursor(0, 1);
lcd.print("Input: " + inputString);
delay(200);
}
// Store and display the captured expression
expression = inputString;
lcd.setCursor(0, 3);
lcd.print("Expression: " + expression);
inputReady = true;
}
double evaluateExpression(String expression)
{
double result = 0.0;
char operation = '+';
String currentNumber = "";
for (char c : expression)
{
if (isOperator(c))
{
// An operator is encountered, calculate the result with the current number and operator
double num = atof(currentNumber.c_str());
result = calculate(result, num, operation);
// Reset the current number and update the current operation
currentNumber = "";
operation = c;
}
else
{
// Append the character to the current number
currentNumber += c;
}
}
// Calculate the result with the last number in the expression
double num = atof(currentNumber.c_str());
result = calculate(result, num, operation);
return result;
}