Bool value in not changing

hi everyone
first of all i need to say im new in programming and my english skills are not good
im working on calculator with 4X4 key pad and 1602 I2C lcd
in my code user will Enter the first value and then enter Operator(+,-,*,/)
When user enterd Operator "Operator_Check" will turn into true
and when "Operator_Check" is true input will save in second number
and when user send "=" "Operator_Check" must be false and calculation is done
after this next input must be saved in first number
my problem is when user press "=" "Operator_Check" is not turning into false
and user numbers are saving in second number
i should mention this program work perfectly first time
thank you so much

#include<Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte ROWS_COUNT = 4;
const byte COLUMNS_COUNT = 4;
char Keys_map [ROWS_COUNT][COLUMNS_COUNT] = {
  {'1', '2', '3', '+'},
  {'4', '5', '6', '-'},
  {'7', '8', '9', '*'},
  {'.', '0', '=', '/'}
};
byte ROWS[ROWS_COUNT] = {2, 3, 4, 5};
byte COLUMNS[COLUMNS_COUNT] = {6, 7, 8, 9};
char inputKey = '&';
String Store_number1 = "";
String Store_number2 = "";
long number1 = 0;
long number2 = 0;
long Result = 0;
char Operator = '&';
bool Check = false;
bool Operator_Check = false;
int del = 300;

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("insert NUM1&NUM2");
}

void loop() {
  Do_Calculation();
  Serial.println(Operator_Check);
}

void pinmode() {
  for (int i = 0; i < 4; i++) {
    pinMode(ROWS[i], OUTPUT);
    digitalWrite(ROWS[i], LOW);
    pinMode(COLUMNS[i], INPUT_PULLUP);
  }
}

char KeyCheck () {
  char pressed_Key = '&';
  for (int j = 0; j < 4; j++) {
    if (digitalRead(COLUMNS[j]) == 0) {
      pinMode(COLUMNS[j], OUTPUT);
      digitalWrite(COLUMNS[j], LOW);
      for (int i = 0; i < 4; i++) {
        pinMode(ROWS[i], INPUT_PULLUP);
        if (digitalRead(ROWS[i]) == 0) {
          delay(del);
          pressed_Key = Keys_map[i][j];
          Check = true;
          return pressed_Key;
        }
      }
    }
  }
}

void Do_Calculation() {
  pinmode();
  inputKey = KeyCheck();
  Operator_sign ();
  ShowResult();

  if (inputKey >= '0' && inputKey <= '9' && Check == true ) {
    if (Operator_Check == false) {
      Store_number1 += inputKey;
      Serial.println(Store_number1);
      number1 = Store_number1.toInt();
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("number1:");
      lcd.setCursor(8, 0);
      lcd.print(number1);
      inputKey = '&';
      Check = false;
    }
    else if (Operator_Check == true) {
      Store_number2 += inputKey;
      Serial.println(Store_number2);
      number2 = Store_number2.toInt();
      lcd.setCursor(0, 1);
      lcd.print("number2:");
      lcd.setCursor(8, 1);
      lcd.print(number2);
      inputKey = '&';
      Check = false;
    }
  }
}

void  Operator_sign () {
  if ((inputKey == '+'  || inputKey == '-'  || inputKey == '*'  || inputKey == '/') && Check == true ) {
    Operator = inputKey;
    Operator_Check = true;
    inputKey = '&';
    Check = false;
  }
}

void ShowResult() {
  if (inputKey == '=' && Check == true) {
    Operator_Check == false;
    inputKey = '&';
    Check = false;
    
    number1 = Store_number1.toInt();
    number2 = Store_number2.toInt();

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(number1);
    lcd.setCursor(8, 0);
    lcd.print(number2);

    switch (Operator) {
      case'+':
        Result = number1 + number2;
        break;
      case'-':
        Result = number1 - number2;
        break;
      case'*':
        Result = number1 * number2;
        break;
      case'/':
        Result = number1 / number2;
        break;
    }
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(number1);
    lcd.setCursor(Store_number1.length() + 1, 0);
    lcd.print(Operator);
    lcd.setCursor(Store_number1.length() + 3, 0);
    lcd.print(number2);
    lcd.setCursor(0, 1);
    lcd.print("=");
    lcd.setCursor(2, 1);
    lcd.print(Result);
    Store_number1 = "";
    Store_number2 = "";
  }
}

Look carefully at the return conditions in this function

i've tried without return but it doesn't work either

wrong --- >> "Operator_Check == false;"
right --- >> "Operator_Check = false;"

RV mineirin

1 Like

Thank you so much I can't understand how I made this terrible mistake

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