sterretje:
Not using the password library, you can use the below
#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
char keys[ROWS][COLS] = // Define the Keymap
{
{
'1', '2', '3', 'A'
}
,
{
'4', '5', '6', 'B'
}
,
{
'7', '8', '9', 'C'
}
,
{
'*', '0', '#', 'D'
}
};
byte rowPins[ROWS] = {50, 51, 52, 53}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {46, 47, 48, 49};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
// put your setup code here, to run once:
}
void loop()
{
// pointer to password entered by used
char *userPwd;
// read the keypad
char ch = keypad.getKey();
// if key pressed
if (ch != NO_KEY)
{
// add to password
userPwd = readPWD(ch);
// if password entry completed
if (userPwd != NULL)
{
// compare
if (strcmp(userPwd, "1234") == 0)
{
// access granted
}
else
{
// access denied
}
}
}
}
/*
read keypad for password
returns NULL while # not pressed, else (pointer to) entered password
*/
char *readPWD(char ch)
{
static char pwd[5];
static byte index = 0;
// if cancel or index 0
if (ch == '*' || index == 0)
{
// clear the password buffer
memset(pwd, 0, sizeof(pwd));
// indicate password not complete
return NULL;
}
// if 4 characters entered and # not pressed
if (index >= 4 && ch != '#')
{
// ignore; indicate password not complete
return NULL;
}
// if # pressed
if (ch == '#')
{
// add nul character
pwd[index] = '\0';
// return the entered password
return pwd;
}
// add to password and increment index
pwd[index++] = ch;
// reset index for next time password needs to be entered
// indicate password not complete
return NULL;
}
In the readPWD function, you can use index as the X position on the LCD where you want to write the '*' when the user presses a button.
Compiles but not tested
sorry but it's not working.