Keypad Library Issues

Hello. I am using the keypad library (Arduino Playground - Keypad Library) and am attempting to store data from a keypad, but I can't figure out what exactly is being stored as code1. The following is my code for a single digit of my passcode, to be repeated 4-6 times.

char code1;
while(! kpd.getKey())
  {
    delay(10);
  }

  code1 = kpd.getKey();
  Serial.print(code1);

  lcd.setCursor(8,2);
  lcd.print(code1);

Nothing prints to serial, and some glitchy character shows up on my LCD. The issue is not with my connections.

It seems to me as if the getKey function only gives the information once per keypress, and so that is "used up" during my while loop check. Maybe. Or I am missing something else entirely.

Just keep the value and you test if your assumptions are correct.

char code1;

do{
  code1 = kpd.getKey();
}while( !code1 );

Serial.print(code1);

The getKey() method returns a char.

Chars however are numbers (like int and long) and printing them will give you the numeric value. The numeric value for e.g. the character '0' is 48 decimal and 30 hexadecimal.

If you want to see the character '0' in the serial monitor, use Serial.write(code1):

Do a google for ascii table.

If you want to see the character '0' in the serial monitor, use Serial.write(code1):

Serial.print(code1); will also send the character '0', not the string "48" to the serial port, since code1 is a char variable.

OPs problem is assuming that calling getKey() again after the while loop ends will get the last key pressed. That is NOT the case.

PaulS:
OPs problem is assuming that calling getKey() again after the while loop ends will get the last key pressed. That is NOT the case.

Yes, that was my issue. The following worked.

  while(! code1)
  {
    delay(10);
    code1 = kpd.getKey();
  }

  Serial.print(code1);

  lcd.setCursor(8,2);
  lcd.print(code1);