// Ấn * để xác nhận
// Ấn D để reset
// Ấn # để hiện x và y
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 16, 2); // Địa chỉ I2C và kích thước LCD
enum CalculatorState {
MENU,
INPUT_X,
INPUT_Y,
DISPLAY_XY,
RESULT_A,
RESULT_B,
RESULT_C
};
CalculatorState currentState = MENU;
int x = 0;
int y = 0;
int result = 0;
void setup() {
lcd.begin(16, 2);
lcd.print("welcome");
delay(500);
lcd.print(".");
delay(200);
lcd.print(".");
delay(200);
lcd.print(".");
delay(200);
}
void loop() {
char key = keypad.getKey();
switch (currentState) {
case MENU:
lcd.setCursor(0, 0);
lcd.print("1: Input X");
lcd.setCursor(0, 1);
lcd.print("2: Input Y");
if (key == '1') {
currentState = INPUT_X;
lcd.clear();
lcd.print("Enter X: ");
} else if (key == '2') {
currentState = INPUT_Y;
lcd.clear();
lcd.print("Enter Y: ");
} else if (key == '#') {
currentState = DISPLAY_XY;
} else if (key == 'A') {
currentState = RESULT_A;
} else if (key == 'B') {
currentState = RESULT_B;
} else if (key == 'C') {
currentState = RESULT_C;
} else if (key == 'D') {
x = 0;
y = 0;
lcd.clear();
lcd.print("reset");
delay(500);
lcd.print(".");
delay(200);
lcd.print(".");
delay(200);
lcd.print(".");
delay(200);
//currentState = MENU;
}
break;
case INPUT_X:
if (key >= '0' && key <= '9') {
x = x * 10 + (key - '0');
lcd.print(key);
} else if (key == '*') {
lcd.clear();
currentState = MENU;
}
break;
case INPUT_Y:
if (key >= '0' && key <= '9') {
y = y * 10 + (key - '0');
lcd.print(key);
} else if (key == '*') {
lcd.clear();
currentState = MENU;
}
break;
case DISPLAY_XY:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("X: ");
lcd.print(x);
lcd.setCursor(0, 1);
lcd.print("Y: ");
lcd.print(x);
delay(3000);
lcd.clear();
currentState = MENU;
break;
case RESULT_A:
result = x + y;
lcd.clear();
lcd.print("Result A: ");
lcd.print(result);
delay(3000);
lcd.clear();
currentState = MENU;
break;
case RESULT_B:
result = x - y;
lcd.clear();
lcd.print("Result B: ");
lcd.print(result);
delay(3000);
lcd.clear();
currentState = MENU;
break;
case RESULT_C:
result = x + x + y + y;
lcd.clear();
lcd.print("Result C: ");
lcd.print(result);
delay(3000);
lcd.clear();
currentState = MENU;
break;
}
}