#include <Keypad.h>
char* password = "123";
int passLength = strlen(password);
int position = 0;
const byte ROWS = 4;
const byte COLS = 4;
char keys[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 keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int Lock = 13;
void setup() {
// put your setup code here, to run once:
pinMode(Lock, OUTPUT);
LockedPosition(true);
}
void loop() {
// put your main code here, to run repeatedly:
char key = keypad.getKey();
if (key == '*' || key == '#'){ // * or # buttons will reset the relay
position = 0;
LockedPosition(true);
}
if (key == password[position]){
// if correct button pressed - check the next password digit
position ++;
}
if (position == passLength){ // successful password, activate the relay
LockedPosition(false);
}
delay(100);
}
void LockedPosition(int locked) {
if (locked){
digitalWrite(Lock, LOW);
digitalWrite(12, LOW);
} else {
digitalWrite(Lock, HIGH);
digitalWrite(12, HIGH);
}
}