-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
123 lines (103 loc) · 2.43 KB
/
main.ino
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
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'.', '0', '=', '/'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
uint64_t value = 0;
void showSpalshScreen() {
lcd.print("Abhineet Raj");
lcd.setCursor(0, 1);
String message = "Arduino project";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(50);
}
delay(500);
}
void updateCursor() {
if (millis() / 250 % 2 == 0 ) {
lcd.cursor();
} else {
lcd.noCursor();
}
}
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
showSpalshScreen();
lcd.clear();
lcd.cursor();
lcd.setCursor(1, 0);
}
char operation = 0;
String memory = "";
String current = "";
uint64_t currentDecimal;
bool decimalPoint = false;
double calculate(char operation, double left, double right) {
switch (operation) {
case '+': return left + right;
case '-': return left - right;
case '*': return left * right;
case '/': return left / right;
}
}
void processInput(char key) {
if ('-' == key && current == "") {
current = "-";
lcd.print("-");
return;
}
switch (key) {
case '+':
case '-':
case '*':
case '/':
if (!operation) {
memory = current;
current = "";
}
operation = key;
lcd.setCursor(0, 1);
lcd.print(key);
lcd.setCursor(current.length() + 1, 1);
return;
case '=':
float leftNum = memory.toDouble();
float rightNum = current.toDouble();
memory = String(calculate(operation, leftNum, rightNum));
current = "";
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(memory);
lcd.setCursor(0, 1);
lcd.print(operation);
return;
}
if ('.' == key && current.indexOf('.') >= 0) {
return;
}
if ('.' != key && current == "0") {
current = String(key);
} else if (key) {
current += String(key);
}
lcd.print(key);
}
void loop() {
updateCursor();
char key = keypad.getKey();
if (key) {
processInput(key);
}
}