#include <Wire.h>
#include <Keypad.h>
// Length of password + 1 for null character
#define Password_Length 8
// Character to hold password input
char Data[Password_Length];
// Password
char Master[Password_Length] = "123A456";
// Pin connected to lock relay input
int lockOutput = 13;
// Counter for character entries
byte data_count = 0;
char customKey;
// Constants for row and column sizes
const byte ROWS = 4;
const byte COLS = 4;
// Array to represent keys on keypad
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Connections to Arduino
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup() {
// Setup serial monitor
Serial.begin(9600);
}
void loop() {
// Get key value if pressed
char customKey = customKeypad.getKey();
Serial.print("Enter password");
if (customKey) {
// Print key value to serial monitor
Serial.println(customKey);
Serial.println(Data[data_count]);
data_count++;
}
if (data_count == Password_Length - 1) {
if (!strcmp(Data, Master)) {
// Password is correct
Serial.println("Correct");
// Turn on relay for 5 seconds
digitalWrite(lockOutput, HIGH);
delay(5000);
digitalWrite(lockOutput, LOW);
}
else {
// Password is incorrect
Serial.println("Incorrect");
delay(1000);
}
}
void clearData()
{ while (data_count != 0) {
Data[data_count--] = 0;
}
}
return;