keypad and lcd display issue

Hi, I wrote a piece of code using the serial monitor and keyboard input, now trying to modify it for keypad and lcd display. I'm using wire.h, LiquidCrystal.h, and keypad.h libraries all downloaded from here and all working with the provided examples.

/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // i2c address is 0x27 20 characters 4 line display

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','4','7','*'},
  {'2','5','8','0'},
  {'3','6','9','#'},
  {'A','B','C','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {13, 12, 11, 10}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

void setup(){
  Serial.begin(9600);
  lcd.init();
  lcd.init();
  lcd.backlight();
}
  
void loop(){
  char customKey = customKeypad.getKey();
  
  if (customKey){
    lcd.print(customKey);
//    Serial.println(customKey);
  }
}

I've made slight modifications to the example provided, letting me output to the lcd display. This code works perfectly. Input keypad values are read and then printed properly to the display. I think this rules out hardware issues.

The new code I am trying to implement is not working, I've tried several approaches with no success. I'm trying to get user input for several values to drive a CNC. This code is only slightly different than the previous working example.

/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // i2c address is 0x27 20 characters 4 line display

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','4','7','*'},
  {'2','5','8','0'},
  {'3','6','9','#'},
  {'A','B','C','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {13, 12, 11, 10}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

void setup(){
  Serial.begin(9600);
  lcd.init();
  lcd.init();
  lcd.backlight();
}
  
void loop(){
  lcd.home();
  lcd.print("enter a key");
  
  customKeypad.waitForKey();
  char customKey = customKeypad.getKey();
  lcd.print(customKey);
  lcd.setCursor(0,1);
  lcd.print("enter an axis");
  char axis = customKeypad.getKey();
  
  
  if (customKey){
    lcd.print(customKey);
   customKeypad.waitForKey();
  }
}

This last code produces garbled output to the lcd. At first I thought faulty hardware (keypad or lcd display) then I go back to the first code (above) and everything works as it should.

A few obvious questions, things like where in the code should the customKeypad.waitForKey(); be, I've tried it before and after the char customKey = customKeypad.getKey(); line. Both seem the same.

I need to allow the user to input several values to adjust a CNC router spindle along x,y and z axis.

  customKeypad.waitForKey();
  char customKey = customKeypad.getKey();

waitForKey() does what it says, it waits for a key and returns when one is detected
getKey() does what it says, it reads a key whether or not one is entered

You seem to be waiting for a key, reading it then ignoring it, but straight afterwards your read the keypad again even though you have just removed the input from the input buffer.

customKeypad.waitForKey();
char customKey = customKeypad.getKey();
lcd.print(customKey);

What I am doing here is reading the keypad, then printing it to the lcd. What I am getting is
output that is unreadable. If I change this to lcd.print('customKey'); such that it prints the
value stored in customKey it prints 25977 which is the number that microsoft OS assigns to
variables that don't contain a value. I'd post an image of how it looks on my display
but this forum doesn't allow images to be uploaded, and I have no where to link a URL from.
Is it possible that my arduino UNO is memory deficient?

What I am doing here is reading the keypad, then printing it to the lcd.

Yes, but when you read it using getKey() it has already been read and thrown away by waitForKey()

Try

char customKey = customKeypad.waitForKey();
lcd.print(customKey);

it prints 25977 which is the number that microsoft OS assigns to
variables that don't contain a value.

What has Microsoft got to do with it ?

Ok. It looks like you are typing the code in the forum, I don't know where you got the above code from, it is not in the code I posted.

Should the

customKeypad.waitForKey();

go after the after the assignment or before it?

customKeypad.waitForKey();
char customKey = customKeypad.getKey();

or

char customKey = customKeypad.getKey();
customKeypad.waitForKey();

which is the number that microsoft OS assigns to
variables that don't contain a value.

Even though what the Microsoft OS does has NOTHING to do with what the Arduino does, this statement is utter nonsense. The OS has NOTHING to do with what compilers do. Compilers do NOT assign constant values to variables except for global variables, which it does NOT initialize to 25977.

I don't know what you are smoking, but you really should cut back.

Should the

customKeypad.waitForKey();

go after the after the assignment or before it?

You don't need both

As I said previously, waitForKey() waits for ever for a key to be pressed and the program does nothing else until one is pressed. getKey() reads the keypad and if a key is pressed at the time it reads it. Whether a key is pressed or not the program moves on. If a key is not pressed then a special code (NO_KEY) is returned so that you can check it.

So, do you want the program to sit and wait for a key, if so use waitForKey(), or read they keypad and move on, if so use getKey()

You could do worse than reading and understanding Arduino Playground - HomePage

Thanks guys. Paul I'm not smoking anything, I'm learning. I'm new. Thanks Bob, I misunderstood your comment.