Yesterday, I've got myself an arduino uno. Since I'm pretty newbie in It, I'd like to ask here for a hint, because I can't manage to make my calculator working. I don't have much experience in programming, but II thought, the loop should wait for one of the function over again, and probably that's why I getting error in console output.
Code:
#include <Key.h>
#include <Keypad.h>
const byte rows = 4;
const byte columns = 4;
char first_number;
char keyboard[rows][columns] = {
{'1','2','3','+'},
{'4','5','6','-'},
{'7','8','9','/'},
{'C','0','=','*'}
};
byte rowsPins[rows] = {9, 8, 7, 6};
byte columnsPins[columns] = {5, 4, 3, 2};
Keypad keys = Keypad(makeKeymap(keyboard), rowsPins, columnsPins, rows, columns);
void setup(){
Serial.begin(9600);
}
int getNumber(){
char user_input = keys.getKey();
int value;
if (user_input){
switch (user_input){
case '0':
value = 0;
break;
case '1':
value = 1;
break;
case '2':
value = 2;
break;
case '3':
value = 3;
break;
case '4':
value = 4;
break;
case '5':
value = 5;
break;
case '6':
value = 6;
break;
case '7':
value = 7;
break;
case '8':
value = 8;
break;
case '9':
value = 9;
break;
default:
Serial.print("Error int");
}
}
return value;
}
int calculate(int first_number, int second_number){
char operation = keys.getKey();
int value;
switch(operation){
case '+':
value = first_number+second_number;
break;
case '-':
value = first_number-second_number;
break;
case '*':
value = first_number*second_number;
break;
case '/':
value = first_number/second_number;
break;
default:
Serial.println("Error cal");
}
if (value){
Serial.println(value);
}
return value;
}
void loop(){
int first_number = getNumber();
int second_number = getNumber();
int test = calculate(first_number, second_number);
}
What I wanted to do, is read numeric input from the keypad twice (it's just for now. I know it should be done different), and then, input the operation, such as multiply, but since the loop calling the function all over again, I can't actually do it. How I should approach it?