Weird behaviour with lcd and a function

Hi I have a little project which I need to request a number the user enters it on the keypad and a function to convert the characters to an integer. (getKeypadIntegerMulti() ) This function works good except it can only go to 32760 but thats probably a variable type. that I can figure out later but the issue I am having is if I use the LCD with that funtion the function stops work when you press the 4 key.I have no idea why this is happening. I will upload my code so maybe someone can see what I am doing wrong or something is not compatable.

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

char skey;
const byte ROWS = 4;
const byte COLS = 3;
char numTurns;
char keys[ROWS][COLS] = {


byte rowPins[ROWS] = {2,3,4,5};
byte colPins[COLS] = {6,7,8};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

// Set the LCD address to 0x27 for a 16 chars and 2 line display

LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup()
	// initialize the LCD

	// Turn on the blacklight and print a message.
  lcd.print("Welcome to Pierre's");
  lcd.print("Master Wind");
  lcd.print("Press any key");

//********************* END Setup ****************

int getKeypadIntegerMulti()
  int value = 0;         // the number accumulator
  int keyvalue;        // the key pressed at current moment
  int isnum;
  Serial.println("Enter the digits,press any non-digit to end ");
  Serial.print("You have typed: ");
    keyvalue = keypad.getKey();                          // input the key
    isnum = (keyvalue >= '0' && keyvalue <= '9');         // is it a digit?
    if (isnum)
      Serial.print(keyvalue - '0');
      value = value * 10 + keyvalue - '0';    // accumulate the input number
      //Serial.println("Lets accumulate the number");

  } while (isnum || !keyvalue);      // until not a digit or while no key pressed
  Serial.println(" "); 
  Serial.print("Returning from funtion: "); 
  return value;

}// END getKeypadInteger

void setWaitforKey(){
skey = keypad.waitForKey();

void loop()

int val= getKeypadIntegerMulti(); 
   Serial.println("Value is");


Which function ?

If I run the function without the LCD it works fine. I cant figure out where the number 4 comes in to play. If I press 4 st start the input it hangs in the function, if I do any number and as soon as I hit 4 it hangs in the funtion.


If I dont initialize the lcd, it works as it should, no issues.

What effect does pressing the key marked '*' or '#' have? The same as pressing 4 ?
Try also replacing the '4' in the matrix with a ' ' (quoted space) and see if you have the same problem with the '4' key.

How is the LCD wired to the Arduino ?

Does it display anything ?

Can you post a circuit diagram please?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

The lcd is a 20X4 lcd with i2C connection SDA and SDC to the arduino.
The LCD works fine.IT will display whatever I send it no problem.

The * and # is like hitting enter. You type your number on the keypad and press either * or # and goes back to the main loop and displays the integer. If you press * or # without entering a number on the keypad, it returns 0 in the main loop

Then I suspect that the keypad wiring is at fault

It works perfectly fine without initializing the LCD. Pretty sure the wiring is ok.

OK, it seems to be cetain numbers , using 4 does it everytime, I just randomly found 2235 will lock it up also.

It is so bizarre I cant figure it out. I dont understand the correlation between the lcd and this function.

Where did you get the LCD library from ?
There are many with that name

from a github, dont remember which one.
I didnt realize there was more than one of these libraries. Is there an official one?

This is the one I used.

I tried a completely different library. LCD works but it does the same thing. So it is either the wiring keypad or the function. I am convinced the wiring is ok cause all the keys are working as inputs. Only when I call the function it has this issue. I dont see an issue with the function but I will try and change the function or rewrite it I guess.

MAy I ask if anyone has an uno or leanardo an IC2 lcd and keypad if they can try this sketch and see if it does it on your side.


Can you please post a circuit diagram?
A hand drawn circuit will be fine, please label pins and include your power supply.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

In addition to a circuit diagram, a photo of your wiring may be helpful.

Power supply is usb connected to computer.