I type 99999 in keypad, and it shows 4294936223 on my serial computer

here is the code

#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <LCD.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7);

int kdelay = 50;
int period = 0;

const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys[COLS][ROWS] =
{
{ '1','4','7','*' },
{ '2','5','8','0' },
{ '3','6','9','#' },
{ 'A','B','C','D' }
};
byte rowPins[ROWS] = { 5, 4, 3, 2 };
byte colPins[COLS] = { 9, 8, 7, 6 };
Keypad kpd = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

int v1 = 0;
unsigned long v2 = 0;

void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
lcd.setBacklightPin(3, POSITIVE);
lcd.setBacklight(HIGH);

}

void loop() {
v2 = GetNumber();
Serial.println(v2);
}
int GetNumber()
{
unsigned long num = 0;
char key = kpd.getKey();
while(key != '#')
{
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');
        
        //Serial.println(num);
        break;

     case '*':
        num = 0;
        lcd.clear();
        break;
  }
  key = kpd.getKey();      

}
lcd.clear();
return num;

}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

thanks a lot

Hello

Change the return type of your function GetNumber, from int to unsigned long

thanks, its working now

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.