Arduino UNO calculator

attempting to make a basic calculator but it just outputs 7’s and 8’s and occasionally 0’s.
i have yet to wire up the keypad i am just testing with bare wires.


#include <Arduino.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>

const byte ROWS = 5;
const byte COLS = 4;

char keys[ROWS][COLS] = {
  {'C','/','*','-'},
  
  {'7','8','9','+'},

  {'4','5','6','+'},

  {'1','2','3','='},

  {'0','0','.','='}

};

byte rowPins[ROWS] = { 0, 1, 2, 3, 4 };
byte colPins[COLS] = { 5, 6, 7, 8 };

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); 

const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = A0;
 
void setup() {
  lcd.begin(16, 2);
  lcd.print("QRWMH-Y10T422");
  lcd.setCursor(0, 1);
  lcd.print("Starting...");

   delay(2000);
    lcd.clear();`Preformatted text`
}

void updateCursor() {
  if (millis() / 250 % 2 == 0 ) {
    lcd.cursor();
  } else {
    lcd.noCursor();
  }
}

char operation = 0;
String memory = "";
String current = "";
uint64_t currentDecimal;
bool decimalPoint = false;

double calculate(char operation, double left, double right) 
{switch (operation) {
    case '+': return left + right;
    case '-': return left - right;
    case '*': return left * right;
    case '/': return left / right;
  }
}

void processInput(char key) 
{if ('-' == key && current == "") {
    current = "-";
    lcd.print("-");
    return;
  }

  switch (key) 
  {
    case '+':
    case '-':
    case '*':
    case '/':
      if (!operation) {
        memory = current;
        current = "";
      }
      operation = key;
      lcd.setCursor(0, 1);
      lcd.print(key);
      lcd.setCursor(current.length() + 1, 1);
      return;

    case '=':
      float leftNum = memory.toDouble();
      float rightNum = current.toDouble();
      memory = String(calculate(operation, leftNum, rightNum));
      current = "";
      lcd.clear();
      lcd.setCursor(1, 0);
      lcd.print(memory);
      lcd.setCursor(0, 1);
      lcd.print(operation);
      return;

  }

  if ('.' == key && current.indexOf('.') >= 0) 
  {
    return;
  }

  if ('.' != key && current == "0") 
  {
    current = String(key);
  } else if (key) {
    current += String(key);
  }

  lcd.print(key);
   if ('C' == key)
 {
   	operation = 0;
	memory = "";
	current = "";
   	lcd.clear();
 }
}

void loop() {
  updateCursor();

  char key = keypad.getKey();
  if (key) {
    processInput(key);
  	}
}

The keypad will probably work quite a bit better. I suggest to skip the "bare wire" stage of debugging.

Follow the logic of the following sketch for the implementation of a Basic Calculator. After that try to minimize the duplication of similar codes. The numbers are entered from Keypad and the results appear on the Serial Monitor.

#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

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