Arduino Security [I know there are lots of them]

OK, It's about done... I need to get rid of the funky weirdness on the display, maybe you guys could help with that?

I made a video of the pile of parts in action. I am still working on the door it's self, more to come later.

Here's the code again! I am using an RGB backlit display with a 2 wire I2C interface, a 4x4 Keypad and a 4 relay board. I have a fire alarm from a school that will be the siren and red and green 120v bulbs in cages for the indicator lights. Somewhere in my junk I have a magnetic door latch too.

Oh! Almost forgot.

      //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_ON);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF);

Must come before

//---( THEN set pins as outputs )----  
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);    
  delay(4000); //Check that all relays are inactive at Reset

Or when it boots everything is turned on and that's a mess.

To do list:

  1. Make the alarm only sound for 5 minutes. Just in case I put this to real use.
  2. Make an enclosure and cable system so the board and relays can be inside the locked room with only the keypad and display outside. 8 wires for keypad, 6 for display with RGB LED. Thinking a couple of Cat5e cables.
  3. Redundant Power Supply.
  4. Weird things on display.

Here's the code right now, the green light shuts off when the red comes on!

#include <Password.h>
#include <Keypad.h> 
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

//  Credit goes to Arduino and it's wonderful community and morganlowe for the code and setup.
//  Digital Pins 2=relay 1; 3 = relay 2; 4 = relay 3; 5 = relay 4; 6-13 = keypad; SDA & SDL = corrosponding inputs for LCD
//  Analog Pins A2 and A3 Green and Blue for LCD

Password password = Password( "5466" );
unsigned char WrongAttempts = 0; //global variable to keep the number of wrong attempts done at the code. 

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {6, 7, 8, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {10, 11, 12, 13}; //connect to the column pinouts of the keypad
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );



/*-----( Declare Constants )-----*/
#define RELAY_ON 0
#define RELAY_OFF 1
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
#define Relay_1  2  // Bell
#define Relay_2  3  // Green Lamp - ready
#define Relay_3  4  // Red Lamp - ALERT!
#define Relay_4  5  // Door actuator - not finished yet

void setup(){
  
      //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_ON);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF); 

//---( THEN set pins as outputs )----  
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);    
  delay(4000); //Check that all relays are inactive at Reset
  
 
  
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  


Serial.begin(9600);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  lcd.init(); // initialize the lcd
  lcd.backlight();
 
  // Print a message to the LCD.
  //lcd.setCursor(0, 1);
  lcd.clear();
lcd.println("Enter Password:");
  digitalWrite(A3, HIGH);
  digitalWrite(A2, LOW);
 lcd.noBacklight(); // turn off red channel

}

void loop()
{
keypad.getKey();
}



void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
  
case PRESSED:
lcd.setCursor(0, 1);
lcd.print("Pressed: ");
lcd.blink();
lcd.println(eKey);
switch (eKey){
case '*': checkPassword(); break;
case '#': password.reset(); break;
default: password.append(eKey);
}
}
}

void checkPassword(){
if (password.evaluate()){
  lcd.clear();
    digitalWrite(A3, LOW);
  digitalWrite(A2, HIGH);
  lcd.backlight();
lcd.println("Success!");
lcd.blink();
lcd.setCursor(0, 1);
lcd.println("Disarmed!");
lcd.blink();
digitalWrite(Relay_1, RELAY_OFF); //DISCONNETC THE SIREN IF THE PASSWORD IS CORRECT
digitalWrite(Relay_2, RELAY_ON);
digitalWrite(Relay_3, RELAY_ON);
digitalWrite(Relay_4, RELAY_ON);
password.reset();  //Reset Password
WrongAttempts = 0;  //Reset how many times it got screwed up
delay(5000);
password.reset();  //Reset Password
WrongAttempts = 0;  //Reset how many times it got screwed up

digitalWrite(Relay_1, RELAY_OFF);
digitalWrite(Relay_2, RELAY_ON);
digitalWrite(Relay_3, RELAY_OFF);
digitalWrite(Relay_4, RELAY_OFF);
lcd.clear();
lcd.backlight();
lcd.println("Armed!");
delay(3000);
lcd.clear();
lcd.println("Enter Password:");
  digitalWrite(A3, HIGH);
  digitalWrite(A2, LOW);
 lcd.noBacklight(); // turn off red channel
lcd.blink();
delay(100);
}else{
    lcd.clear();
      digitalWrite(A3, LOW);
  digitalWrite(A2, HIGH);
  lcd.backlight();
lcd.println("Wrong");
lcd.blink();
delay(1000);
lcd.clear();
lcd.backlight();
lcd.println("Armed!");
lcd.setCursor(0, 1);
lcd.println("Enter Password:");
  digitalWrite(A3, HIGH);
  digitalWrite(A2, LOW);
 lcd.noBacklight(); // turn off red channel
  password.reset();
WrongAttempts++;  



if (WrongAttempts >= 3) { //Trouble counter
digitalWrite(Relay_1, RELAY_ON); //START THE SIREN
digitalWrite(Relay_3, RELAY_ON); //START THE REDNESS
lcd.clear();
lcd.backlight();
lcd.print("Alarm Activated");
  digitalWrite(A3, HIGH);
  digitalWrite(A2, HIGH);
  password.reset();
  digitalWrite(Relay_2, RELAY_OFF); //turn off the good lamp
  password.reset();  //Reset Password
WrongAttempts = 0;  //Reset how many times it got screwed up


}


}




}