#include <Keypad.h>
#include <LiquidCrystal.h>
#include <string.h>
#define RLedPin 10
#define GLedPin 13
// Create an LCD object
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int ROW_NUM = 4; // four rows
const int COLUMN_NUM = 4; // four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {A1, A2, A3, A4}; // connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
int count = 0;
char code[4] = "";
void setup(){
Serial.begin(9600);
// Set up the number of columns and rows on the LCD
lcd.begin(16, 2);
pinMode(RLedPin, OUTPUT);
pinMode(GLedPin, OUTPUT);
welcome();
}
void loop(){
char key = keypad.getKey();
if (key){
lcd.print(key);
strncat(code, &key, 1);
if(count == 4){
// remove the null character from the end of the code array
code[4] = '\0';
if(strcmp(code,"1234") == 0){
lcd.clear();
lcd.print("access Granted");
digitalWrite(GLedPin, HIGH);
digitalWrite(RLedPin, LOW); // turn off the red LED
}
else{
lcd.clear();
lcd.print("access Denied");
digitalWrite(RLedPin, HIGH); // turn on the red LED
digitalWrite(GLedPin, LOW); // turn off the green LED
}
delay(2000);
// clear the code array by setting all of its elements to \0
for (int i = 0; i < 4; i++) {
code[i] = '\0';
}
count = 0; // reset the count variable
welcome();
}
else{
count += 1;
}
}
}
void welcome(){
lcd.clear();
lcd.print("Welcome");
delay(2000);
lcd.clear();
lcd.print(" enter code:");
}