Skip to content

Commit b978502

Browse files
authored
Create calc
0 parents  commit b978502

File tree

1 file changed

+212
-0
lines changed

1 file changed

+212
-0
lines changed

calc

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
2+
// calculator
3+
4+
#include <LiquidCrystal.h>
5+
6+
#include <Keypad.h>
7+
8+
9+
10+
LiquidCrystal lcd(0, 1, 2, 3, 4, 5);
11+
12+
const byte ROWS = 4;
13+
14+
const byte COLS = 4;
15+
16+
17+
18+
char keys [ROWS] [COLS] = {
19+
20+
{'1', '2', '3', '-'},
21+
22+
{'4', '5', '6', '*'},
23+
24+
{'7', '8', '9', '/'},
25+
26+
{'C', '0', '=', '+'}
27+
28+
};
29+
30+
byte rowPins[ROWS] = {13,12,11,10};
31+
32+
byte colPins[COLS] = {9,8,7,6};
33+
34+
35+
36+
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
37+
38+
39+
40+
41+
42+
boolean presentValue = false;
43+
44+
boolean next = false;
45+
46+
boolean final = false;
47+
48+
String num1, num2;
49+
50+
int answer;
51+
52+
char op;
53+
54+
55+
56+
void setup()
57+
58+
{
59+
60+
lcd.begin(16,2);
61+
62+
lcd.setCursor(0,0);
63+
64+
lcd.print(" AKUL VERMA JI");
65+
66+
lcd.setCursor(0,1);
67+
68+
lcd.print(" Calculator");
69+
70+
delay(3000);
71+
72+
lcd.clear();
73+
74+
lcd.setCursor(0,0);
75+
76+
lcd.print(" by ");
77+
78+
lcd.setCursor(0,1);
79+
80+
lcd.print(" AKUL VERMA JI" );
81+
82+
delay(3000);
83+
84+
lcd.clear();
85+
86+
}
87+
88+
89+
90+
void loop(){
91+
92+
char key = myKeypad.getKey();
93+
94+
95+
96+
if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0'))
97+
98+
{
99+
100+
if (presentValue != true)
101+
102+
{
103+
104+
num1 = num1 + key;
105+
106+
int numLength = num1.length();
107+
108+
lcd.setCursor(15 - numLength, 0); //to adjust one whitespace for operator
109+
110+
lcd.print(num1);
111+
112+
}
113+
114+
else
115+
116+
{
117+
118+
num2 = num2 + key;
119+
120+
int numLength = num2.length();
121+
122+
lcd.setCursor(15 - numLength, 1);
123+
124+
lcd.print(num2);
125+
126+
final = true;
127+
128+
}
129+
130+
}
131+
132+
133+
134+
else if (presentValue == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+'))
135+
136+
{
137+
138+
if (presentValue == false)
139+
140+
{
141+
142+
presentValue = true;
143+
144+
op = key;
145+
146+
lcd.setCursor(15,0);
147+
148+
lcd.print(op);
149+
150+
}
151+
152+
}
153+
154+
155+
156+
else if (final == true && key != NO_KEY && key == '='){
157+
158+
if (op == '+'){
159+
160+
answer = num1.toInt() + num2.toInt();
161+
162+
}
163+
164+
else if (op == '-'){
165+
166+
answer = num1.toInt() - num2.toInt();
167+
168+
}
169+
170+
else if (op == '*'){
171+
172+
answer = num1.toInt() * num2.toInt();
173+
174+
}
175+
176+
else if (op == '/'){
177+
178+
answer = num1.toInt() / num2.toInt();
179+
180+
}
181+
182+
lcd.clear();
183+
184+
lcd.setCursor(15,0);
185+
186+
lcd.autoscroll();
187+
188+
lcd.print(answer);
189+
190+
lcd.noAutoscroll();
191+
192+
}
193+
194+
else if (key != NO_KEY && key == 'C'){
195+
196+
lcd.clear();
197+
198+
presentValue = false;
199+
200+
final = false;
201+
202+
num1 = "";
203+
204+
num2 = "";
205+
206+
answer = 0;
207+
208+
op = ' ';
209+
210+
}
211+
212+
}

0 commit comments

Comments
 (0)