#include <LiquidCrystal.h>
#include <Keypad.h>
#define ROWS 4
#define COLS 4
#define BT_DEL 6
char operation;
String n1;
String n2;
uint8_t rowPins[4] = {2, 3, 4, 5};
uint8_t colPins[4] = {A5, A4, A3, A2};
char keys[ROWS][COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'.', '0', '/', '='}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins,
ROWS, COLS);
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
void intro(){
lcd.setCursor(3, 0);
lcd.print("Arduino Cal");
delay(200);
lcd.setCursor(5, 1);
String men = "Thauas";
for (byte i = 0; i < men.length(); i++) {
lcd.print(men[i]);
delay(50);
}
delay(2100);
lcd.clear();
}
void setup() {
pinMode(BT_DEL, INPUT_PULLUP);
lcd.begin(16, 2);
intro();
}
float calculate(float n1, float n2, char s){
switch(s){
case '+':
return n1+n2;
case '-':
return n1-n2;
case '*':
return n1*n2;
case '/':
return n1/n2;
}
}
void loop() {
char key = keypad.getKey();
if(digitalRead(BT_DEL) == LOW){
n1 = "";
n2 = "";
operation = '\0';
lcd.clear();
}
if(key != NO_KEY){
if(key == '+' || key == '-' || key == '*' || key == '/'){
if(n1.length() > 0){
lcd.setCursor(0, 1);
operation = key;
lcd.print(operation);
}
}
else if(key != '='){
if(operation == 0){
if(n1.length() == 0 && key == '.'){}
else{
n1 += key;
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(n1);
}
}
else{
if(n2.length() == 0 && key == '.');
else{
n2 += key;
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(n1);
lcd.setCursor(0, 1);
lcd.print(operation);
lcd.print(n2);
}
}
}
else{
if(operation != 0 && n1.length() > 0 && n2.length() > 0){
lcd.clear();
float result = calculate(n1.toFloat(), n2.toFloat(), operation);
n1 = String(result);
n2 = "";
operation = '\0';
lcd.print(result);
}
}
}
}