#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
int num_colunas = 16;
int num_linhas = 2;
LiquidCrystal_I2C lcd(0x27, num_colunas, num_linhas);
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {18, 17, 16, 15};
byte colPins[KEYPAD_COLS] = {7, 6, 5, 4};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'.', '0', '=', '/'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
uint64_t value = 0;
void setup(){
Serial.begin(115200);
print_inicial();
}
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 '=':
if(current == "2456"){
print_inicial();
}
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(){
//print_inicial();
updateCursor();
char key = keypad.getKey();
if (key) {
processInput(key);
}
}
void print_inicial(){
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Sist Embarcados");
lcd.setCursor(2,1);
lcd.print("Calculadora");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Alunos:");
lcd.setCursor(2,1);
lcd.print("Andrew");
delay(2000);
lcd.clear();
}
void showSpalshScreen() {
lcd.print("SistemasEmbarcados");
lcd.setCursor(3, 1);
String message = "Calculadora";
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();
}
}