1#include <Keypad.h>
2#include <LiquidCrystal.h>
3
4LiquidCrystal
5 lcd(0, 1, 2, 3, 4, 5);
6const byte ROWS = 4;
7const byte COLS = 4;
8
9char
10 keys [ROWS] [COLS] = {
11 {'1', '2', '3', '+'},
12 {'4', '5', '6', '-'},
13
14 {'7', '8', '9', '*'},
15 {'C', '0', '=', '/'}
16};
17byte rowPins[ROWS] =
18 {13,12,11,10};
19byte colPins[COLS] = {9,8,7,6};
20
21Keypad myKeypad = Keypad(
22 makeKeymap(keys), rowPins, colPins, ROWS, COLS );
23
24boolean presentValue =
25 false;
26boolean next = false;
27boolean final = false;
28String num1, num2;
29int
30 answer;
31char op;
32
33void setup()
34{
35 lcd.begin(16,2);
36
37 lcd.setCursor(0,0);
38 lcd.print("An Arduino");
39 edit it
40 lcd.setCursor(0,1);
41 lcd.print("Calculator");
42 delay(3000);
43
44 lcd.clear();
45 lcd.setCursor(0,0);
46 lcd.print("The");
47 lcd.setCursor(0,1);
48
49 lcd.print("Arduinolator!");
50 delay(3000);
51 lcd.clear();
52 and you can begin using it.
53}
54
55void loop(){
56
57 char key = myKeypad.getKey();
58
59
60 if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0'))
61
62 {
63 if (presentValue != true)
64 {
65 num1 = num1 + key;
66 int
67 numLength = num1.length();
68 lcd.setCursor(15 - numLength, 0);
69 one whitespace for operator
70 lcd.print(num1);
71 }
72 else
73
74 {
75 num2 = num2 + key;
76 int numLength = num2.length();
77 lcd.setCursor(15
78 - numLength, 1);
79 lcd.print(num2);
80 final = true;
81 }
82 }
83
84
85 else if (presentValue == false && key != NO_KEY && (key == '/' || key == '*' ||
86 key == '-' || key == '+'))
87 {
88 if (presentValue == false)
89 {
90
91 presentValue = true;
92 op = key;
93 lcd.setCursor(15,0);
94
95 lcd.print(op);
96 }
97 }
98
99 else if (final == true && key != NO_KEY
100 && key == '='){
101 if (op == '+'){
102 answer = num1.toInt() + num2.toInt();
103
104 }
105 else if (op == '-'){
106 answer = num1.toInt() - num2.toInt();
107
108 }
109 else if (op == '*'){
110 answer = num1.toInt() * num2.toInt();
111
112 }
113 else if (op == '/'){
114 answer = num1.toInt() / num2.toInt();
115
116 }
117 lcd.clear();
118 lcd.setCursor(15,0);
119 lcd.autoscroll();
120
121 lcd.print(answer);
122 lcd.noAutoscroll();
123 }
124 else if (key !=
125 NO_KEY && key == 'C'){
126 lcd.clear();
127 presentValue = false;
128 final
129 = false;
130 num1 = "";
131 num2 = "";
132 answer = 0;
133 op =
134 ' ';
135 }
136}
137