# Unexpected long number when performing simple math

Hi There,

Based on http://arduino.cc/forum/index.php?topic=57627.0 I’ve written a very simple sketch to take the input from a keypad and add it to the previous numbers to create one long number which I’ll then use in my program.

This is what I would expect the sketch to output:

``````Key Pressed: 1
Full Number: 1
Key Pressed: 2
Full Number: 12
Key Pressed: 3
Full Number: 123
``````

However, this is what it actually outputs:

``````Key Pressed: 1
Full Number: 2863311585
Key Pressed: 2
Full Number: 2863311586
Key Pressed: 3
Full Number: 2863311587
``````

Where are those large numbers coming from??I have a feeling it has to do with the data type, but I just can’t figure it out. Any ideas?

Thank you

``````#include <SoftwareSerial.h>

/////////////////////////////////////////////////////////////////////////////////////////////
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
/////////////////////////////////////////////////////////////////////////////////////////////

unsigned long inputNum = 0;

void setup() {
Serial.begin(9600);
}

void loop() {

if(key != '#') {
inputNum = inputNum * 10 + (key - '0'); // convert ascii to int
}

if (key != NO_KEY){
Serial.print("Key Pressed: ");
Serial.println(key);
Serial.print("Full Number: ");
Serial.println(inputNum);
}

}
``````

This occurs on every iteration of loop:

``````    inputNum = inputNum * 10 + (key - '0');
``````

Irrespective of whether you're getting NO_KEY (which I expect is -1) from the keyboard. i.e you defend against #, but not no_key.

wildbill: This occurs on every iteration of loop:

``````    inputNum = inputNum * 10 + (key - '0');
``````

Irrespective of whether you're getting NO_KEY (which I expect is -1) from the keyboard. i.e you defend against #, but not no_key.

You beauty! So simple, yet it has somehow escaped me for the past few hours!