Code not working right

I am a newbie so sorry if this is easy or my thing is unnecessarily long. I have made a simple calculator but whenever I try to make it print the answer, it is all weird and messed up.

#include <LiquidCrystal.h>
#include <Keypad.h>
int opt1 = '+';
int opt2 = '+';
int find = 0;
int answer = 0;
int answerprint = 0;

char num[3];

int clear = 0;
int currentlength = 0;
int optlength = 0;
byte divide [8]={
  0b00100,
  0b00100,
  0b00000,
  0b11111,
  0b11111,
  0b00000,
  0b00100,
  0b00100,
};
const byte ROWS = 4;
const byte COLS = 4;

char Keys[ROWS][COLS] = {
  {'1', '2', '3', '+'},
  {'4', '5', '6', '-'},
  {'7', '8', '9', 'x'},
  {'*', '0', '=', 1}
};

byte rowPins[ROWS] = {5, A5, A4, A2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A1, A0, A3, A6};

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


LiquidCrystal lcd(7,9,10,11,12,13); // the pins we use on the LCD
int P = 8;


void setup() {
digitalWrite(6, HIGH);
  pinMode(6, OUTPUT);
lcd.createChar(1, divide);
lcd.begin(16, 2);
lcd.print("Type an equation");
lcd.setCursor(0,1);
lcd.cursor();
  while(find==0){
  
  while((optlength == 0)&&(currentlength == 0)&&(find == 0)){
  char key = keypad.getKey();
  
    key == NO_KEY;
    
    if (key != NO_KEY){
      if ((key != '+')&&(key != '-')&&(key != 'x')&&(key != 1)&&(key != '=')){
      
      if(key == '*'){
        digitalWrite(6, LOW);
        
      }
       
      if (key != '*'){
      lcd.print(key);
      num[0] = key;
      currentlength++;    
      }

  }
    }
    }
  while((currentlength == 1)&&(optlength == 0)&&(find == 0)){
  char optkey = keypad.getKey();
  optkey == NO_KEY;
   if ((optkey != NO_KEY)&&(optlength == 0)&&(currentlength == 1)){
     if ((optkey == '+')||(optkey == '-')||(optkey == 'x')||
     (optkey == 1)||(optkey == '=')||(optkey == '*')){
       
       if(optkey == '*'){
       digitalWrite(6, LOW);
       }
       if(optkey == '='){
        lcd.print(optkey); 
        
        find++;
       }
       
       if((optkey != '=')&&(optkey != '*')){
       lcd.print(optkey);
       opt1 = optkey;
       optlength++; 
       }
     }
     }
  }
  
while((currentlength==1)&&(optlength==1)&&(find == 0)){
  char key2 = keypad.getKey();
  
    key2 == NO_KEY;
    
    if ((key2 != NO_KEY)&&(currentlength == 1)){
      if ((key2 != '+')&&(key2 != '-')&&(key2 != 'x')&&(key2 != 1)&&(key2 != '='))
      { 
        if (key2 == '*'){
          digitalWrite(6, LOW);
        }
      if (key2 != '*'){
      lcd.print(key2);
      num[1] = key2;
      currentlength++;    
      }

  }
    }
    }
while((currentlength == 2)&&(optlength == 1)&&(find == 0)){
  char optkey2 = keypad.getKey();
  optkey2 == NO_KEY;
   if ((optkey2 != NO_KEY)&&(optlength == 1)&&(currentlength == 2)){
     if ((optkey2 == '+')||(optkey2 == '-')||(optkey2 == 'x')||(optkey2 == 1)||
     (optkey2 == '*')||(optkey2 == '=')){
    if(optkey2 == '*'){
       digitalWrite(6, LOW);
       }
       if(optkey2 == '='){
        lcd.print(optkey2); 
        find++;
       }
       
       if((optkey2 != '=')&&(optkey2 != '*')){
       lcd.print(optkey2);
       opt2 = optkey2;
       optlength++; 
       }
     }
     }
  }
    while((currentlength==2)&&(optlength==2)&&(find == 0)){
  char key3 = keypad.getKey();
  
    key3 == NO_KEY;
    
    if ((key3 != NO_KEY)&&(currentlength == 2)){
      if ((key3 != '+')&&(key3 != '-')&&(key3 != 'x')&&(key3 != 1)&&(key3 != '=')){ 
      if (key3 == '*'){
      digitalWrite(6, LOW);
      }
      if (key3 != '*'){
      lcd.print(key3);
      num[2] = key3;
      currentlength++;    
      }

  }
    }
    }
    
    while((currentlength==3)&&(optlength==2)&&(find == 0)){
      char findkey = keypad.getKey();
  
    findkey == NO_KEY;
    
    if(findkey != NO_KEY){
      if((findkey == '=')||(findkey == '*')){
        if (findkey == '*'){
          digitalWrite(6, LOW);
        }
        if(findkey == '='){
          lcd.print("=");
          find++;
        }
      }
    }
    }
      
    }
}


