Components and supplies
Jumper wires (generic)
9V battery (generic)
Breadboard (generic)
RGB LCD Shield Kit, 16x2 Character Display
Arduino UNO
Project description
Code
untitled
arduino
1* 2 * Arduino Keypad calculator Program 3 */ 4 5#include <LiquidCrystal.h> //Header file for LCD from https://www.arduino.cc/en/Reference/LiquidCrystal 6#include <Keypad.h> //Header file for Keypad from https://github.com/Chris--A/Keypad 7 8const byte ROWS = 4; // Four rows 9const byte COLS = 4; // Three columns 10 11// Define the Keymap 12char keys[ROWS][COLS] = { 13 14 {'7','8','9','D'}, 15 16 {'4','5','6','C'}, 17 18 {'1','2','3','B'}, 19 20 {'*','0','#','A'} 21 22}; 23 24byte rowPins[ROWS] = { 0, 1, 2, 3 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins. 25byte colPins[COLS] = { 4, 5, 6, 7 }; // Connect keypad COL0, COL1 and COL2 to these Arduino pins. 26 27Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Create the Keypad 28 29const int rs = 8, en = 9, d4 = 10, d5 = 11, d6 = 12, d7 = 13; //Pins to which LCD is connected 30LiquidCrystal lcd(rs, en, d4, d5, d6, d7); 31 32 long Num1,Num2,Number; 33 char key,action; 34 boolean result = false; 35 36void setup() { 37 lcd.begin(16, 2); //We are using a 16*2 LCD display 38 lcd.print("DIY Calculator"); //Display a intro message 39 lcd.setCursor(0, 1); // set the cursor to column 0, line 1 40 lcd.print("-CircuitDigest"); //Display a intro message 41 42 delay(2000); //Wait for display to show info 43 lcd.clear(); //Then clean it 44} 45 46void loop() { 47 48key = kpd.getKey(); //storing pressed key value in a char 49 50if (key!=NO_KEY) 51DetectButtons(); 52 53if (result==true) 54CalculateResult(); 55 56DisplayResult(); 57} 58 59void DetectButtons() 60{ 61 lcd.clear(); //Then clean it 62 if (key=='*') //If cancel Button is pressed 63 {Serial.println ("Button Cancel"); Number=Num1=Num2=0; result=false;} 64 65 if (key == '1') //If Button 1 is pressed 66 {Serial.println ("Button 1"); 67 if (Number==0) 68 Number=1; 69 else 70 Number = (Number*10) + 1; //Pressed twice 71 } 72 73 if (key == '4') //If Button 4 is pressed 74 {Serial.println ("Button 4"); 75 if (Number==0) 76 Number=4; 77 else 78 Number = (Number*10) + 4; //Pressed twice 79 } 80 81 if (key == '7') //If Button 7 is pressed 82 {Serial.println ("Button 7"); 83 if (Number==0) 84 Number=7; 85 else 86 Number = (Number*10) + 7; //Pressed twice 87 } 88 89 90 if (key == '0') 91 {Serial.println ("Button 0"); //Button 0 is Pressed 92 if (Number==0) 93 Number=0; 94 else 95 Number = (Number*10) + 0; //Pressed twice 96 } 97 98 if (key == '2') //Button 2 is Pressed 99 {Serial.println ("Button 2"); 100 if (Number==0) 101 Number=2; 102 else 103 Number = (Number*10) + 2; //Pressed twice 104 } 105 106 if (key == '5') 107 {Serial.println ("Button 5"); 108 if (Number==0) 109 Number=5; 110 else 111 Number = (Number*10) + 5; //Pressed twice 112 } 113 114 if (key == '8') 115 {Serial.println ("Button 8"); 116 if (Number==0) 117 Number=8; 118 else 119 Number = (Number*10) + 8; //Pressed twice 120 } 121 122 123 if (key == '#') 124 {Serial.println ("Button Equal"); 125 Num2=Number; 126 result = true; 127 } 128 129 if (key == '3') 130 {Serial.println ("Button 3"); 131 if (Number==0) 132 Number=3; 133 else 134 Number = (Number*10) + 3; //Pressed twice 135 } 136 137 if (key == '6') 138 {Serial.println ("Button 6"); 139 if (Number==0) 140 Number=6; 141 else 142 Number = (Number*10) + 6; //Pressed twice 143 } 144 145 if (key == '9') 146 {Serial.println ("Button 9"); 147 if (Number==0) 148 Number=9; 149 else 150 Number = (Number*10) + 9; //Pressed twice 151 } 152 153 if (key == 'A' || key == 'B' || key == 'C' || key == 'D') //Detecting Buttons on Column 4 154 { 155 Num1 = Number; 156 Number =0; 157 if (key == 'A') 158 {Serial.println ("Addition"); action = '+';} 159 if (key == 'B') 160 {Serial.println ("Subtraction"); action = '-'; } 161 if (key == 'C') 162 {Serial.println ("Multiplication"); action = '*';} 163 if (key == 'D') 164 {Serial.println ("Devesion"); action = '/';} 165 166 delay(100); 167 } 168 169} 170 171void CalculateResult() 172{ 173 if (action=='+') 174 Number = Num1+Num2; 175 176 if (action=='-') 177 Number = Num1-Num2; 178 179 if (action=='*') 180 Number = Num1*Num2; 181 182 if (action=='/') 183 Number = Num1/Num2; 184} 185 186void DisplayResult() 187{ 188 lcd.setCursor(0, 0); // set the cursor to column 0, line 1 189 lcd.print(Num1); lcd.print(action); lcd.print(Num2); 190 191 if (result==true) 192 {lcd.print(" ="); lcd.print(Number);} //Display the result 193 194 lcd.setCursor(0, 1); // set the cursor to column 0, line 1 195 lcd.print(Number); //Display the result 196} 197
untitled
arduino
1* 2 * Arduino Keypad calculator Program 3 */ 4 5#include <LiquidCrystal.h> 6 //Header file for LCD from https://www.arduino.cc/en/Reference/LiquidCrystal 7#include 8 <Keypad.h> //Header file for Keypad from https://github.com/Chris--A/Keypad 9 10const 11 byte ROWS = 4; // Four rows 12const byte COLS = 4; // Three columns 13 14// Define 15 the Keymap 16char keys[ROWS][COLS] = { 17 18 {'7','8','9','D'}, 19 20 {'4','5','6','C'}, 21 22 23 {'1','2','3','B'}, 24 25 {'*','0','#','A'} 26 27}; 28 29byte rowPins[ROWS] 30 = { 0, 1, 2, 3 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins. 31byte 32 colPins[COLS] = { 4, 5, 6, 7 }; // Connect keypad COL0, COL1 and COL2 to these Arduino 33 pins. 34 35Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS 36 ); // Create the Keypad 37 38const int rs = 8, en = 9, d4 = 10, d5 = 11, d6 = 39 12, d7 = 13; //Pins to which LCD is connected 40LiquidCrystal lcd(rs, en, d4, d5, 41 d6, d7); 42 43 long Num1,Num2,Number; 44 char key,action; 45 boolean result 46 = false; 47 48void setup() { 49 lcd.begin(16, 2); //We are using a 16*2 LCD 50 display 51 lcd.print("DIY Calculator"); //Display a intro message 52 lcd.setCursor(0, 53 1); // set the cursor to column 0, line 1 54 lcd.print("-CircuitDigest"); 55 //Display a intro message 56 57 delay(2000); //Wait for display to show info 58 59 lcd.clear(); //Then clean it 60} 61 62void loop() { 63 64key = kpd.getKey(); 65 //storing pressed key value in a char 66 67if (key!=NO_KEY) 68DetectButtons(); 69 70if 71 (result==true) 72CalculateResult(); 73 74DisplayResult(); 75} 76 77void 78 DetectButtons() 79{ 80 lcd.clear(); //Then clean it 81 if (key=='*') 82 //If cancel Button is pressed 83 {Serial.println ("Button Cancel"); Number=Num1=Num2=0; 84 result=false;} 85 86 if (key == '1') //If Button 1 is pressed 87 {Serial.println 88 ("Button 1"); 89 if (Number==0) 90 Number=1; 91 else 92 Number 93 = (Number*10) + 1; //Pressed twice 94 } 95 96 if (key == '4') //If 97 Button 4 is pressed 98 {Serial.println ("Button 4"); 99 if (Number==0) 100 101 Number=4; 102 else 103 Number = (Number*10) + 4; //Pressed twice 104 } 105 106 107 if (key == '7') //If Button 7 is pressed 108 {Serial.println ("Button 109 7"); 110 if (Number==0) 111 Number=7; 112 else 113 Number = (Number*10) 114 + 7; //Pressed twice 115 } 116 117 118 if (key == '0') 119 {Serial.println 120 ("Button 0"); //Button 0 is Pressed 121 if (Number==0) 122 Number=0; 123 124 else 125 Number = (Number*10) + 0; //Pressed twice 126 } 127 128 if 129 (key == '2') //Button 2 is Pressed 130 {Serial.println ("Button 2"); 131 if 132 (Number==0) 133 Number=2; 134 else 135 Number = (Number*10) + 2; //Pressed 136 twice 137 } 138 139 if (key == '5') 140 {Serial.println ("Button 141 5"); 142 if (Number==0) 143 Number=5; 144 else 145 Number = (Number*10) 146 + 5; //Pressed twice 147 } 148 149 if (key == '8') 150 {Serial.println 151 ("Button 8"); 152 if (Number==0) 153 Number=8; 154 else 155 Number 156 = (Number*10) + 8; //Pressed twice 157 } 158 159 160 if (key == '#') 161 162 {Serial.println ("Button Equal"); 163 Num2=Number; 164 result = true; 165 166 } 167 168 if (key == '3') 169 {Serial.println ("Button 3"); 170 171 if (Number==0) 172 Number=3; 173 else 174 Number = (Number*10) + 3; 175 //Pressed twice 176 } 177 178 if (key == '6') 179 {Serial.println 180 ("Button 6"); 181 if (Number==0) 182 Number=6; 183 else 184 Number 185 = (Number*10) + 6; //Pressed twice 186 } 187 188 if (key == '9') 189 190 {Serial.println ("Button 9"); 191 if (Number==0) 192 Number=9; 193 else 194 195 Number = (Number*10) + 9; //Pressed twice 196 } 197 198 if (key == 199 'A' || key == 'B' || key == 'C' || key == 'D') //Detecting Buttons on Column 4 200 201 { 202 Num1 = Number; 203 Number =0; 204 if (key == 'A') 205 {Serial.println 206 ("Addition"); action = '+';} 207 if (key == 'B') 208 {Serial.println ("Subtraction"); 209 action = '-'; } 210 if (key == 'C') 211 {Serial.println ("Multiplication"); 212 action = '*';} 213 if (key == 'D') 214 {Serial.println ("Devesion"); action 215 = '/';} 216 217 delay(100); 218 } 219 220} 221 222void CalculateResult() 223{ 224 225 if (action=='+') 226 Number = Num1+Num2; 227 228 if (action=='-') 229 Number 230 = Num1-Num2; 231 232 if (action=='*') 233 Number = Num1*Num2; 234 235 if (action=='/') 236 237 Number = Num1/Num2; 238} 239 240void DisplayResult() 241{ 242 lcd.setCursor(0, 243 0); // set the cursor to column 0, line 1 244 lcd.print(Num1); lcd.print(action); 245 lcd.print(Num2); 246 247 if (result==true) 248 {lcd.print(" ="); lcd.print(Number);} 249 //Display the result 250 251 lcd.setCursor(0, 1); // set the cursor to column 252 0, line 1 253 lcd.print(Number); //Display the result 254} 255
Downloadable files
Arduino-Calculator-circuit-diagram
Arduino-Calculator-circuit-diagram
Arduino-Calculator-circuit-diagram
Arduino-Calculator-circuit-diagram
Comments
Only logged in users can leave comments
123samridhgarg
0 Followers
•0 Projects
Table of contents
Intro
50
0