I tried to add a feature where u can change the password but after i enter the keys to change password, it only allows me to press 2 keys and the screen will display back the "Type Password".
#include <Wire.h> // deals with I2C connections
#include <LiquidCrystal_I2C.h> // activates the LCD I2C library
#include <Keypad.h> // activates the Keypad library
#define Password_Length 9 // Amount of digits plus one null character (8 + 1 = 9) defines password length
char userInput[Password_Length]; // This variable will store the user input in a string
char newuserInput[Password_Length];
char Master[Password_Length] = "12345678";
char Change[Password_Length] = "99991234";
char customKey; // This variable holds key input of every key pressed
byte pressCount = 0;
const byte ROWS = 4; // Constants for row and
const byte COLS = 4; // column size of your keypad.
char hexaKeys [ROWS][COLS]={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins [ROWS]={9,8,7,6};
byte colPins [COLS]={5,4,3,2};
Keypad customKeypad = Keypad (makeKeymap(hexaKeys),rowPins,colPins,ROWS,COLS);
LiquidCrystal_I2C lcd (0x27,16,2);
void setup() {
lcd.backlight();
lcd.init();
lcd.clear();
}
void loop() {
do {
lcd.setCursor(0,0);
lcd.print("Type Password:"); // One space is offset to center the text on the LCD
customKey = customKeypad.waitForKey();
if (customKey != NO_KEY && customKey != 'C' && customKey != 'D') // If the user presses the number keys, the data is entered
{
userInput[pressCount] = customKey;
lcd.setCursor(pressCount + 2, 1);
lcd.print("*");
pressCount++; // Key press count will roll over until 8 digits are entered
}
else if (customKey == 'C') // C is to clear the entire password input from any point and start over again as
{ // it calls up the clearData function.
lcd.clear();
clearData();
}
else if (customKey == 'D') // D = enter,If less than 8 digits are entered, the input will be invalid and
{ // an error message will flash, then call the clearData function to
lcd.clear(); // start the entering process all over again.
lcd.setCursor(0,0);
lcd.print("INVALID ENTRY");
delay(800);
clearData();
}
if (pressCount == 8) // Once 8 digits are entered, the function to check the password string is called up and
{ // both the character and string data are sent to the function.
lcd.clear();
waitHere();
}}
while (!strcmp(userInput,Master)&&strcmp(userInput,Change));
if (!strcmp(userInput,Change)){
lcd.setCursor(0,0);
lcd.print("Enter old password:"); // One space is offset to center the text on the LCD
customKey = customKeypad.waitForKey();
if (customKey != NO_KEY && customKey != 'C' && customKey != 'D') // If the user presses the number keys, the data is entered
{
newuserInput[pressCount] = customKey;
lcd.setCursor(pressCount + 2, 1);
lcd.print(" ");
pressCount++; // Key press count will roll over until 8 digits are entered
}
else if (customKey == 'C') // C is to clear the entire password input from any point and start over again as
{ // it calls up the clearData function.
lcd.clear();
clearData();
}
else if (customKey == 'D') // D = enter,If less than 8 digits are entered, the input will be invalid and
{ // an error message will flash, then call the clearData function to
lcd.clear(); // start the entering process all over again.
lcd.setCursor(0,0);
lcd.print("INVALID ENTRY");
delay(800);
clearData();
}
if (pressCount == 8) // Once 8 digits are entered, the function to check the password string is called up and
{ // both the character and string data are sent to the function.
lcd.clear();
changepassword();
}
}
else if (!strcmp(newuserInput,Master)){
lcd.setCursor(0,0);
lcd.print("Enter new password:"); // One space is offset to center the text on the LCD
customKey = customKeypad.waitForKey();
if (customKey != NO_KEY && customKey != 'C' && customKey != 'D') // If the user presses the number keys, the data is entered
{
userInput[pressCount] = customKey;
lcd.setCursor(pressCount + 2, 1);
lcd.print(" ");
pressCount++;
}
// Key press count will roll over until 8 digits are entered
else if (customKey == 'C') // C is to clear the entire password input from any point and start over again as
{ // it calls up the clearData function.
lcd.clear();
clearData();
}
else if (customKey == 'D') // D = enter,If less than 8 digits are entered, the input will be invalid and
{ // an error message will flash, then call the clearData function to
lcd.clear(); // start the entering process all over again.
lcd.setCursor(0,0);
lcd.print("INVALID ENTRY");
delay(800);
clearData();
}
if (pressCount == 8) // Once 8 digits are entered, the function to check the password string is called up and
{ // both the character and string data are sent to the function.
lcd.clear();
newpass();
}
}
}
void waitHere(){
lcd.setCursor(0,0);
lcd.print("Type Password:");
lcd.setCursor(0,1);
lcd.print(" ********");
customKey = customKeypad.waitForKey(); // Program will halt here until a key is pushed
if (customKey != NO_KEY && customKey == 'D') // The ENTER button is pushed and the password goes through the routine.
{
lcd.clear();
lcd.setCursor(0,0);
if (!strcmp(userInput, Master)) // The user input string is matched up with the Master password string and matches up
{ // running the routine to grant access before returning back to the beginning.
lcd.setCursor(0,0);
lcd.print("ACCESS GRANTED.");
delay(5000);
clearData();
}
else if (strcmp(userInput, Master)) // The user input string is matched up with the Master password string and does not match up
{
if (!strcmp(userInput,Change))
{
lcd.setCursor(0,0);
lcd.print("CHANGE PASSWORD");
delay(2000);
}
else
{ // running the routine to deny access before returning back to the beginning.
lcd.setCursor(0,0);
lcd.print("ACCESS DENIED.");
delay(2000);
clearData();
}
}
}
if (customKey != NO_KEY && customKey == 'C') // If the CLEAR button is pushed, the data is cleared and the program starts over.
{
lcd.clear();
clearData();
}
}
void changepassword() {
lcd.setCursor(0,0);
lcd.print("Enter old password:");
lcd.setCursor(0,1);
lcd.print(" ********");
customKey = customKeypad.waitForKey(); // Program will halt here until a key is pushed
if (customKey != NO_KEY && customKey == 'D') // The ENTER button is pushed and the password goes through the routine.
{
lcd.clear();
lcd.setCursor(0,0);
if (!strcmp(newuserInput, Master)) // The user input string is matched up with the Master password string and matches up
{ // running the routine to grant access before returning back to the beginning.
lcd.setCursor(0,0);
lcd.print("Password Reset.");
delay(2000);
}
}
}
void newpass(){
lcd.setCursor(0,0);
lcd.print("Enter new password:");
lcd.setCursor(0,1);
lcd.print(" ********");
customKey = customKeypad.waitForKey(); // Program will halt here until a key is pushed
if (customKey != NO_KEY && customKey == 'D') // The ENTER button is pushed and the password goes through the routine.
{
userInput[pressCount] = Master[pressCount];
}
}
void clearData() {
while (pressCount != 0)
{
userInput[pressCount--] = 0; // Clears out the user input data, both digit and string data are reset to zero.
}
setup(); // Returns program back to the beginning
}