Variable larger than four digits is returned as 10 digit jumble

Hi guys,

I have been working on a project with an Arduino Mega 2560, 4x4 keypad, and LCD display. In the program I would like the user to be able to enter a 5-7 digit number using the keypad, press "E" and have the value returned to the main void loop and displayed on the LCD.

//Libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

//4x4 Keypad setup 
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
byte rowPins[ROWS] = {26, 27, 28, 29};
byte colPins[COLS] = {22, 23, 24, 25};
byte data_count = 0, master_count = 0;
bool Pass_is_good;

char hexaKeys[ROWS][COLS] = 
{
  {'1','2','3','M'},
  {'4','5','6','C'},
  {'7','8','9','P'},
  {'<','0','E','S'}
};
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

//LCD initiate
LiquidCrystal_I2C lcd(0x27,16,2);

//internal program variables
int Display = 11;
int prevDisplay = 0;
int gettingNum = false;
unsigned long timeOut = 1000;
long int timeIn = 1000;

void setup(){
  Serial.begin(9600); //this creates the Serial Monitor
  Wire.begin(); //this creates a Wire object
  lcd.begin(16,2);
  lcd.backlight();
}

//Enter number then press enter
int GetNumber(){
//only works for 4 or less digits
  Serial.println("In GetNumber Func");
  if (gettingNum == true){
    long num = 0;
    char key = customKeypad.getKey();
    lcd.setCursor(8, 1);
    while(key != 'E'){

        switch (key){

          case NO_KEY:
              break;

          case '0': case '1': case '2': case '3': case '4':
          case '5': case '6': case '7': case '8': case '9':
              lcd.print(key);
              num = num * 10 + (key - '0');
              break;

          case 'C':
              num = 0;
              lcd.clear();
              break;
        }
        key = customKeypad.getKey();
    }
    //Assigns correct num but returns jumbled num
    Serial.println(num);
    Serial.println(num/2);
    return num;
  }
}

void loop(){
  //Set Up Menu
  char customKey = customKeypad.getKey();
  switch(Display){
      case 11:{
        prevDisplay = 10;
        lcd.setCursor(0, 0);
        lcd.print("Enter Millis");
        //Get millis from buttons, turn to int, set as time out
        //May need to set inputNumber to 4 and send to GetNumber();
        gettingNum = true;
        timeOut = GetNumber();
        Serial.println(timeOut);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Extension Time:");
        lcd.setCursor(0, 1);
        lcd.print(timeOut);
        gettingNum = false;
        delay(800);
        Display = 20;
        prevDisplay = 10;
        break;
      }
  }
}

When I run my code, it works great up to 4 digits of user input. For a number larger than 4 digits, the GetNumber() function is interpreting the user input and assigning it into the num variable correctly, but when it is returned to the main void loop, assigned to variable timeOut(), and printed to the LCD or serial print, it becomes a random 10 digit number. I have been trying to use unsigned long, but its still not working.

Thank you for the help!

If GetNumber() is to return large numbers, it needs to return a long.

a7

Take a look at Keypad data entry. A beginners guide

I origionally have num defined as long. I changed the return line to:
return long(num);

and the line where it is called to:
timeOut = long(GetNumber());

Nothing has changed in the output. Is this the incorrect way to return a long?

I did not find anything in here about using numbers longer than 4 digits

No. You want the return value promised by the function to be of type long. Change the definition of the function:

long GetNumber() {
   ...


  ...
   return num;
}

Your fix made the return value a long, and then when the function actually returned, it kept its promise and munged it somehow to fit into an int. And handed you crap.

Probably. :expressionless:

a7

correct way to return a long, yes, but the function declaration must also be long, not int.

LOL. I would say it pretty much is not the correct way to return a long.

The cast was a good stab, but maybe not even as num is already of type long.

a7

whups, yep, caught me napping!

It worked! Thank you!

There are examples showing how to read digits from the keypad and add them to a total. If the total variable declared as the appropriate data type then the only limit to how many digits that can be entered is the size of the data type