#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {7, 6, 5, 4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {3, 2, 1, 0}; //connect to the column pinouts of the keypad
#define BUZZER 12
#define GREEN_LED 9
#define BLUE_LED 10
#define RED_LED 11
#define RELAY 8
#define BUTTON 13
#define LONG_BUZZ 2
#define SHORT_BUZZ 1
#define LONG_THRESH 100
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal_I2C lcd(0x27, 16, 2);
char password[4]={'1','7','0','1'};
char user[4];
int buzz_password[4] = {LONG_BUZZ, SHORT_BUZZ, LONG_BUZZ, LONG_BUZZ};
int buzz_user[4];
int pass_index=0;
bool user_done=false;
int lastState = HIGH;
int buzz_index=0;
int start_time, stop_time, pressed_time;
bool buzz_user_done=false;
void setup(){
init_buzzer();
init_RGB();
init_relay();
init_LCD();
init_button();
}
void loop(){
//test_all();
main_logic();
}
void main_logic()
{
get_keypad();
get_buzz();
if(user_done || buzz_user_done)
{
if (check_password() || check_buzz_password())
{
open_door();
}
else
{
lcd.print("Error");
}
user_done=false;
buzz_user_done=false;
}
}
//init
void init_buzzer()
{
pinMode(BUZZER, OUTPUT);
}
void init_button()
{
pinMode(BUTTON, INPUT_PULLUP);
}
void init_RGB()
{
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
}
void init_relay()
{
pinMode(RELAY, OUTPUT);
}
void init_LCD()
{
lcd.init();
lcd.clear();
lcd.backlight();
lcd.home();
}
//buzzer
void play_buzzer(int time)
{
tone(BUZZER, 2000);
delay(time);
}
void stop_buzzer()
{
noTone(BUZZER);
}
//rgb led
void rgb_led(int red,int green,int blue)
{
analogWrite(RED_LED,red);
analogWrite(BLUE_LED,blue);
analogWrite(GREEN_LED,green);
}
void led_off()
{
rgb_led(0,0,0);
}
//relay
void open_lock(int time)
{
digitalWrite(RELAY, HIGH);
delay(time);
}
void close_lock()
{
digitalWrite(RELAY, LOW);
}
//test
void test_all()
{
//test_buzzer();
//test_led();
// test_relay();
// test_LCD();
//test_keypad();
//test_button();
get_buzz();
}
void test_buzzer()
{
play_buzzer(1000);
stop_buzzer();
delay(1000);
}
void test_button()
{
int value = digitalRead((BUTTON));
lcd.setCursor(0,0);
if (lastState != value) {
lastState = value;
if (value == HIGH) {
lcd.print(" released");
}
if (value == LOW) {
lcd.println(" pressed");
}
}
delay(50);
}
void test_led()
{
rgb_led(255,0,0);
delay(1000);
rgb_led(0,255,0);
delay(1000);
led_off();
delay(1000);
}
void test_relay()
{
open_lock(2000);
close_lock();
delay(1000);
}
void test_LCD()
{
lcd.setCursor(3,0);
lcd.print("Hello world");
delay(1000);
lcd.setCursor(5,1);
lcd.print("Goodbye");
delay(1000);
lcd.clear();
delay(1000);
}
void test_keypad()
{
char key = keypad.getKey();
if (key){
lcd.print(key);
}
}
//mainlogic
void get_keypad()
{
char key = keypad.getKey();
if (key){
lcd.print(key);
user[pass_index]=key;
if(pass_index==3)
{
user_done=true;
pass_index=0;
}
else
{
user_done=false;
pass_index++;
}
}
}
bool check_password()
{
for(int j=0;j<4;j++)
{
if(user[j] != password[j])
return false;
}
return true;
}
void open_door()
{
lcd.clear();
lcd.print("OK");
open_lock(5000);
user_done = false;
close_lock();
}
bool door_bell()
{
int value = digitalRead((BUTTON));
//lcd.setCursor(0,0);
if (lastState != value) {
lastState = value;
if (value == HIGH) {
//lcd.print(" released");
stop_buzzer();
stop_time = millis();
/*
lcd.setCursor(0,1);
lcd.print("stop = ");
lcd.println(stop_time);
*/
pressed_time = stop_time - start_time;
delay(1000);
return true;
}
if (value == LOW) {
//lcd.println(" pressed");
tone(BUZZER, 700);
start_time = millis();
lcd.clear();
/*
lcd.setCursor(0,0);
lcd.print("start = ");
lcd.print(start_time);
*/
return false;
}
}
return false;
}
void get_buzz()
{
int buzz;
if (door_bell())
{
/*
lcd.setCursor(0,0);
lcd.print("pressed ");
lcd.setCursor(12,0);
lcd.print(pressed_time);
*/
if (pressed_time > LONG_THRESH)
buzz = LONG_BUZZ;
else
buzz = SHORT_BUZZ;
buzz_user[buzz_index] = buzz;
//lcd.setCursor(0,1);
lcd.print(buzz);
if(buzz_index==3)
{
buzz_user_done=true;
buzz_index=0;
}
else
{
buzz_user_done=false;
buzz_index++;
}
}
}
bool check_buzz_password()
{
lcd.print("checking");
for(int j=0;j<4;j++)
{
if(buzz_user[j] != buzz_password[j])
return false;
}
return true;
}