#include <LedControl.h>
// Include Keypad library
#include <Keypad.h>
// Password Length
const int Password_Length = 4;
// Character to hold password input
String Data;
// Password
String Master = "0001";
// Pin connected to lock relay signal
int lockOutput = 11;
// Counter for character entries
byte data_count = 0;
// Character to hold key input
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);
int DIN = 12;
int CS = 11;
int CLK = 10;
LedControl lc=LedControl(DIN,CLK,CS,0);
void setup() {
lc.shutdown(0,false);
lc.setIntensity(0,15);
// Set lockOutput as an OUTPUT pin
pinMode(lockOutput, OUTPUT);
}
void printByte(byte character [])
{
int i = 0;
for(i=0;i<8;i++)
{
lc.setRow(0,i,character[i]);
}
}
void loop() {
// Look for keypress
customKey = customKeypad.getKey();
if (customKey) {
// Enter keypress into array and increment counter
Data += customKey;
data_count++;
}
// See if we have reached the password length
if (data_count == Password_Length) {
if (Data == Master) {
// Correct Password
byte SMILE[8]= {0x3C,0x42,0xA5,0x81,0xA5,0x99,0x42,0x3C};
printByte(SMILE);
// Turn on relay for 5 seconds
digitalWrite(lockOutput, HIGH);
delay(5000);
digitalWrite(lockOutput, LOW);
}
else {
// Incorrect Password
byte SAD[8]= {0x3C,0x42,0xA5,0x81,0x99,0xA5,0x42,0x3C};
printByte(SAD);
delay(1000);
}
// Clear data and LCD display
clearData();
}
}
void clearData() {
//Reset data_count
data_count = 0;
//Reset Data
Data ="";
}