/*
Project: Calculator
Name: Kevin Hessel
Date: 23.10.2023
Libraries:
- LiquidCrystal.h
- Keypad.h
-----------------------------
*/
#include <Keypad.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(5, 4, 3, 2, 1, 0); //LCD pins
String num1, num2;
boolean gotNum1 = false;
boolean gotOp = false;
boolean showResult = false;
boolean keepCalculating = false;
char op;
float answer;
unsigned long pressStart;
int treshholdHold = 2000;
const byte ROWS = 4; //number of rows
const byte COLS = 4; //number of coloumns
char keys[ROWS][COLS] = {
{ '1', '2', '3', '+' },
{ '4', '5', '6', '-' },
{ '7', '8', '9', '*' },
{ 'C', '0', '#', '/' }
};
byte rowPins[ROWS] = { 13, 12, 11, 10 }; //row pins
byte colPins[COLS] = { 9, 8, 7, 6 }; //coloumn pins
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
lcd.begin(16, 2);
lcd.print(" Calculator");
delay(2000);
lcd.clear();
}
//prints the answer on the lcd and sets it in the top right corner - 1
//answer first needs to be converted into a string, then count the length of the string, then convert the new value back into an integer and subtract it
void printAnswer(float answerToPrint) {
String StringAnswer;
int answerLength;
lcd.clear();
StringAnswer = String(answerToPrint, 7);
answerLength = StringAnswer.length();
lcd.setCursor(15 - answerLength, 0);
lcd.print(answerToPrint, 7);
lcd.setCursor(0, 0);
lcd.print("=");
keepCalculating = true;
gotOp = false;
gotNum1 = true;
num2 = "";
op = "";
}
void printNum1(String NUM) {
int numLength = NUM.length();
lcd.setCursor(15 - numLength, 0);
lcd.print(NUM);
}
void printNum2(String NUM) {
int numLength = NUM.length();
lcd.setCursor(15 - numLength, 1);
lcd.print(NUM);
}
void loop() {
char key = keypad.getKey();
//input for the first number of the calculation and sets it into the right top corner - 1 of the lcd
if (key != NO_KEY && (key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' || key == '7' || key == '8' || key == '9' || key == '0')) {
if (gotOp == false && keepCalculating == false) {
num1 = num1 + key;
printNum1(num1);
gotNum1 = true;
}
}
//input for the operator of the calculation and sets it into the very right top corner of the lcd
if (key != NO_KEY && (key == '+' || key == '-' || key == '*' || key == '/')) {
if (gotNum1 == true) { //if number 1 has a value
op = "";
op = key;
lcd.setCursor(15, 0);
lcd.print(op);
gotOp = true;
}
}
//input for the second number of the calculation and sets it into the right bottom corner - 1 of the lcd
if (key != NO_KEY && (key == '1' || key == '2' || key == '3' || key == '4' || key == '5' || key == '6' || key == '7' || key == '8' || key == '9' || key == '0')) {
if (gotOp == true || keepCalculating == true) { //if an operator has been assigned //or answer has already been printed
num2 = num2 + key;
printNum2(num2);
showResult = true;
}
if (gotNum1 == true && keepCalculating == true && gotOp == false) { //if number 1 has a value //if answer has already been printed
op = '+';
lcd.setCursor(15, 0);
lcd.print(op);
gotOp = true;
}
}
//calculates and shows the result depending on the chosen operator
if (key != NO_KEY && key == '#' && showResult == true) {
if (keepCalculating == false) {
switch (op) {
case '+':
answer = num1.toInt() + num2.toInt();
printAnswer(answer);
break;
case '-':
answer = num1.toInt() - num2.toInt();
printAnswer(answer);
break;
case '*':
answer = num1.toInt() * num2.toInt();
printAnswer(answer);
break;
case '/':
answer = num1.toFloat() / num2.toFloat();
printAnswer(answer);
break;
}
}
//after the first calculation you can keep on calculating with the "answer"
if (keepCalculating == true) { //if an answer has been printed
switch (op) {
case '+':
answer = answer + num2.toInt();
printAnswer(answer);
break;
case '-':
answer = answer - num2.toInt();
printAnswer(answer);
break;
case '*':
answer = answer * num2.toInt();
printAnswer(answer);
break;
case '/':
answer = answer / num2.toInt();
printAnswer(answer);
break;
}
}
}
//deletes all the data and resets the lcd monitor
if (key != NO_KEY && key == 'C') {
num1 = "";
num2 = "";
op = "";
gotNum1 = false;
gotOp = false;
showResult = false;
keepCalculating = false;
lcd.clear();
}
}