lcdi2c keypad problems

I have an lcd from web4robot.com with a 4x3 keypad hooked up to it. The lcd and keyboard are working, the only issue I’m having is getting the timing right on the key pushes. The way I have written my code it seems I have to push the buttons just right in order to get a single character. Basically what I’m trying to do is enter a passcode of x amount of characters in a given amount of time. I pretty much have it working the only issue is the timing. Maybe my method is all wrong and someone can correct me or point me in the right direction. The code below watches for a key to be pressed, then branches off to a function that will wait roughly 7 seconds for another key to be pressed before returning to the main loop.

// keypad code

#include <Wire.h>
#include <LCDI2C.h>

LCDI2C lcd = LCDI2C(2,16,0x4C,1);             // Number of lines and i2c address of the display

void setup() { 

  lcd.init();                          // Init the display, clears the display
  lcd.setCursor(0,0);                      // Place the cursor on the 2nd line
  lcd.print("Keypad = ");
  Serial.begin(9600);
}


void loop()
{
 int keyInput;
  keyInput=lcd.keypad();
  delay(10);
  lcd.setCursor(0,10);
  lcd.print("   ");      // Clear prev entry
  lcd.setCursor(0,10);
  lcd.print(keyInput,DEC);// Print the value we got back from the keypad

  if (keyInput > 0)
    {
      int result = getPassCode(keyInput);
    }

}



int getPassCode(int keyInput)
{
  int charCount = 0;
  int inputtime = 0;
  Serial.print("start input");

  while (inputtime < 7)
  {
    if(keyInput != 0)
    {
      Serial.print("\n");
      Serial.print(keyInput);
      Serial.print("\n");      
      inputtime =0;
    }
    keyInput = lcd.keypad();
    delay(1000);
    Serial.print(inputtime);
    inputtime++;
  }

  Serial.print("\n"); 
  Serial.print("end input");
  lcd.setCursor(1,8);
  lcd.print("done");
}

I don’t understand this bit:

while (inputtime < 7)
  {
    if(keyInput != 0)
    {
      Serial.print("\n");
      Serial.print(keyInput);
      Serial.print("\n");      
      inputtime =0;
    }
    keyInput = lcd.keypad();
    delay(1000);
    Serial.print(inputtime);
    inputtime++;
  }

If you press a key it will reset inputtime to zero, and thus it will always wait 7 seconds after you press the key.

Plus here:

int getPassCode(int keyInput)

You have declared that getPassCode will return an int, but there is no “return” in it. So the result is undefined. You should have got a compiler warning but compiler warnings are suppressed .

And here:

 if (keyInput > 0)
    {
      int result = getPassCode(keyInput);
    }

You get the result from getPassCode (which itself is not defined) and then just discard it. I should point out that the keyInput passed to getPassCode will not be returned to the calling function as it is passed by value and not by reference.

I suggest you look at each line of code and put a comment on what you are hoping it will achieve. This should clarify the process. I mean, why delay for 1 second directly after getting a keypress? That immediately is going to add a 1-second sluggishness to whatever is happening. Here:

keyInput = lcd.keypad();
delay(1000);

Thanks for the reply. The idea behind the delay is to give the user time to input the entire 4 digit passcode before it automaticlly exits the routine and goes back to the main loop. Also, I haven't yet put into the function is that if 4 keys are pressed then it will automaticlly return to the main loop. I will continue to work on it, I have made a little more progress since my orginal post. Thanks again.