#include <Keypad.h>
float x1, x2,out;
char c;
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char key;
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() {
Serial.println("");
Serial.println("NEW OPERATION");
for (int i = 0; i < 3; i++) {
key = keypad.getKey();
if (key) {
if(key=='C'){
loop();
break;
}
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((x1<0||x1>9)||(x2<0||x2>9)||!(c=='*'||c=='/'||c=='+'||c=='-')){
Serial.println("WRONG INPUT!!!");
loop();
}
if(c=='*'){
Serial.println(out=x1*x2);
}
if(c=='/'){
Serial.println(out=x1/x2);
}
if(c=='+'){
Serial.println(out=x1+x2);
}
if(c=='-'){
Serial.println(out=x1-x2);
}
while(1){
Serial.print(out);
for (int i = 1; i < 3; i++) {
key = keypad.getKey();
if (key) {
if(key=='C'){
loop();
break;
}
Serial.print(key);
if (i == 1) {
c = key;
}
if (i == 2) {
x2 = int(key) - 48;
Serial.print(" = ");
}
}
else {
i--;
}
}
if((x2<0||x2>9)||!(c=='*'||c=='/'||c=='+'||c=='-')){
Serial.println("WRONG INPUT!!!");
loop();
}
if(c=='*'){
Serial.println(out=out*x2);
}
if(c=='/'){
Serial.println(out=out/x2);
}
if(c=='+'){
Serial.println(out=out+x2);
}
if(c=='-'){
Serial.println(out=out-x2);
}}
}