#include <LiquidCrystal.h>
#include <Keypad.h>
const int rs = 12, en = 11, d4 = 10, d5 = 9, d6 = 8, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char key;
char status = 0;
char keys [ROWS] [COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'},
};
uint8_t colPins [COLS] = {5,4,3,2};
uint8_t rowPins [ROWS] = {A0, A1, A2, A3};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
lcd.begin(16, 2);
lcd.print("Kalkulator Siap!");
delay(1000);
lcd.clear();
}
int x = 0, y = 0;
float b = 0, c = 0, d = 0;
char e, f;
void loop() {
char key = keypad.getKey();
if (key != NO_KEY){
if (key >= 48 && key < 58){
if (y == 0){
b = b*10 + key-48;
lcd.setCursor(0,y);
lcd.print(b);
} else {
c = c*10 + key - 48;
lcd.setCursor(2,y);
lcd.print(c);
}
} else if (key == 'A' || key == 'B' || key == 'C' || key == 'D') {
lcd.setCursor(0,1);
e = key;
switch(e){
case 'A' : f = '+'; break;
case 'B' : f = '-'; break;
case 'C' : f = '*'; break;
case 'D' : f = '/'; break;
default : x = 0; y = 0; b = 0; c = 0; d = 0; e = 0; break;
}
lcd.print(f);
y = 1;
} else if (key == '#'){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("=");
switch(e){
case 'A' : d = b+c; break;
case 'B' : d = b-c; break;
case 'C' : d = b*c; break;
case 'D' : d = b/c; break;
default : x = 0; y = 0; b = 0; c = 0; d = 0; e = 0; break;
}
lcd.setCursor(2,0);
lcd.print(d);
c = 0;
b = d;
}
}
//Reset
if (key == '*'){
lcd.clear();
x = 0;
y = 0;
b = 0;
c = 0;
d = 0;
lcd.print("Reset");
for (int i = 0; i < 5; i++){
delay(100);
lcd.setCursor(i,1);
lcd.print(".");
}
lcd.clear();
}
}