Components and supplies
4x4 keypad
I2C 16x2 Arduino LCD Display Module
Jumper wires (generic)
Male/Female Jumper Wires
Alphanumeric LCD, 16 x 2
Arduino UNO
Apps and platforms
Arduino IDE
Project description
Code
Here is the code
c_cpp
1#include <LiquidCrystal_I2C.h> 2#include <Keypad.h> //Header file for Keypad from https://github.com/Chris--A/Keypad 3 4const byte ROWS = 4; // Four rows 5const byte COLS = 4; // Four columns 6 7// Define the Keymap 8char keys[ROWS][COLS] = { 9 10 {'1','2','3','A'}, 11 12 {'4','5','6','B'}, 13 14 {'7','8','9','C'}, 15 16 {'*','0','#','D'} 17 18}; 19 20byte rowPins[ROWS] = { 0, 1, 2, 3 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins. 21byte colPins[COLS] = { 4, 5, 6, 7 }; // Connect keypad COL0, COL1 and COL2 to these Arduino pins. 22 23Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Create the Keypad 24 25const int rs = 8, en = 9, d4 = 10, d5 = 11, d6 = 12, d7 = 13; //Pins to which LCD is connected 26LiquidCrystal_I2C lcd(0x27, 16, 2); 27 28 long Num1,Num2,Number; 29 char key,action; 30 boolean result = false; 31 32void setup() { 33 lcd.begin(); //We are using a 16*2 LCD display 34 lcd.print("Calculator Ready"); //Display a intro message 35 lcd.setCursor(0, 1); // set the cursor to column 0, line 1 36 lcd.print("A=+ B=- C=* D=/"); //Display a intro message 37 38 delay(6000); //Wait for display to show info 39 lcd.clear(); //Then clean it 40} 41 42void loop() { 43 44key = kpd.getKey(); //storing pressed key value in a char 45 46if (key!=NO_KEY) 47DetectButtons(); 48 49if (result==true) 50CalculateResult(); 51 52DisplayResult(); 53} 54 55void DetectButtons() 56{ 57 lcd.clear(); //Then clean it 58 if (key=='*') //If cancel Button is pressed 59 {Serial.println ("Button Cancel"); Number=Num1=Num2=0; result=false;} 60 61 if (key == '1') //If Button 1 is pressed 62 {Serial.println ("Button 1"); 63 if (Number==0) 64 Number=1; 65 else 66 Number = (Number*10) + 1; //Pressed twice 67 } 68 69 if (key == '4') //If Button 4 is pressed 70 {Serial.println ("Button 4"); 71 if (Number==0) 72 Number=4; 73 else 74 Number = (Number*10) + 4; //Pressed twice 75 } 76 77 if (key == '7') //If Button 7 is pressed 78 {Serial.println ("Button 7"); 79 if (Number==0) 80 Number=7; 81 else 82 Number = (Number*10) + 7; //Pressed twice 83 } 84 85 86 if (key == '0') 87 {Serial.println ("Button 0"); //Button 0 is Pressed 88 if (Number==0) 89 Number=0; 90 else 91 Number = (Number*10) + 0; //Pressed twice 92 } 93 94 if (key == '2') //Button 2 is Pressed 95 {Serial.println ("Button 2"); 96 if (Number==0) 97 Number=2; 98 else 99 Number = (Number*10) + 2; //Pressed twice 100 } 101 102 if (key == '5') 103 {Serial.println ("Button 5"); 104 if (Number==0) 105 Number=5; 106 else 107 Number = (Number*10) + 5; //Pressed twice 108 } 109 110 if (key == '8') 111 {Serial.println ("Button 8"); 112 if (Number==0) 113 Number=8; 114 else 115 Number = (Number*10) + 8; //Pressed twice 116 } 117 118 119 if (key == '#') 120 {Serial.println ("Button Equal"); 121 Num2=Number; 122 result = true; 123 } 124 125 if (key == '3') 126 {Serial.println ("Button 3"); 127 if (Number==0) 128 Number=3; 129 else 130 Number = (Number*10) + 3; //Pressed twice 131 } 132 133 if (key == '6') 134 {Serial.println ("Button 6"); 135 if (Number==0) 136 Number=6; 137 else 138 Number = (Number*10) + 6; //Pressed twice 139 } 140 141 if (key == '9') 142 {Serial.println ("Button 9"); 143 if (Number==0) 144 Number=9; 145 else 146 Number = (Number*10) + 9; //Pressed twice 147 } 148 149 if (key == 'A' || key == 'B' || key == 'C' || key == 'D') //Detecting Buttons on Column 4 150 { 151 Num1 = Number; 152 Number =0; 153 if (key == 'A') 154 {Serial.println ("Addition"); action = '+';} 155 if (key == 'B') 156 {Serial.println ("Subtraction"); action = '-'; } 157 if (key == 'C') 158 {Serial.println ("Multiplication"); action = '*';} 159 if (key == 'D') 160 {Serial.println ("Division"); action = '/';} 161 162 delay(100); 163 } 164 165} 166 167void CalculateResult() 168{ 169 if (action=='+') 170 Number = Num1+Num2; 171 172 if (action=='-') 173 Number = Num1-Num2; 174 175 if (action=='*') 176 Number = Num1*Num2; 177 178 if (action=='/') 179 Number = Num1/Num2; 180} 181 182void DisplayResult() 183{ 184 lcd.setCursor(0, 0); // set the cursor to column 0, line 1 185 lcd.print(Num1); lcd.print(action); lcd.print(Num2); 186 187 if (result==true) 188 {lcd.print(" ="); lcd.print(Number);} //Display the result 189 190 lcd.setCursor(0, 1); // set the cursor to column 0, line 1 191 lcd.print(Number); //Display the result 192} 193
Comments
Only logged in users can leave comments