#include <Keypad.h>
int lock_pin = A0;
int unlock_pin = A1;
int green_led = 11;
int blue_led = 12;
int red_led = 13;
int j = 0;
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
const String password = "432"; // change your password here
String input_password;
void setup() {
pinMode(lock_pin, OUTPUT);
pinMode(unlock_pin, OUTPUT);
pinMode(green_led, OUTPUT);
pinMode(blue_led, OUTPUT);
pinMode(red_led, OUTPUT);
Serial.begin(9600);
input_password.reserve(32); // maximum input characters is 33, change if needed
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key == '*') {
input_password = ""; // clear input password
} else if (key == '#') {
if (password == input_password) {
Serial.println("password is correct");
Serial.println(input_password);
if (j == 0)
{
digitalWrite(lock_pin, HIGH);
digitalWrite(green_led, HIGH);
delay(1000);
digitalWrite(lock_pin, LOW);
digitalWrite(green_led, LOW);
j = 1;
}
else if (j == 1)
{
digitalWrite(unlock_pin, HIGH);
digitalWrite(blue_led, HIGH);
delay(500);
digitalWrite(unlock_pin, LOW);
digitalWrite(blue_led, LOW);
j = 0;
}
} else {
Serial.println("password is incorrect, try again");
digitalWrite(red_led, HIGH); //..the green LED not..
delay(500);
digitalWrite(red_led, LOW);
}
input_password = ""; // clear input password
} else {
input_password += key; // append new character to input password string
}
}
}
Comments