#include <Keypad.h>
String ram[4];
int counter=0;
int counter2=0;
int phase=0;
bool stat=false;
String passcode="0000";
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 5, 4, 3, 2 };
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 };
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
pinMode(10, INPUT);
}
void loop() {
char key = keypad.getKey();
if(stat){
if (counter2==0){
counter=0;
counter2=counter2+1;
}
if (key != NO_KEY && counter<5) {
ram[counter-1]=key;
Serial.print(key);
counter=counter+1;
}else if (counter==0){
Serial.println("Enter New Password");
counter++;
}else if (counter>4){
passcode=ram[0]+ram[1]+ram[2]+ram[3];
counter=0;
counter2=0;
phase=0;
stat=false;
Serial.println();
Serial.println("Retuening to console...");
}
}
else if(phase==1){
if (counter2==0){
counter=0;
counter2=counter2+1;
}
if (key != NO_KEY && counter<5) {
ram[counter-1]=key;
Serial.print(key);
counter=counter+1;
}else if (counter==0){
Serial.println();
Serial.println("CHANGING PASSCODE...");
Serial.println("Enter OLD PASSCODE");
counter++;
}else if (counter>4){
if(ram[0]+ram[1]+ram[2]+ram[3]==passcode){
Serial.println();
stat=true;
Serial.println("PASSCODE UPDATED!");
}else{
Serial.println();
Serial.println("INCORRECT");
Serial.println("Retuening to console...");
}
counter=0;
counter2=0;
phase=0;
}
}else if(digitalRead(10)==0){
phase=1;
}
else if (key != NO_KEY && phase==0 && counter<5) {
ram[counter-1]=key;
Serial.print(key);
counter=counter+1;
}else if (counter==0){
Serial.println("Enter passcode");
counter=counter+1;
}else if (counter>4){
if(ram[0]+ram[1]+ram[2]+ram[3]==passcode){
Serial.println();
Serial.println("VERIFIED");
}else{
Serial.println();
Serial.println("INCORRECT");
}
counter=0;
}
}