#include <Keypad.h> // http://arduino.cc/playground/uploads/Code/Keypad.zip
#include <Password.h> // http://arduino.cc/playground/uploads/Code/Password.zip
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int relay1 = 2; // Digital pin 2
int relay2 = 3; // Digital pin 3
int relay3 = 4; // Digital pin 4
int relay4 = 5; // Digital pin 5
int GreenLED = A1;
int BluLED = A0;
int locked = 1;
int passinput = 0;
int ledcurrentvar;
int lockedled = 13; // Define pin 13 for Output LED
int unlockedled = 12; // Define pin 12 for Output LED
long flashvarLED = 0;
long flashtimeLED = 300;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {{'1','2','3','A'},{'4','5','6','B'},{'7','8','9','C'},{'*','0','#','D'}};
byte rowPins[ROWS] = {13, 12, 11, 10};
byte colPins[COLS] = {9, 8, 7, 6};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
Password password = Password("23170"); // Yo can change the password here
void setup(){
Serial.begin(9600);
lcd.init();
home();
lcd.backlight();
pinMode(relay1, OUTPUT);
digitalWrite(relay1, 255);
pinMode(relay2, OUTPUT);
digitalWrite(relay2, 255);
pinMode(relay3, OUTPUT);
digitalWrite(relay3, 255);
pinMode(relay4, OUTPUT);
digitalWrite(relay4, 255);
pinMode(GreenLED, OUTPUT);
pinMode(BluLED, OUTPUT);
pinMode(lockedled, OUTPUT);
digitalWrite(lockedled, 255);
pinMode(unlockedled, OUTPUT);
digitalWrite(unlockedled, 0);
}
void home(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Please put Password");
lcd.setCursor(4, 1);
lcd.print("<<<<---->>>>");
lcd.setCursor(5, 2);
lcd.print("<<<---->>>");
lcd.setCursor(6, 3);
lcd.print("<<---->>");
}
void loop(){
char key = keypad.getKey();
if(locked){
if(passinput){
unsigned long currentvarLED = millis();
if(currentvarLED - flashvarLED > flashtimeLED) {
flashvarLED = ledcurrentvar;
digitalWrite(lockedled, !digitalRead(lockedled));
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("Key Password Mode");
}
}
else{
digitalWrite(lockedled, 255);
}
digitalWrite(unlockedled, 0);
digitalWrite(GreenLED, LOW);
digitalWrite(BluLED, HIGH);
}
if (key != NO_KEY){
Serial.println(key);
password.append(key);
passinput = 1;
if(key == '*'){
password.reset();
passinput = 0;
locked = 1;
lcd.print("Relay1 ON");
digitalWrite(relay1, 1);
digitalWrite(relay2, 1);
digitalWrite(relay3, 1);
digitalWrite(relay4, 1);
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Lock Mode");
lcd.setCursor(7, 2);
lcd.print("Thank");
delay(3000);
home();
}
if(password.evaluate()) {
digitalWrite(GreenLED, HIGH);
digitalWrite(BluLED, LOW);
locked = !locked;
password.reset();
passinput = 0;
}
if(!locked) {
passinput = 0;
digitalWrite(lockedled, 0);
digitalWrite(unlockedled, 255);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Controler Mode");
switch (key) {
case '1':
digitalWrite(relay1, !digitalRead(relay1));
lcd.setCursor(7,2);
lcd.print("Relay1");
break;
case '2':
digitalWrite(relay2, !digitalRead(relay2));
lcd.setCursor(7,2 );
lcd.print("Relay2");
break;
case '3':
digitalWrite(relay3, !digitalRead(relay3));
lcd.setCursor(7,2 );
lcd.print("Rela3");
break;
case '4':
digitalWrite(relay4, !digitalRead(relay4));
lcd.setCursor(7, 2);
lcd.print("Relay4");
break;
case '5':
digitalWrite(relay4, !digitalRead(relay4));
lcd.setCursor(7, 2);
lcd.print("Relay4");
break;
}
password.reset();
}
}
}