#include <Keypad.h>
#include <LiquidCrystal.h>
const uint8_t ROWS =4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'.', '0', '=', '/'}
};
uint8_t colPins[COLS] = {5,4,3,2};
uint8_t rowPins[ROWS] = {9,8,7,6};
Keypad KLAWA = Keypad(makeKeymap(keys), rowPins, colPins,ROWS,COLS);
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
void showStartScreen(){
lcd.setCursor(4,0);
lcd.print("VA_LUHA");
lcd.setCursor(3,1);
String message = "Calculator";
for (byte i=0; i<message.length();i++) {
lcd.print(message[i]);
delay(50);
}
delay(500);
}
void updateCursor() {
if (millis()/250%2==0){
lcd.cursor();
} else {
lcd.noCursor();
}
}
void setup() {
// put your setup code here, to run once:
//Serial.begin(9600);
lcd.begin(16,2);
showStartScreen();
lcd.clear();
lcd.cursor();
lcd.setCursor(1,0);
//lcd.print("Hello SerafimGamer");
}
char operation = 0;
String memory = "";
String current ="";
uint64_t currentDecimal;
bool decimalPoint = false;
double calculate(char operation, double left, double right) {
switch (operation) {
case '+': return left + right;
case '-': return left - right;
case '*': return left * right;
case '/': return left / right;
}
}
void processInput(char key) {
if ('-'==key && current == "") {
current ="-";
lcd.print("-");
return;
}
switch (key) {
case '+':
case '-':
case '*':
case '/':
if (!operation) {
memory = current;
current = "";
}
operation = key;
lcd.setCursor(0,1);
lcd.print(key);
lcd.setCursor(current.length()+1,1);
return;
case '=':
float leftNum = memory.toDouble();
float rightNum = current.toDouble();
memory = String(calculate(operation, leftNum, rightNum));
current = "";
lcd.clear();
lcd.setCursor(1,0);
lcd.print(memory);
lcd.setCursor(0,1);
lcd.print(operation);
return;
}
if ('.' == key && current.indexOf('.') >= 0) {
return;
}
if ('.' != key && current == "0") {
current = String(key);
} else if (key) {
current+= String(key);
}
lcd.print(key);
}
void loop() {
// put your main code here, to run repeatedly:
updateCursor();
char key = KLAWA.getKey();
if (key) {
processInput(key);
}
}