#include <Keypad.h>
float x1, x2;
char c;
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '/', '+', '-'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < 3; i++) {
char key = keypad.getKey();
if (key) {
Serial.print(key);
if (i == 0) {
x1 = int(key) - 48;
}
if (i == 1) {
c = key;
}
if (i == 2) {
x2 = int(key) - 48;
Serial.print(" = ");
}
}
else {
i--;
}
}
if(c=='*'){
Serial.println(x1*x2);
}
if(c=='/'){
Serial.println(x1/x2);
}
if(c=='+'){
Serial.println(x1+x2);
}
if(c=='-'){
Serial.println(x1-x2);
}
}