Pages: [1] 2   Go Down
Author Topic: Arduino Security [I know there are lots of them]  (Read 638 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am using a mashup of parts though. Have a 4x4 keypad, LCD screen that runs on just 2 wires for signal, 4 relay board and an Uno.

Its works, once. I can't get it to reset for the next password. I was thinking of using a sensor that says the door has been opened to reset it or set off the alarm if the password was not used.

I am looking for just a little guidance. I need to finish out the build and the code. The LCD has an RGB LED in it and I want to use that to indicate some of the status. I only have 2 digital pins left and all the analog. With the door senor on those last two digital pins I'm not sure how to do the RGB LED but I am thinking the analog can run it.

Right now, can anyone help with the reset for the next password?

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

Password password = Password( "1234" );
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; //three 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
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


//PIN assignments

int r1 = 2; //alarm
int r2 = 3;  //green light
int r3 = 4;  //Red light
int r4 = 5;  //Nothing?


void setup(){

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.print("Security Status");
pinMode(r1, OUTPUT);
pinMode(r2, OUTPUT);
pinMode(r3, OUTPUT);
pinMode(r4, OUTPUT);

}

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



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

void checkPassword(){
if (password.evaluate()){
  lcd.clear();
lcd.println("Success!");
lcd.println("Disarmed!");
digitalWrite(r1, LOW); //DISCONNETC THE SIREN IF THE PASSWORD IS CORRECT
digitalWrite(r2, HIGH);
delay(2000);

digitalWrite(r3, HIGH);
delay(100);
}else{
    lcd.clear();
lcd.println("Wrong");
delay(1000);
lcd.clear();
WrongAttempts++; 

if (WrongAttempts >= 3) { //UPS....
digitalWrite(r1, HIGH); //START THE SIREN
digitalWrite(r3, HIGH); //START THE REDNESS
lcd.clear();
lcd.print("Alarm Activated");
}


}

digitalWrite(r2, HIGH);
delay(100);
digitalWrite(r2, HIGH);
delay(100);
 
}

 

 
« Last Edit: February 06, 2013, 10:45:48 am by morganlowe » Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4336
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It would help to understand your code if you gave the pins meaningful names.

Why do you need to know whether the door has opened ?  You already know that a correct password has been entered and if so, pins 'r2' and 'r3' are set high, presumably they are something to do with releasing the door (see what I mean about using meaningful names ?)

If the password is correct then release the door, give the user a few seconds to open the door, lock the door again and reset the password.  If the user did not open the door in the time allowed then, tough, they will need to enter the password again
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I suppose Io could explain that.

r 1-4 are the relays. I didn't bother with meaningful names because I'm not used to working like that.

Relay 1 is alarm
Relay 2 is a big red bulb
Relay 3 is a big green bulb
Relay 4 is nothing but defined because it's there.

This isn't for anything serious, I was going to install it on the bathroom door.

The alarm is an old school bell.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4336
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

r 1-4 are the relays. I didn't bother with meaningful names because I'm not used to working like that.
It would be a good idea to get into the habit

Your comments indicate what the pins are used for but you cannot tell when reading the code.

How about
Code:
int alarm = 2; //alarm
int green_light = 3;  //green light
int red_light = 4;  //Red light
int nothing_yet = 5;  //Nothing?

I must admit to being amused by the fact that your explanation of what the pins are used for does not match your comments, which I think proves the point about using meaningful names.
Quote
Relay 1 is alarm
Relay 2 is a big red bulb
Relay 3 is a big green bulb
Relay 4 is nothing but defined because it's there.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Alright, I'll change it, but that's not why I posted it here.

Thanks for the tip though!

How do we make it reset after a successful entry or after a set period of time when the alarm goes off.

One possibility is having a sensor for door open that resets it, but I don't think that would be the best overall solution. I am thinking something time based.
« Last Edit: February 06, 2013, 12:21:49 pm by morganlowe » Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think either option would be viable. Making the system rearm itself after a delay (ten seconds would be enough?) would be easiest since it does not require any extra hardware.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My question is how to implement it. I tried setting it in the code and I broke the whole thing. I'm a N00b, any thoughts? Tips?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4336
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What do you do when the user enters '#' ?
Can't you do the same after a delay when they enter a correct password and you have flashed lights or whatever ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My question is how to implement it. I tried setting it in the code and I broke the whole thing. I'm a N00b, any thoughts? Tips?

I would just add a fixed delay after the code that prints "Success!", and follow that by code to print out "Armed!" and reconnect the siren and do whatever you want to happen to the lamps. (Which, by the way, doesn't currently seem very sensible - the red and green lamps don't seem to be turned off in any coherent way. I suggest you decide what you want each lamp to represent, and then define when it needs to be turned on and off in terms of events within your sketch.)
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Alright, I made some changes. I think it's much better but still incomplete. I picked a hellova project for my introduction to this.

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

Password password = Password( "1234" );
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
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_OFF);
  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


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.print("Security Status");

}

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



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

void checkPassword(){
if (password.evaluate()){
  lcd.clear();
lcd.println("Success!");
lcd.setCursor(0, 1);
lcd.println("Disarmed!");
digitalWrite(Relay_1, RELAY_OFF); //DISCONNETC THE SIREN IF THE PASSWORD IS CORRECT
digitalWrite(Relay_2, RELAY_ON);
digitalWrite(Relay_4, RELAY_ON);
delay(2000);

digitalWrite(Relay_4, RELAY_OFF);

delay(100);
}else{
    lcd.clear();
lcd.println("Wrong");
delay(1000);
lcd.clear();
WrongAttempts++; 

if (WrongAttempts >= 3) { //UPS....
digitalWrite(Relay_1, RELAY_ON); //START THE SIREN
digitalWrite(Relay_3, RELAY_ON); //START THE REDNESS
lcd.clear();
lcd.print("Alarm Activated");
}


}

digitalWrite(Relay_2, RELAY_ON);
delay(100);
digitalWrite(Relay_2, RELAY_ON);
delay(100);
 
}

 
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4336
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why issue commands to set the relay pins low then define them as outputs ?

Much better with the meaningful names though.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oops, forgot to take that out. Thanks for reminding me!

So umm... Any ideas on how to reset it?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So umm... Any ideas on how to reset it?

You mean - any other ideas?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4336
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Leave the pinMode and digitalWrite commands in there but do them in the reverse order.

You have had my idea on how to reset the password when the user has been successful.  You do it when the user presses the '#' key
Code:
case '#': password.reset();
so why not when they get it right and the light show is triggered ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
      //-------( 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

Code:
//---( 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.

http://youtu.be/UNHlT6wiEhI


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

Code:
#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


}


}




}

 
« Last Edit: February 07, 2013, 01:58:44 am by morganlowe » Logged

Pages: [1] 2   Go Up
Jump to: