Arduino - Math Calculator
An Arduino based math calculator, capable of adding, subtracting, multiplying and dividing.
Components and supplies
9V battery (generic)
Resistor 330 ohm
Rotary potentiometer (generic)
Matrix Keypad
Jumper wires (generic)
Alphanumeric LCD, 16 x 2
Arduino UNO
Tools and machines
Tape, Electrical
Scissor, Electrician
Project description
Code
Code
c_cpp
1#include <LiquidCrystal.h>//including the LCD libary for the project 2#include <Keypad.h> //including the keypad libary for the project 3 4LiquidCrystal lcd(A0, A1, A2, A3, A4, A5); 5const byte ROWS = 4; //setting the amount of rows on the keypad 6const byte COLM = 4; //setting the amount of columns on the keypad 7 8char keys[ROWS][COLM] = //setting the keys depending on the number of rows and colums 9{ 10 {'1', '2', '3', 'A'}, 11 {'4', '5', '6', 'B'}, 12 {'7', '8', '9', 'C'}, 13 {'*', '0', '#', 'D'} 14}; 15 16byte rowPins[ROWS] = {13, 12, 11, 10}; //defining the letters to GPIO pins of Arduino 17byte colmPins[COLM] = {9, 8, 7, 6}; 18 19Keypad keypad = Keypad(makeKeymap(keys), rowPins, colmPins, ROWS, COLM); //compiles all the arrays to create a keypad object 20 21float inputs[2]; //array used to store inputs entered by user for two numbers 22bool inputs_entered[2]; //array used for Arduino to see whether two inputs have been entered 23bool inputs_displayed[2]; //array used to inform Arduino whether the typed information has been printed 24 25char arithmetic_op; //character datatype used to store input entered by user for operation 26bool arithmetic_op_entered = false; //boolean datatype used for Arduino to see whether the input has been entered 27bool arithmetic_op_displayed = false; //boolean datatype used to inform Arduino whether the typed information has been printed 28 29void setup() { 30 Serial.begin(9600); //opening the serial communication channel at baud rate 9600(9600 bits) 31 lcd.begin(16,2); //setting up the LCD's number of columns and rows 32 33 lcd.setCursor(0,0); 34 lcd.print("Math calculator"); 35 delay(1000); 36 lcd.clear(); 37 38 ResetValues(); //function for resetting value 39 Calculator(); //function for calculating answer and displaying the result 40} 41 42void loop() {} //void loop is not being used but has to be kept in sketch for....well reasons 43 44void ResetValues() //function for resetting value 45{ 46 for (int i = 0; i < 2; i++) //for statement that runs whatever in the curly brackets twice; creates integer variable iteration(i), if (i) is less than 2[do whatever], add 1 to i 47 { 48 inputs_entered[i] = false; //inside the array, depending on the iteration, change the value(true) to false(as the iteraton increments so does the value being checked so both values are made false for resetting everything 49 inputs_displayed[i] = false; //once again(look above) resetting the value 50 51 inputs[i] = 0; //as this is a float, we cannot say false or true so instead we just set it back to 0 as a way to flush the value 52 } 53 54 arithmetic_op = NULL; // as it is a charcter, we cannot change the value inside to true or 0, instead null(in my opinion a form of saying no value, it's undefined, etc) 55 arithmetic_op_entered = false; //similar to above we are simply resetting the values and whether they have been entered 56 arithmetic_op_displayed = false; //resetting value 57} 58 59void Calculator() 60{ 61 ResetValues();//function for resetting 62 63 GetInputs(); //function for getting value from keypad 64 GetOperator();//function for getting operator value from keypad 65 66 switch (arithmetic_op) { //plugging operator from GetOperator function via a switch statement to complete functions 67 case 'A': 68 lcd.print(inputs[0] + inputs[1]); 69 delay(1000); 70 lcd.clear(); 71 break; 72 case 'B': 73 lcd.print(inputs[0] - inputs[1]); 74 delay(1000); 75 lcd.clear(); 76 break; 77 case 'C': 78 lcd.print(inputs[0] * inputs[1]); 79 delay(1000); 80 lcd.clear(); 81 break; 82 case 'D': 83 lcd.print(inputs[0] / inputs[1]); 84 delay(1000); 85 lcd.clear(); 86 break; 87 } 88 89 delay(100); //delay for 100 miliseconds 90 Serial.flush(); //flush potentional values from serial monitor 91 Calculator(); //calling function again, [looping] 92} 93 94void GetInputs() //function for getting inputs 95{ 96 while (!inputs_entered[0]) { //whilst inputs entered(variable used to store whether two values have been entered) is false 97 for (int i = 0; i < 2; i++) //looping contents twice as shown before(you may be asking how we are using the same variable again, we are not, this is a local variable not global, so we can use the same name again, further help in comments) 98 { 99 if (!inputs_entered[i] && !inputs_displayed[i]) //if inputs entered and inputs displayed are false 100 { 101 lcd.print("Input " + String(i) + ": "); //displaying what input it is for the number is via using the iteration from the for loop 102 inputs_displayed[i] = true; //setting display value to true, we have completed printing a line of input 1 or 2 103 104 while (!inputs_entered[i]) // if inputs_entered(from the two values inside) is false, get values 105 { 106 char customKey = keypad.getKey(); //getting value from keypad 107 if (customKey != NO_KEY) // if there is a output which is not a blank space(a keypad outputs responses even if there is no button press, resulting in "empty" values) we get around this via using this "function" 108 { 109 customKey = convertValues(customKey); //as the value outputed is a character, in order to do arithmetic calculations, we must convert it to a integer 110 inputs[i] = customKey; //depending on what iteration we are on add the corrosponding letter to the inputs array 111 lcd.print(inputs[i]); //printing the value 112 delay(1000); 113 lcd.clear(); 114 inputs_entered[i] = true; //in the array depending on what iteration set the value to true as the process has been completed 115 } 116 } 117 } 118 } 119 } 120} 121 122void GetOperator()//function for getting operator 123{ 124 while (!arithmetic_op_entered) { //process is very similar to above in getting two values to perform arithmetic operations(if anyone is confused, don't hesitate to comment) 125 if (!arithmetic_op_entered && !arithmetic_op_displayed) 126 { 127 lcd.print("Operator: "); 128 arithmetic_op_displayed = true; 129 130 while (!arithmetic_op_entered) 131 { 132 char customKey = keypad.getKey(); 133 if (customKey != NO_KEY) 134 { 135 arithmetic_op = customKey; 136 lcd.print(keypad.getKey()); 137 delay(1000); 138 lcd.clear(); 139 arithmetic_op_entered = true; 140 } 141 } 142 } 143 } 144} 145 146int convertValues(int x) //function used above to convert character value into integer 147{ 148 return x - '0'; //returning changed character value now to integer value 149}
Image of Math calculator
c_cpp
Using the keypad you can perform a series of arithmetic operations using one digit numbers
1#include <LiquidCrystal.h>//including the LCD libary for the project 2#include <Keypad.h> //including the keypad libary for the project 3 4LiquidCrystal lcd(A0, A1, A2, A3, A4, A5); 5const byte ROWS = 4; //setting the amount of rows on the keypad 6const byte COLM = 4; //setting the amount of columns on the keypad 7 8char keys[ROWS][COLM] = //setting the keys depending on the number of rows and colums 9{ 10 {'1', '2', '3', 'A'}, 11 {'4', '5', '6', 'B'}, 12 {'7', '8', '9', 'C'}, 13 {'*', '0', '#', 'D'} 14}; 15 16byte rowPins[ROWS] = {13, 12, 11, 10}; //defining the letters to GPIO pins of Arduino 17byte colmPins[COLM] = {9, 8, 7, 6}; 18 19Keypad keypad = Keypad(makeKeymap(keys), rowPins, colmPins, ROWS, COLM); //compiles all the arrays to create a keypad object 20 21float inputs[2]; //array used to store inputs entered by user for two numbers 22bool inputs_entered[2]; //array used for Arduino to see whether two inputs have been entered 23bool inputs_displayed[2]; //array used to inform Arduino whether the typed information has been printed 24 25char arithmetic_op; //character datatype used to store input entered by user for operation 26bool arithmetic_op_entered = false; //boolean datatype used for Arduino to see whether the input has been entered 27bool arithmetic_op_displayed = false; //boolean datatype used to inform Arduino whether the typed information has been printed 28 29void setup() { 30 Serial.begin(9600); //opening the serial communication channel at baud rate 9600(9600 bits) 31 lcd.begin(16,2); //setting up the LCD's number of columns and rows 32 33 lcd.setCursor(0,0); 34 lcd.print("Math calculator"); 35 delay(1000); 36 lcd.clear(); 37 38 ResetValues(); //function for resetting value 39 Calculator(); //function for calculating answer and displaying the result 40} 41 42void loop() {} //void loop is not being used but has to be kept in sketch for....well reasons 43 44void ResetValues() //function for resetting value 45{ 46 for (int i = 0; i < 2; i++) //for statement that runs whatever in the curly brackets twice; creates integer variable iteration(i), if (i) is less than 2[do whatever], add 1 to i 47 { 48 inputs_entered[i] = false; //inside the array, depending on the iteration, change the value(true) to false(as the iteraton increments so does the value being checked so both values are made false for resetting everything 49 inputs_displayed[i] = false; //once again(look above) resetting the value 50 51 inputs[i] = 0; //as this is a float, we cannot say false or true so instead we just set it back to 0 as a way to flush the value 52 } 53 54 arithmetic_op = NULL; // as it is a charcter, we cannot change the value inside to true or 0, instead null(in my opinion a form of saying no value, it's undefined, etc) 55 arithmetic_op_entered = false; //similar to above we are simply resetting the values and whether they have been entered 56 arithmetic_op_displayed = false; //resetting value 57} 58 59void Calculator() 60{ 61 ResetValues();//function for resetting 62 63 GetInputs(); //function for getting value from keypad 64 GetOperator();//function for getting operator value from keypad 65 66 switch (arithmetic_op) { //plugging operator from GetOperator function via a switch statement to complete functions 67 case 'A': 68 lcd.print(inputs[0] + inputs[1]); 69 delay(1000); 70 lcd.clear(); 71 break; 72 case 'B': 73 lcd.print(inputs[0] - inputs[1]); 74 delay(1000); 75 lcd.clear(); 76 break; 77 case 'C': 78 lcd.print(inputs[0] * inputs[1]); 79 delay(1000); 80 lcd.clear(); 81 break; 82 case 'D': 83 lcd.print(inputs[0] / inputs[1]); 84 delay(1000); 85 lcd.clear(); 86 break; 87 } 88 89 delay(100); //delay for 100 miliseconds 90 Serial.flush(); //flush potentional values from serial monitor 91 Calculator(); //calling function again, [looping] 92} 93 94void GetInputs() //function for getting inputs 95{ 96 while (!inputs_entered[0]) { //whilst inputs entered(variable used to store whether two values have been entered) is false 97 for (int i = 0; i < 2; i++) //looping contents twice as shown before(you may be asking how we are using the same variable again, we are not, this is a local variable not global, so we can use the same name again, further help in comments) 98 { 99 if (!inputs_entered[i] && !inputs_displayed[i]) //if inputs entered and inputs displayed are false 100 { 101 lcd.print("Input " + String(i) + ": "); //displaying what input it is for the number is via using the iteration from the for loop 102 inputs_displayed[i] = true; //setting display value to true, we have completed printing a line of input 1 or 2 103 104 while (!inputs_entered[i]) // if inputs_entered(from the two values inside) is false, get values 105 { 106 char customKey = keypad.getKey(); //getting value from keypad 107 if (customKey != NO_KEY) // if there is a output which is not a blank space(a keypad outputs responses even if there is no button press, resulting in "empty" values) we get around this via using this "function" 108 { 109 customKey = convertValues(customKey); //as the value outputed is a character, in order to do arithmetic calculations, we must convert it to a integer 110 inputs[i] = customKey; //depending on what iteration we are on add the corrosponding letter to the inputs array 111 lcd.print(inputs[i]); //printing the value 112 delay(1000); 113 lcd.clear(); 114 inputs_entered[i] = true; //in the array depending on what iteration set the value to true as the process has been completed 115 } 116 } 117 } 118 } 119 } 120} 121 122void GetOperator()//function for getting operator 123{ 124 while (!arithmetic_op_entered) { //process is very similar to above in getting two values to perform arithmetic operations(if anyone is confused, don't hesitate to comment) 125 if (!arithmetic_op_entered && !arithmetic_op_displayed) 126 { 127 lcd.print("Operator: "); 128 arithmetic_op_displayed = true; 129 130 while (!arithmetic_op_entered) 131 { 132 char customKey = keypad.getKey(); 133 if (customKey != NO_KEY) 134 { 135 arithmetic_op = customKey; 136 lcd.print(keypad.getKey()); 137 delay(1000); 138 lcd.clear(); 139 arithmetic_op_entered = true; 140 } 141 } 142 } 143 } 144} 145 146int convertValues(int x) //function used above to convert character value into integer 147{ 148 return x - '0'; //returning changed character value now to integer value 149}
Code
c_cpp
1#include <LiquidCrystal.h>//including the LCD libary for the project 2#include <Keypad.h> //including the keypad libary for the project 3 4LiquidCrystal lcd(A0, A1, A2, A3, A4, A5); 5const byte ROWS = 4; //setting the amount of rows on the keypad 6const byte COLM = 4; //setting the amount of columns on the keypad 7 8char keys[ROWS][COLM] = //setting the keys depending on the number of rows and colums 9{ 10 {'1', '2', '3', 'A'}, 11 {'4', '5', '6', 'B'}, 12 {'7', '8', '9', 'C'}, 13 {'*', '0', '#', 'D'} 14}; 15 16byte rowPins[ROWS] = {13, 12, 11, 10}; //defining the letters to GPIO pins of Arduino 17byte colmPins[COLM] = {9, 8, 7, 6}; 18 19Keypad keypad = Keypad(makeKeymap(keys), rowPins, colmPins, ROWS, COLM); //compiles all the arrays to create a keypad object 20 21float inputs[2]; //array used to store inputs entered by user for two numbers 22bool inputs_entered[2]; //array used for Arduino to see whether two inputs have been entered 23bool inputs_displayed[2]; //array used to inform Arduino whether the typed information has been printed 24 25char arithmetic_op; //character datatype used to store input entered by user for operation 26bool arithmetic_op_entered = false; //boolean datatype used for Arduino to see whether the input has been entered 27bool arithmetic_op_displayed = false; //boolean datatype used to inform Arduino whether the typed information has been printed 28 29void setup() { 30 Serial.begin(9600); //opening the serial communication channel at baud rate 9600(9600 bits) 31 lcd.begin(16,2); //setting up the LCD's number of columns and rows 32 33 lcd.setCursor(0,0); 34 lcd.print("Math calculator"); 35 delay(1000); 36 lcd.clear(); 37 38 ResetValues(); //function for resetting value 39 Calculator(); //function for calculating answer and displaying the result 40} 41 42void loop() {} //void loop is not being used but has to be kept in sketch for....well reasons 43 44void ResetValues() //function for resetting value 45{ 46 for (int i = 0; i < 2; i++) //for statement that runs whatever in the curly brackets twice; creates integer variable iteration(i), if (i) is less than 2[do whatever], add 1 to i 47 { 48 inputs_entered[i] = false; //inside the array, depending on the iteration, change the value(true) to false(as the iteraton increments so does the value being checked so both values are made false for resetting everything 49 inputs_displayed[i] = false; //once again(look above) resetting the value 50 51 inputs[i] = 0; //as this is a float, we cannot say false or true so instead we just set it back to 0 as a way to flush the value 52 } 53 54 arithmetic_op = NULL; // as it is a charcter, we cannot change the value inside to true or 0, instead null(in my opinion a form of saying no value, it's undefined, etc) 55 arithmetic_op_entered = false; //similar to above we are simply resetting the values and whether they have been entered 56 arithmetic_op_displayed = false; //resetting value 57} 58 59void Calculator() 60{ 61 ResetValues();//function for resetting 62 63 GetInputs(); //function for getting value from keypad 64 GetOperator();//function for getting operator value from keypad 65 66 switch (arithmetic_op) { //plugging operator from GetOperator function via a switch statement to complete functions 67 case 'A': 68 lcd.print(inputs[0] + inputs[1]); 69 delay(1000); 70 lcd.clear(); 71 break; 72 case 'B': 73 lcd.print(inputs[0] - inputs[1]); 74 delay(1000); 75 lcd.clear(); 76 break; 77 case 'C': 78 lcd.print(inputs[0] * inputs[1]); 79 delay(1000); 80 lcd.clear(); 81 break; 82 case 'D': 83 lcd.print(inputs[0] / inputs[1]); 84 delay(1000); 85 lcd.clear(); 86 break; 87 } 88 89 delay(100); //delay for 100 miliseconds 90 Serial.flush(); //flush potentional values from serial monitor 91 Calculator(); //calling function again, [looping] 92} 93 94void GetInputs() //function for getting inputs 95{ 96 while (!inputs_entered[0]) { //whilst inputs entered(variable used to store whether two values have been entered) is false 97 for (int i = 0; i < 2; i++) //looping contents twice as shown before(you may be asking how we are using the same variable again, we are not, this is a local variable not global, so we can use the same name again, further help in comments) 98 { 99 if (!inputs_entered[i] && !inputs_displayed[i]) //if inputs entered and inputs displayed are false 100 { 101 lcd.print("Input " + String(i) + ": "); //displaying what input it is for the number is via using the iteration from the for loop 102 inputs_displayed[i] = true; //setting display value to true, we have completed printing a line of input 1 or 2 103 104 while (!inputs_entered[i]) // if inputs_entered(from the two values inside) is false, get values 105 { 106 char customKey = keypad.getKey(); //getting value from keypad 107 if (customKey != NO_KEY) // if there is a output which is not a blank space(a keypad outputs responses even if there is no button press, resulting in "empty" values) we get around this via using this "function" 108 { 109 customKey = convertValues(customKey); //as the value outputed is a character, in order to do arithmetic calculations, we must convert it to a integer 110 inputs[i] = customKey; //depending on what iteration we are on add the corrosponding letter to the inputs array 111 lcd.print(inputs[i]); //printing the value 112 delay(1000); 113 lcd.clear(); 114 inputs_entered[i] = true; //in the array depending on what iteration set the value to true as the process has been completed 115 } 116 } 117 } 118 } 119 } 120} 121 122void GetOperator()//function for getting operator 123{ 124 while (!arithmetic_op_entered) { //process is very similar to above in getting two values to perform arithmetic operations(if anyone is confused, don't hesitate to comment) 125 if (!arithmetic_op_entered && !arithmetic_op_displayed) 126 { 127 lcd.print("Operator: "); 128 arithmetic_op_displayed = true; 129 130 while (!arithmetic_op_entered) 131 { 132 char customKey = keypad.getKey(); 133 if (customKey != NO_KEY) 134 { 135 arithmetic_op = customKey; 136 lcd.print(keypad.getKey()); 137 delay(1000); 138 lcd.clear(); 139 arithmetic_op_entered = true; 140 } 141 } 142 } 143 } 144} 145 146int convertValues(int x) //function used above to convert character value into integer 147{ 148 return x - '0'; //returning changed character value now to integer value 149}
Image of Math calculator
c_cpp
Using the keypad you can perform a series of arithmetic operations using one digit numbers
1#include <LiquidCrystal.h>//including the LCD libary for the project 2#include 3 <Keypad.h> //including the keypad libary for the project 4 5LiquidCrystal lcd(A0, 6 A1, A2, A3, A4, A5); 7const byte ROWS = 4; //setting the amount of rows on the 8 keypad 9const byte COLM = 4; //setting the amount of columns on the keypad 10 11char 12 keys[ROWS][COLM] = //setting the keys depending on the number of rows and colums 13{ 14 15 {'1', '2', '3', 'A'}, 16 {'4', '5', '6', 'B'}, 17 {'7', '8', '9', 'C'}, 18 19 {'*', '0', '#', 'D'} 20}; 21 22byte rowPins[ROWS] = {13, 12, 11, 10}; //defining 23 the letters to GPIO pins of Arduino 24byte colmPins[COLM] = {9, 8, 7, 6}; 25 26Keypad 27 keypad = Keypad(makeKeymap(keys), rowPins, colmPins, ROWS, COLM); //compiles all 28 the arrays to create a keypad object 29 30float inputs[2]; //array used to store 31 inputs entered by user for two numbers 32bool inputs_entered[2]; //array used for 33 Arduino to see whether two inputs have been entered 34bool inputs_displayed[2]; 35 //array used to inform Arduino whether the typed information has been printed 36 37char 38 arithmetic_op; //character datatype used to store input entered by user for operation 39bool 40 arithmetic_op_entered = false; //boolean datatype used for Arduino to see whether 41 the input has been entered 42bool arithmetic_op_displayed = false; //boolean datatype 43 used to inform Arduino whether the typed information has been printed 44 45void 46 setup() { 47 Serial.begin(9600); //opening the serial communication channel at 48 baud rate 9600(9600 bits) 49 lcd.begin(16,2); //setting up the LCD's number of 50 columns and rows 51 52 lcd.setCursor(0,0); 53 lcd.print("Math calculator"); 54 55 delay(1000); 56 lcd.clear(); 57 58 ResetValues(); //function for resetting 59 value 60 Calculator(); //function for calculating answer and displaying the result 61} 62 63void 64 loop() {} //void loop is not being used but has to be kept in sketch for....well 65 reasons 66 67void ResetValues() //function for resetting value 68{ 69 for (int 70 i = 0; i < 2; i++) //for statement that runs whatever in the curly brackets twice; 71 creates integer variable iteration(i), if (i) is less than 2[do whatever], add 1 72 to i 73 { 74 inputs_entered[i] = false; //inside the array, depending on the 75 iteration, change the value(true) to false(as the iteraton increments so does the 76 value being checked so both values are made false for resetting everything 77 78 inputs_displayed[i] = false; //once again(look above) resetting the value 79 80 81 inputs[i] = 0; //as this is a float, we cannot say false or true so instead 82 we just set it back to 0 as a way to flush the value 83 } 84 85 arithmetic_op 86 = NULL; // as it is a charcter, we cannot change the value inside to true or 0, 87 instead null(in my opinion a form of saying no value, it's undefined, etc) 88 arithmetic_op_entered 89 = false; //similar to above we are simply resetting the values and whether they 90 have been entered 91 arithmetic_op_displayed = false; //resetting value 92} 93 94void 95 Calculator() 96{ 97 ResetValues();//function for resetting 98 99 GetInputs(); 100 //function for getting value from keypad 101 GetOperator();//function for getting 102 operator value from keypad 103 104 switch (arithmetic_op) { //plugging operator 105 from GetOperator function via a switch statement to complete functions 106 case 107 'A': 108 lcd.print(inputs[0] + inputs[1]); 109 delay(1000); 110 lcd.clear(); 111 112 break; 113 case 'B': 114 lcd.print(inputs[0] - inputs[1]); 115 delay(1000); 116 117 lcd.clear(); 118 break; 119 case 'C': 120 lcd.print(inputs[0] 121 * inputs[1]); 122 delay(1000); 123 lcd.clear(); 124 break; 125 case 126 'D': 127 lcd.print(inputs[0] / inputs[1]); 128 delay(1000); 129 lcd.clear(); 130 131 break; 132 } 133 134 delay(100); //delay for 100 miliseconds 135 Serial.flush(); 136 //flush potentional values from serial monitor 137 Calculator(); //calling function 138 again, [looping] 139} 140 141void GetInputs() //function for getting inputs 142{ 143 144 while (!inputs_entered[0]) { //whilst inputs entered(variable used to store whether 145 two values have been entered) is false 146 for (int i = 0; i < 2; i++) //looping 147 contents twice as shown before(you may be asking how we are using the same variable 148 again, we are not, this is a local variable not global, so we can use the same name 149 again, further help in comments) 150 { 151 if (!inputs_entered[i] && !inputs_displayed[i]) 152 //if inputs entered and inputs displayed are false 153 { 154 lcd.print("Input 155 " + String(i) + ": "); //displaying what input it is for the number is via using 156 the iteration from the for loop 157 inputs_displayed[i] = true; //setting 158 display value to true, we have completed printing a line of input 1 or 2 159 160 161 while (!inputs_entered[i]) // if inputs_entered(from the two values inside) 162 is false, get values 163 { 164 char customKey = keypad.getKey(); 165 //getting value from keypad 166 if (customKey != NO_KEY) // if there is 167 a output which is not a blank space(a keypad outputs responses even if there is 168 no button press, resulting in "empty" values) we get around this via using this 169 "function" 170 { 171 customKey = convertValues(customKey); 172 //as the value outputed is a character, in order to do arithmetic calculations, 173 we must convert it to a integer 174 inputs[i] = customKey; //depending 175 on what iteration we are on add the corrosponding letter to the inputs array 176 177 lcd.print(inputs[i]); //printing the value 178 delay(1000); 179 180 lcd.clear(); 181 inputs_entered[i] = true; //in the array 182 depending on what iteration set the value to true as the process has been completed 183 184 } 185 } 186 } 187 } 188 } 189} 190 191void GetOperator()//function 192 for getting operator 193{ 194 while (!arithmetic_op_entered) { //process is very 195 similar to above in getting two values to perform arithmetic operations(if anyone 196 is confused, don't hesitate to comment) 197 if (!arithmetic_op_entered && !arithmetic_op_displayed) 198 199 { 200 lcd.print("Operator: "); 201 arithmetic_op_displayed = true; 202 203 204 while (!arithmetic_op_entered) 205 { 206 char customKey = keypad.getKey(); 207 208 if (customKey != NO_KEY) 209 { 210 arithmetic_op = customKey; 211 212 lcd.print(keypad.getKey()); 213 delay(1000); 214 lcd.clear(); 215 216 arithmetic_op_entered = true; 217 } 218 } 219 } 220 } 221} 222 223int 224 convertValues(int x) //function used above to convert character value into integer 225{ 226 227 return x - '0'; //returning changed character value now to integer value 228}
Image of Math calculator
c_cpp
Using the keypad you can perform a series of arithmetic operations using one digit numbers
1#include <LiquidCrystal.h>//including the LCD libary for the project 2#include <Keypad.h> //including the keypad libary for the project 3 4LiquidCrystal lcd(A0, A1, A2, A3, A4, A5); 5const byte ROWS = 4; //setting the amount of rows on the keypad 6const byte COLM = 4; //setting the amount of columns on the keypad 7 8char keys[ROWS][COLM] = //setting the keys depending on the number of rows and colums 9{ 10 {'1', '2', '3', 'A'}, 11 {'4', '5', '6', 'B'}, 12 {'7', '8', '9', 'C'}, 13 {'*', '0', '#', 'D'} 14}; 15 16byte rowPins[ROWS] = {13, 12, 11, 10}; //defining the letters to GPIO pins of Arduino 17byte colmPins[COLM] = {9, 8, 7, 6}; 18 19Keypad keypad = Keypad(makeKeymap(keys), rowPins, colmPins, ROWS, COLM); //compiles all the arrays to create a keypad object 20 21float inputs[2]; //array used to store inputs entered by user for two numbers 22bool inputs_entered[2]; //array used for Arduino to see whether two inputs have been entered 23bool inputs_displayed[2]; //array used to inform Arduino whether the typed information has been printed 24 25char arithmetic_op; //character datatype used to store input entered by user for operation 26bool arithmetic_op_entered = false; //boolean datatype used for Arduino to see whether the input has been entered 27bool arithmetic_op_displayed = false; //boolean datatype used to inform Arduino whether the typed information has been printed 28 29void setup() { 30 Serial.begin(9600); //opening the serial communication channel at baud rate 9600(9600 bits) 31 lcd.begin(16,2); //setting up the LCD's number of columns and rows 32 33 lcd.setCursor(0,0); 34 lcd.print("Math calculator"); 35 delay(1000); 36 lcd.clear(); 37 38 ResetValues(); //function for resetting value 39 Calculator(); //function for calculating answer and displaying the result 40} 41 42void loop() {} //void loop is not being used but has to be kept in sketch for....well reasons 43 44void ResetValues() //function for resetting value 45{ 46 for (int i = 0; i < 2; i++) //for statement that runs whatever in the curly brackets twice; creates integer variable iteration(i), if (i) is less than 2[do whatever], add 1 to i 47 { 48 inputs_entered[i] = false; //inside the array, depending on the iteration, change the value(true) to false(as the iteraton increments so does the value being checked so both values are made false for resetting everything 49 inputs_displayed[i] = false; //once again(look above) resetting the value 50 51 inputs[i] = 0; //as this is a float, we cannot say false or true so instead we just set it back to 0 as a way to flush the value 52 } 53 54 arithmetic_op = NULL; // as it is a charcter, we cannot change the value inside to true or 0, instead null(in my opinion a form of saying no value, it's undefined, etc) 55 arithmetic_op_entered = false; //similar to above we are simply resetting the values and whether they have been entered 56 arithmetic_op_displayed = false; //resetting value 57} 58 59void Calculator() 60{ 61 ResetValues();//function for resetting 62 63 GetInputs(); //function for getting value from keypad 64 GetOperator();//function for getting operator value from keypad 65 66 switch (arithmetic_op) { //plugging operator from GetOperator function via a switch statement to complete functions 67 case 'A': 68 lcd.print(inputs[0] + inputs[1]); 69 delay(1000); 70 lcd.clear(); 71 break; 72 case 'B': 73 lcd.print(inputs[0] - inputs[1]); 74 delay(1000); 75 lcd.clear(); 76 break; 77 case 'C': 78 lcd.print(inputs[0] * inputs[1]); 79 delay(1000); 80 lcd.clear(); 81 break; 82 case 'D': 83 lcd.print(inputs[0] / inputs[1]); 84 delay(1000); 85 lcd.clear(); 86 break; 87 } 88 89 delay(100); //delay for 100 miliseconds 90 Serial.flush(); //flush potentional values from serial monitor 91 Calculator(); //calling function again, [looping] 92} 93 94void GetInputs() //function for getting inputs 95{ 96 while (!inputs_entered[0]) { //whilst inputs entered(variable used to store whether two values have been entered) is false 97 for (int i = 0; i < 2; i++) //looping contents twice as shown before(you may be asking how we are using the same variable again, we are not, this is a local variable not global, so we can use the same name again, further help in comments) 98 { 99 if (!inputs_entered[i] && !inputs_displayed[i]) //if inputs entered and inputs displayed are false 100 { 101 lcd.print("Input " + String(i) + ": "); //displaying what input it is for the number is via using the iteration from the for loop 102 inputs_displayed[i] = true; //setting display value to true, we have completed printing a line of input 1 or 2 103 104 while (!inputs_entered[i]) // if inputs_entered(from the two values inside) is false, get values 105 { 106 char customKey = keypad.getKey(); //getting value from keypad 107 if (customKey != NO_KEY) // if there is a output which is not a blank space(a keypad outputs responses even if there is no button press, resulting in "empty" values) we get around this via using this "function" 108 { 109 customKey = convertValues(customKey); //as the value outputed is a character, in order to do arithmetic calculations, we must convert it to a integer 110 inputs[i] = customKey; //depending on what iteration we are on add the corrosponding letter to the inputs array 111 lcd.print(inputs[i]); //printing the value 112 delay(1000); 113 lcd.clear(); 114 inputs_entered[i] = true; //in the array depending on what iteration set the value to true as the process has been completed 115 } 116 } 117 } 118 } 119 } 120} 121 122void GetOperator()//function for getting operator 123{ 124 while (!arithmetic_op_entered) { //process is very similar to above in getting two values to perform arithmetic operations(if anyone is confused, don't hesitate to comment) 125 if (!arithmetic_op_entered && !arithmetic_op_displayed) 126 { 127 lcd.print("Operator: "); 128 arithmetic_op_displayed = true; 129 130 while (!arithmetic_op_entered) 131 { 132 char customKey = keypad.getKey(); 133 if (customKey != NO_KEY) 134 { 135 arithmetic_op = customKey; 136 lcd.print(keypad.getKey()); 137 delay(1000); 138 lcd.clear(); 139 arithmetic_op_entered = true; 140 } 141 } 142 } 143 } 144} 145 146int convertValues(int x) //function used above to convert character value into integer 147{ 148 return x - '0'; //returning changed character value now to integer value 149}
Downloadable files
screenshot_2020-12-24_at_17_06_30_HcCIwqaCos.png
screenshot_2020-12-24_at_17_06_30_HcCIwqaCos.png

screenshot_2020-12-24_at_17_06_30_HcCIwqaCos.png
screenshot_2020-12-24_at_17_06_30_HcCIwqaCos.png

Comments
Only logged in users can leave comments