#include <LiquidCrystal_I2C.h>
#define BTN_A 5
#define BTN_B 3
#define BTN_ACTION 4
#define BTN_CALC 2
LiquidCrystal_I2C lcd(0x27,16,2);
int a = 0;
int b = 0;
int action = 0;
int result = 0;
void setup() {
lcd.init();
lcd.clear();
lcd.backlight();
pinMode(BTN_A, INPUT_PULLUP);
pinMode(BTN_B, INPUT_PULLUP);
pinMode(BTN_ACTION, INPUT_PULLUP);
pinMode(BTN_CALC, INPUT_PULLUP);
}
void loop() {
if(digitalRead(BTN_A)==LOW){
delay(200);
a = a + 1;
}
if(digitalRead(BTN_B)==LOW){
delay(200);
b = b + 1;
}
if(digitalRead(BTN_ACTION)==LOW){
delay(500);
action = action + 1;
if (action > 3){
action = 0;
}
}
if(digitalRead(BTN_CALC)==LOW){
delay(500);
lcd.setCursor(0,0);
if (action == 0){
result = a + b;
lcd.print(String(a) + " + " + String(b) + " = " + String(result));
}
if (action == 1){
result = a - b;
lcd.print(String(a) + " - " + String(b) + " = " + String(result));
}
if (action == 2){
result = a * b;
lcd.print(String(a) + " * " + String(b) + " = " + String(result));
}
if (action == 3){
if(b > 0){
result = a / b;
lcd.print(String(a) + " / " + String(b) + " = " + String(result));
} else {
lcd.print("Error divide 0");
}
}
}
lcd.setCursor(0,0);
if (action == 0){
lcd.print(String(a) + " + " + String(b));
}
if (action == 1){
lcd.print(String(a) + " - " + String(b));
}
if (action == 2){
lcd.print(String(a) + " * " + String(b));
}
if (action == 3){
lcd.print(String(a) + " / " + String(b));
}
}