Can't manage my calculator to work

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?

byte getNumber(){
  bool isCharGet = false;
  byte value=0;
  while (!isCharGet ){
    char user_input = keys.getKey();
    if (user_input>='0' && user_input<='9'){
      value =user_input-'0';
      isCharGet =true;
    }
  }
  return value;
}

doesn't getKey() return a specific value indicating no key was pressed?

Yes. The value zero.

only allows one chance for a key to have been pressed. To wait until a key has been pressed:

  char user_input;
  int value = -1;
  do
  {
    user_input = keys.getKey();
  } while (user_input == 0);

If you ever want to extend to multi-digit numbers you need to add something like an 'enter' key to indicate the end of a number.

@anwarimr
Follow the logic of the following sketch for the implementation of a Basic Calculator. After that try to minimize the duplication of similar codes.

#include <Keypad.h>

const byte rows = 4;
const byte columns = 4;
char first_Number[3] = {0};
char second_Number[3] = {0};
int i, j;
char user_input;


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);
  Serial.print("Enter First 2-digit Number: ");
}

void loop()
{
  user_input = keys.getKey();
  if (user_input != 0)
  {
    first_Number[i] = user_input;
    Serial.print(first_Number[i]);
    i++;
    if (i == 2)
    {
      byte firstNumber = atoi(first_Number);
      Serial.println();
      //Serial.println(firstNumber);
      //---------------------------
      Serial.print("Enter Second 2-digit Number: ");
      do
      {
        user_input = keys.getKey();
        if (user_input != 0)
        {
          second_Number[j] = user_input;
          Serial.print(second_Number[j]);
          j++;
          if (j == 2)
          {
            byte secondNumber = atoi(second_Number);
            Serial.println();
            //Serial.println(secondNumber);
            //-----------------------------
            Serial.print("Enter Opearation: ");
            do
            {
              user_input = keys.getKey();
              if (user_input != 0)
              {
                Serial.print(user_input);
                Serial.println();
                //------------------------
                switch (user_input)
                {
                  case '+':
                    Serial.print("Sum is: ");
                    Serial.println(firstNumber + secondNumber);
                    break;
                  case '-':
                    Serial.print("Difference is: ");
                    Serial.println(firstNumber - secondNumber);
                    break;
                  case '*':
                    Serial.print("Multiplication is: ");
                    Serial.println(firstNumber * secondNumber);
                    break;
                  case '/':
                    Serial.print("Dividend is: ");
                    Serial.println(firstNumber / secondNumber);
                    break;
                  default:
                    Serial.print("Undefined!");
                    while (1);
                }
              }
            }
            while (!user_input);
          }
        }
      }
      while (i != 4);
    }
  }
}

Output:

Enter First 2-digit Number: 12
Enter Second 2-digit Number: 34
Enter Operation: +
Sum is: 46
//--------------------------------------------
Enter First 2-digit Number: 34
Enter Second 2-digit Number: 12
Enter Operation: -
Difference is: 22
//-----------------------------------------
Enter First 2-digit Number: 39
Enter Second 2-digit Number: 13
Enter Operration: /
Dividend is: 3
//----------------------------------------
Enter First 2-digit Number: 34
Enter Second 2-digit Number: 10
Enter Operation: *
Product is: 22

You have just re-invented Reverse Polish Notation.
https://en.wikipedia.org/wiki/Reverse_Polish_notation

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.