# Simple calculator problem [closed]

So I am trying to create a calculator program in arduino using this codes.

void loop()
{
if (keypressed != NO_KEY)
{
//Serial.print(keypressed - 48);
Serial.print(keypressed);

if(keypressed > 47 && keypressed < 58)
{
if(!mySwitch)
{
num1 = (num1*10) + (keypressed - 48);
}else{
num2 = (num2*10) + (keypressed - 48);
}
}

if(keypressed == 61)
{
num1 = 0;
num2 = 0;
mySwitch = false;
}else if (keypressed == 43)
{
mySwitch = true;
}
}
}


This code is for adding the numbers. I do a nested loop

if(keypressed == 61)
{
if(keypressed == 43){
num1 = 0;
num2 = 0;
mySwitch = false;
}else if (keypressed == 43){
mySwitch = true;
}

if(keypressed == 45){
num1 = 0;
num2 = 0;
mySwitch = false;
}else if (keypressed == 45){
mySwitch = true;
}
}


it will only accepts the value and not compute it after pressing the "="sign btw "61", "43", "45" is the ASCII value of "=", "+", "-"

## closed as unclear what you're asking by Phil Frost, Daniel Grillo, Stephen Collings, Chetan Bhargava, Nick Alexeev♦Jan 14 '14 at 22:00

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Could you properly indent your code, please, and tell us what the various values of keypressed mean. – Joe Hass Jan 14 '14 at 17:06
• And maybe generally reduce your problem. You will probably find your mistake in doing so. – Phil Frost Jan 14 '14 at 17:07
• They're ASCII character values; e.g., '0' through '9' and 'A'; – Dave Tweed Jan 14 '14 at 17:13

Assuming I understood what you are trying to do (subtraction when '-' is pressed and addition when '+' is pressed), use the following:

#include <Keypad.h>

const byte numRows = 4; //number of rows on the keypad
const byte numCols = 4; //number of columns on the keypad

//keymap defines the key pressed according to the row and columns just as appears on        the keypad
char keymap[numRows][numCols] =
{
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', 'C'},
{' ', '0', '=', 'D'}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {11, 10, 9, 8}; //Rows 0 to 3
byte colPins[numCols] = {7, 6, 5, 4}; //Columns 0 to 3

//initializes an instance of the Keypad class

boolean mySwitch = false;
boolean do_subtraction_flag = false;  // when true we will apply subtraction

void setup()
{
Serial.begin(9600);
num1 = 0;
num2 = 0;
}

//If key is pressed, this key is stored in 'keypressed' variable
//If key is not equal to 'NO_KEY', then this key is printed out
//if count=17, then count is reset back to 0 (this means no key is pressed during the   whole keypad scan process

void loop()
{

if(keypressed != NO_KEY)
{

//Serial.print(keypressed - 48);
Serial.print(keypressed);

if(keypressed > 47 && keypressed < 58)  // is between '0' and '9'
{
if(!mySwitch)
{
num1 = (num1 * 10) + (keypressed - 48);
}
else
{
num2 = (num2 * 10) + (keypressed - 48);
}
}

if(keypressed == '=')
{
if(do_subtraction_flag)  // we want to subtract the numbers
{
}
else  // we want to add the numbers
{
}

num1 = 0;
num2 = 0;
mySwitch = false;
do_subtraction_flag = false;
}
else if(keypressed == '+')
{
mySwitch = true;
}
else if(keypressed == '-')
{
mySwitch = true;
do_subtraction_flag = true;
}
}
}


I have added a flag Boolean variable named do_subtraction_flag
The flag is set depending on the button used, false for addition and true for subtraction.
Its value is used when '=' is pressed to conditionally execute the code that either subtracts num2 from num1 or adds them.

Note that I have replaced keypressed == 61 with keypressed == '='
and keypressed == 43 with keypressed == '+'
It is much easier to read the code this way.

• you got what im trying to do , but when i tried you code theres an error expected primary-expression before 'if' in if(keypressed > 47 && keypressed < 58) // is between '0' and '9' – NewInEverything Jan 14 '14 at 17:32
• @NewInEverything Yes, sorry there were two ++ characters inserted when I tried to zoom my editor (in the line just before the error), they should be removed. – alexan_e Jan 14 '14 at 17:34
• your right. How about clearing the screen. I search over the net and found Serial.print(27,BYTE); //Print "esc" Serial.print("[2J"); but some said it doesnt work anymore. and also deleting a single character. – NewInEverything Jan 14 '14 at 17:40
• @NewInEverything Try the solutions provided Clearing the terminal screen? – alexan_e Jan 14 '14 at 17:50
• I tried that. but I am using a 4x20 display to show the output. even I tried that the display will only go at the bottom most line of my display. – NewInEverything Jan 14 '14 at 18:03