-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino-calculator.cpp
158 lines (147 loc) · 4.19 KB
/
arduino-calculator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <Adafruit_LiquidCrystal.h>
#include <Keypad.h>
Adafruit_LiquidCrystal lcd(0);
String input = "";
int pos = 0;
const int switchPin = 12, switchClearDataPin = 13;
int oldState = 0;
float firstNum, secondNum, answer;
const byte rows = 4; // Số hàng của ma trận keypad
const byte cols = 4; // Số cột của ma trận keypad
char keymap[rows][cols] = {
{'1','2','3','+'},
{'4','5','6','-'},
{'7','8','9','.'},
{'*','0','#','/'}
};
byte rowPins[rows] = {9, 8, 7, 6};
byte colPins[cols] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keymap), rowPins, colPins, rows, cols);
void setup()
{
pinMode(switchPin, INPUT_PULLUP);
pinMode(switchClearDataPin, INPUT_PULLUP);
lcd.begin(16, 2);
lcd.print("Calculator");
lcd.setCursor(0, 1);
lcd.print("Arduino");
delay(1000);
lcd.clear();
}
void loop() {
if (digitalRead(switchClearDataPin) == HIGH) {
clearData();
}
int switchState = digitalRead(switchPin); // liên tục kiểm tra trạng thái slideswitch xóa 1 phần tử trong loop
if (switchState != oldState) {
input = input.substring(0, input.length() - 1);
oldState = switchState;
pos = pos - 1;
lcd.setCursor(pos, 0);
lcd.print(" ");
lcd.setCursor(pos, 0);
cal();
} else {
cal();
}
}
void cal() {
char key = keypad.getKey();
if (key != NO_KEY && key != '#') { // không lưu dấu # vào input
input = input + String(key);
pos++;
lcd.print(key);
lcd.setCursor(pos, 0);
}
if (key == '#') {
byte length = String(input).length(); // độ dài input
String num1 = "", num2 = "";
byte firstNumState = 0, secondNumState = 0, symbolState = 0;
char symbol;
for (int i = 0; i < (length); i = i + 1) {
char chr = String(input).charAt(i);
if (firstNumState == 1 && symbolState == 1) {
num2 = num2 + chr; // lấy số hạng 2
}
if (chr == '+' || chr == '-' || chr == '*' || chr == '/') {
symbol = chr; // lưu dấu phép tính vào symbol
firstNumState = 1; // báo hiệu đã xong num1
symbolState = 1; // báo hiệu đã có symbol
}
if (firstNumState == 0) {
num1 = num1 + chr; // lấy số hạng 1
}
}
// xử lý lấy số hạng
firstNum = getNum(num1);
secondNum = getNum(num2);
// thực hiện phép tính
switch (symbol) {
case '+':
answer = firstNum + secondNum;
lcd.setCursor(0, 1);
// lcd.print("Answer:");
lcd.setCursor(0, 1);
lcd.print(answer);
break;
case '-':
answer = firstNum - secondNum;
lcd.setCursor(0, 1);
// lcd.print("Answer:");
lcd.setCursor(0, 1);
lcd.print(answer);
break;
case '*':
answer = firstNum * secondNum;
lcd.setCursor(0, 1);
// lcd.print("Answer:");
lcd.setCursor(0, 1);
lcd.print(answer);
break;
case '/':
if (secondNum != 0) {
answer = firstNum / secondNum;
lcd.setCursor(0, 1);
// lcd.print("Answer:");
lcd.print(answer);
break;
} else {
lcd.setCursor(0, 1);
lcd.print("Math ERROR!");
break;
}
}
}
}
// xóa dữ liệu
void clearData() {
lcd.clear();
input = "";
pos = 0;
}
// xử lý lấy số hạng thập phân
float getNum(String str) {
String naturalPart = "", decimalPart = "";
byte length = String(str).length();
byte naturalState = 0;
for (int i = 0; i < (length); i = i + 1) {
char chr = String(str).charAt(i);
if (naturalState == 1) {
decimalPart = decimalPart + chr; // lấy phần thập phân
}
if (chr == '.') {
naturalState = 1; // báo hiệu đã xong phần nguyên
}
if (naturalState == 0) {
naturalPart = naturalPart + chr; // lấy phần nguyên
}
}
byte count10 = String(decimalPart).length();
float mul = 1;
for (int i = 0; i < count10; i++) {
mul = mul / 10.0;
}
float ans;
ans = (String(naturalPart).toInt() + String(decimalPart).toInt() * mul);
return ans;
}