Simple code tweak help needed

Hello everyone :slight_smile:
This program is simply a calculator that I have developed< THIS CODE IS NOT MINE>.
I am trying to edit this code so that when I for example add 1+2
the display will show 3 right? Cool, here is what I wanna do. I want the calculator
to be able to lets say multiply the output(3) *2 and it should show 6.
With this code I will have to reset the calculator every time I want to use the last output in a new
equation.

#include <LiquidCrystal.h> //import lcd library
#include <Keypad.h> //import keypad library

LiquidCrystal lcd(5, 4, 3, 2, 1, 0); //lcd pins
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns

//define the keymap
char keys [ROWS] [COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'X', '0', '=', '/'}
};
byte rowPins[ROWS] = {
9 ,8 ,7 ,6}; //connect keypad ROW1, ROW2, ROW3, ROW4 to these arduino pins
byte colPins[COLS] = {
13, 12, 11, 10}; //connect keypad COL1, COL2, COL3, COL4 to these arduino pins

//create the keypad
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

//variables declaration
boolean valOnePresent = false;
boolean next = false;
boolean final = false;
String num1, num2;
int ans;
char op;

void setup(){
lcd.begin(16,2);
lcd.setCursor(2,0);
lcd.print("Calculator by:!");
lcd.setCursor(0,1);
lcd.print("Hadi M. Shabara");
delay(2500);
lcd.clear(); //clears the LCD screen and positions the cursor in the upper-left corner.
}

void loop(){
char key = myKeypad.getKey();

if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0')){
if (valOnePresent != true){
num1 = num1 + key;
int numLength = num1.length();
lcd.setCursor(15 - numLength, 0); //to adjust one whitespace for operator
lcd.print(num1);
}
else {
num2 = num2 + key;
int numLength = num2.length();
lcd.setCursor(15 - numLength, 1);
lcd.print(num2);
final = true;
}
}

else if (valOnePresent == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+')){
if (valOnePresent == false){
valOnePresent = true;
op = key;
lcd.setCursor(15,0); //operator on right corner
lcd.print(op);
}
}

else if (final == true && key != NO_KEY && key == '='){
if (op == '+'){
ans = num1.toInt() + num2.toInt();
}
else if (op == '-'){
ans = num1.toInt() - num2.toInt();
}
else if (op == '*'){
ans = num1.toInt() * num2.toInt();
}
else if (op == '/'){
ans = num1.toInt() / num2.toInt();
}
lcd.clear();
lcd.setCursor(15,0);
lcd.autoscroll();
lcd.print(ans);
lcd.noAutoscroll();
}
else if (key != NO_KEY && key == 'X'){
lcd.clear();
valOnePresent = false;
final = false;
num1 = "";
num2 = "";
ans = 0;
op = ' ';
}
}

Or you could just do this:
http://forum.arduino.cc/index.php/topic,160778.0.html

That isn't even complete :smiley:

if you follow the post it winds up here with the working version: (Reply#77 + Reply#78 modification)
http://forum.arduino.cc/index.php/topic,153845.75.html

Here's a keypad that uses one pin.
http://playground.arduino.cc/Code/OneWireKeyPad#.UyozdvldWSo

Thank you for trying to help me, but I am a very basic user and I am almost lost in all these codes.

If you have no programming experience then how do you know all your code needs is a simple tweak ? If you knew enough to make that statement you would not be posting here , correct ?
Have you tried Googling "arduino keypad calculator ?

#include <LiquidCrystal.h> //import lcd library
#include <Keypad.h> //import keypad library

LiquidCrystal lcd(5, 4, 3, 2, 1, 0); //lcd pins
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns

//define the keymap
char keys [ROWS] [COLS] = {
  {'1', '2', '3', '+'},
  {'4', '5', '6', '-'},
  {'7', '8', '9', '*'},
  {'X', '0', '=', '/'}
};
byte rowPins[ROWS] = {
  9 ,8 ,7 ,6}; //connect keypad ROW1, ROW2, ROW3, ROW4 to these arduino pins
byte colPins[COLS] = {
  13, 12, 11, 10}; //connect keypad COL1, COL2, COL3, COL4 to these arduino pins

//create the keypad
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

//variables declaration
boolean valOnePresent = false;
boolean next = false;
boolean final = false;
String num1, num2;
int ans;
char op;

void setup(){
  lcd.begin(16,2);
  lcd.setCursor(2,0);
  lcd.print("Calculator by:!");
  lcd.setCursor(0,1);
  lcd.print("Hadi M. Shabara");  
  delay(2500);
  lcd.clear(); //clears the LCD screen and positions the cursor in the upper-left corner. 
}

void loop(){
  char key = myKeypad.getKey();

  if (key != NO_KEY && (key=='1'||key=='2'||key=='3'||key=='4'||key=='5'||key=='6'||key=='7'||key=='8'||key=='9'||key=='0')){
    if (valOnePresent != true){
      num1 = num1 + key;
      int numLength = num1.length();
      lcd.setCursor(15 - numLength, 0); //to adjust one whitespace for operator
      lcd.print(num1);
    }
    else {
      num2 = num2 + key;
      int numLength = num2.length();
      lcd.setCursor(15 - numLength, 1);
      lcd.print(num2);
      final = true;
    }
  }

  else if (valOnePresent == false && key != NO_KEY && (key == '/' || key == '*' || key == '-' || key == '+')){
    if (valOnePresent == false){
      valOnePresent = true;
      op = key;
      lcd.setCursor(15,0); //operator on right corner
      lcd.print(op);
    }
  }

  else if (final == true && key != NO_KEY && key == '='){
    if (op == '+'){
      ans = num1.toInt() + num2.toInt();
    }
    else if (op == '-'){
      ans = num1.toInt() - num2.toInt();
    }
    else if (op == '*'){
      ans = num1.toInt() * num2.toInt();
    }
    else if (op == '/'){
      ans = num1.toInt() / num2.toInt();
    }    
      lcd.clear();
      lcd.setCursor(15,0);
      lcd.autoscroll();
      lcd.print(ans);
      lcd.noAutoscroll();
  }
  else if (key != NO_KEY && key == 'X'){
    lcd.clear();
    valOnePresent = false;
    final = false;
    num1 = "";
    num2 = "";
    ans = 0;
    op = ' ';
  }
}

Thanks for taking the time to help, but is this code supposed to fix my issues? I tried it and it is still the same.

You were supposed to use the "#" CODE TAGS button to post your code. I reposted your code with the code tags button so others would be able to scroll through it. Please use the code tags button "#" when you post your code.

Thank you for trying to help me, but I am a very basic user and I am almost lost in all these codes.

So then why are you posting if you don't need any code ?

@raschemmel, thanks for posting the code correctly.

@insanehad, if you want people to study a long piece of code make life easy for them by following the Forum guidelines.

As I understand it you want to be able (say) to add 2 + 3 = 6 followed by (say) 6 * 3 = 18 with the calculator remembering the 6 between calculations.

From a quick glance the problem seems to be that all of the "code" is within the IF statement that checks the keypad inputs and I think, to do what you want, the code needs to separate the keypad inputs from the calculations. If I'm write that is not a trivial change, though it shouldn't be difficult.

Rather than thiink about how to modify that piece of code it will probably be easier to think about the problem from scratch and just use that code for ideas about how to read the keypad etc.

...R

I definitely had that in mind. I have been reading through the code and I think I got most of it. I will try to rewrite the code with a new idea and will most back at you :slight_smile: ! Thanks