/*
* Electronic Safety Lock v2
*
* (c) 2020 by Vassilis Serasidis
* Home: https://www.serasidis.gr
* email: <avrsite@yahoo.gr> , <info@serasidis.gr>
*
* Components:
* 1. Arduino compatible board (http://tiny.cc/dqclkz)
* 2. 12-key Capacitive Touch Keypad (http://tiny.cc/3nclkz)
* 3. Grove - 16x2 I2C LCD (http://tiny.cc/2uclkz)
* 4. Servo Motor (http://shorturl.at/dfuSX)
*
* How to Use
* 1. Power ON the circuit
* 2. Enter the default password <1036>
* 3. The Servo Motor will unlock the Door
*
*/
#define SERVO_1 3 /* D3 as output. Drives the Servo motor */
#define SERVO_ON 160
#define SERVO_OFF 20
#define KBD_TX 10 /* Pin D10. Data from Capacitive Keyboard to Arduino (9600 bps) */
#define KBD_RX 11 /* This pin is not used */
#define KBD_ASTERISK 0x0A /* <*> */
#define KBD_ZERO 0x0B /* <0> */
#define KBD_SHARP 0x0C /* <#> */
#include <Wire.h>
#include <Servo.h>
#include "rgb_lcd.h"
#include <SoftwareSerial.h>
const byte my_password[] = {1,0,3,6};
const byte SIZE_OF_MY_PASS = sizeof(my_password);
byte success_digits = 0;
byte lcd_position = 0;
Servo myservo; /* create servo object to control a servo */
rgb_lcd lcd;
SoftwareSerial CapKeypad(KBD_TX, KBD_RX); // Arduino RX, Arduino TX
void setup() {
CapKeypad.begin(9600);
pinMode(KBD_TX, INPUT);
lcd.begin(16, 2);
clear_lcd_password_area();
myservo.attach(SERVO_1);
}
void loop() {
if(read_keyboard() == true){ /* If the typed password is correct, */
unlock_door(); /* unlock the door */
}
}
boolean read_keyboard(){
byte val;
if(CapKeypad.available() > 0){
val = CapKeypad.read() - 0xe0; /* Remove the prefix 0xE0 from the keyboard value */
if(val == KBD_ASTERISK){ /* <Clear password> Button */
clear_lcd_password_area();
return false;
}
if(val == KBD_ZERO){ /* <0> : Replace the Zero-Button value */
val = 0;
}
if(val == KBD_SHARP){ /* <Enter> Button */
if((success_digits == SIZE_OF_MY_PASS)&&(success_digits == lcd_position)){
success_digits = 0;
return true;
}else{
lcd.clear();
lcd.print(F(" Wrong password"));
delay(1000);
lcd.clear();
clear_lcd_password_area();
return false;
}
}
if((lcd_position < 14)&&(val < 0x0a)){
lcd_position++;
lcd.write('*');
}
if(val == my_password[success_digits]){
success_digits++;
}else{
success_digits = 0;
}
}
return false;
}
void clear_lcd_password_area(){
lcd_position = 0;
success_digits = 0;
lcd.setCursor(0, 0);
lcd.print(" Enter password");
lcd.setCursor(0, 1);
lcd.print(F(" "));
lcd.setCursor(0, 1);
}
void unlock_door(){
lcd.clear();
lcd.print(F("Door is unlocked"));
myservo.write(SERVO_ON);
delay(2000);
lcd.clear();
lcd.print(F(" Door is locked "));
myservo.write(SERVO_OFF);
delay(2000);
clear_lcd_password_area();
}