void loop(){
 if(answerprint == 0){
   
   if(opt1 == '+'){
     answer = num[0] + num[1];
   }
      if(opt1 == '-'){
     answer = num[0] - num[1];
   }
      if(opt1 == 'x'){
     answer = num[0] * num[1];
   }
      if(opt1 == 1){
     answer = num[0] / num[1];
   }
   
   if (opt2 == '+'){
     answer = answer + num[2];
     lcd.print(answer);
     answerprint++;
   }
   if (opt2 == '-'){
     answer = answer - num[2];
     lcd.print(answer);
     answerprint++;
   }
   if (opt2 == 'x'){
     answer = answer * num[2];
     lcd.print(answer);
     answerprint++;
   }
   if (opt2 == 1){
     answer = answer / num[2];
     lcd.print(answer);
     answerprint++;
   }
 }


while(answerprint == 1){
  char endkey = keypad.getKey();
  
    endkey == NO_KEY;
    
    if(endkey != NO_KEY){
      if (endkey == '*'){
        digitalWrite(6, LOW);
      }
      
}
}
}

If anyone has any idea as to why this occurs, help is very much appreciated!

I haven't followed the whole flow but this does not look right

 if ((optkey == '+')||(optkey == '-')||(optkey == 'x')||
     (optkey == 1)||(optkey == '=')||(optkey == '*')){

I would expect

 if ((optkey == '+')||(optkey == '-')||(optkey == 'x')||
     (optkey == '1')||(optkey == '=')||(optkey == '*')){

i.e. '1' rather than just 1

Luke21206:
it is all weird and messed up.

What does that mean?

Can you post some examples?

...R

I’d also suggest embedding some Serial.print() debug statements in your code so you can see how far you get reading two clean operands from the keypad and identify the operator.
In other words, if you are trying to add 123 and 456 together, do you successfully get 123 in a variable, 456 in another variable and also see the “+” sign ?

I haven't followed the whole flow but this does not look right
Code: [Select]

if ((optkey == '+')||(optkey == '-')||(optkey == 'x')||
(optkey == 1)||(optkey == '=')||(optkey == '*')){

I would expect
Code: [Select]

if ((optkey == '+')||(optkey == '-')||(optkey == 'x')||
(optkey == '1')||(optkey == '=')||(optkey == '*')){

I use 1 without the '' because the custom character at the beginning takes the variable 1.

By weird and messed up, I mean the answer is totally messed up. Here are some examples of what show up:
1+2+3=150
5x6=2862
9-1-1= -41
1+2÷9 =1

the answer for division always seems to be 1, and basic (blank - blank) subtraction works.
I am still experimenting though, so any answers or questions would be appreciated.

I've been doing a little more experimenting, and I think I know the problem, but not whats causing it.
basically, every time you push a number, the number is increased by 48. so the reason 4x5=2756, is 4+48 is 52, and 5+48 is 53, and 52x53=2756. I have no idea where 48 comes from, but if you have any idea, please respond.

'0' == 0x30 == 48

'0' == 0x30 == 48

Please explain more and show in the code where that happens :slight_smile:

The ASCII character '0' is represented by the value 48.

A key on the keypad generates an ascii character. The value of the character ‘0’ is 48.

So if you press 4, you get character ‘4’ (value 52) and need to subtract 48 to get the number 4. Same for e.g. pressing 5. Next you can multiply and print.

Note, I did not look at your code, just explaining where 48 comes from…