#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {1, 2, 3, 4};
byte colPins[COLS] = {5, 6, 7, 8};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
char step;
char simbol;
enum {x, y, operasi, hitung};
String strx = "", stry="";
int intx, inty;
int hasil;
void setup() {
Serial.begin(9600);
//lcd.begin();
lcd.init();
lcd.backlight();
// lcd.setCursor(0,0);
// lcd.print("Aritmatika");
}
void loop() {
char customKey = customKeypad.getKey();
switch(step){
case x:
if(customKey){
if(customKey == 'A' || customKey == 'B' || customKey == 'C' ||customKey == 'D'){
step = operasi;
}
else{
strx+=customKey;
lcd.setCursor(0,0);
lcd.print(strx);
}
}break;
case operasi:
if(customKey){
if(customKey == 'A'){
lcd.print('+');
simbol = '+';
step = y;
}
else if(customKey == 'B'){
lcd.print('-');
simbol = '-';
step = y;
}
else if(customKey == 'C'){
lcd.print('x');
simbol = 'x';
step = y;
}
else if(customKey == 'D'){
lcd.print(':');
simbol = ':';
step = y;
}
else{
strx+= customKey;
lcd.setCursor(0,0);
lcd.print(x);
}
}break;
case y:
if(customKey){
if(customKey == '#'){
step = hitung;
}
else{
stry+=customKey;
lcd.print(stry);
}
}break;
case hitung:
intx = strx.toInt(); //ubah ke integer dari float
inty = stry.toInt();
if (simbol == '+'){
hasil = intx + inty;
}
else if (simbol == '-'){
hasil = intx - inty;
}
else if(simbol == 'x'){
hasil = intx * inty;
}
else if (simbol == ':'){
hasil = intx / inty;
}
lcd.setCursor(0, 1);
lcd.println("=");
lcd.print(hasil);
if(customKey){
if(customKey == '*'){
strx = "";
stry = "";
lcd.clear();
step = x;
}
}
break;
}
}