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");
}