Go Down

Topic: Keypad Security Feature (Read 1 time) previous topic - next topic

jdimpas

Oct 14, 2012, 02:26 pm Last Edit: Oct 14, 2012, 03:18 pm by jdimpas Reason: 1
Hi All,

I have a 4x3 keypad and I wanted to make a security system on it, to get started I already have the (red) LED and (green)LED working on the sketch as ARM and DISARM features. I was thinking to add a bit, like making the siren switch On whenever I enter (3) wrong password repeatedly.

I can't figure it out.

Appreciate any inputs, thanks!


Code: [Select]
#include <Password.h>
#include <Keypad.h>

Password password = Password( "1234" );

const byte ROWS = 4; // Four rows
const byte COLS = 3; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 5, 4, 3, 2 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 8, 7, 6 };

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


//PIN assignments

int alarmSiren = 11;
int redLED = 10;
int greenLED = 9;


void setup(){

Serial.begin(9600);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad

pinMode(alarmSiren, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);

}

void loop()
{
keypad.getKey();


//######Siren on when 3 wrong repeated PIN entered####
int i;
for(i=0; i<4;i+1);

if(i==3)
 {
  digitalWrite(alarmSiren, HIGH);
 }

}
//########################################



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

void checkPassword(){
if (password.evaluate()){
Serial.println("Success! System Disarmed");

digitalWrite(greenLED, HIGH);
delay(100);
digitalWrite(greenLED, HIGH);
delay(100);



}else{
Serial.println("Wrong");
}

digitalWrite(redLED, HIGH);
delay(100);
digitalWrite(redLED, HIGH);
delay(100);

}

PaulS

Quote
I know it's kind of basic, but I can't figure it out.

Can't figure what out?

That code does something. You want it to do something. It seems fairly obvious that if the two somethings were the same thing, you wouldn't have come here asking for help.

What either of those somethings is, though, is not at all obvious.

The ball's back in your court.

jdimpas

@PaulS. Opps Sorry Sir..
I think the line I made was misleading you, I wasn't referring to myself and think this as a piece of cake either. What I meant was people around here might think this query is just a basic from their end.  :)

apology for that.

bubulindo


Quote
I know it's kind of basic, but I can't figure it out.

Can't figure what out?



I was thinking to add a bit, like making the siren switch On whenever I enter (3) wrong password repeatedly.

Seems obvious to me that this is what he needs figuring out... did you even tried to read the text?

Have a look at a different version of your software with some comments and the functionality you want... I haven't tested, but it should be something like this.

Code: [Select]

#include <Password.h>
#include <Keypad.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 = 3; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 5, 4, 3, 2 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 8, 7, 6 };

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


//PIN assignments

int alarmSiren = 11;
int redLED = 10;
int greenLED = 9;


void setup(){

Serial.begin(9600);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad

pinMode(alarmSiren, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);

}

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



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

void checkPassword(){
if (password.evaluate()){
Serial.println("Success! System Disarmed");
digitalWrite(alarmSiren, LOW); //DISCONNETC THE SIREN IF THE PASSWORD IS CORRECT
digitalWrite(greenLED, HIGH);
delay(100);

//WHAT DOES THIS DO??????
digitalWrite(greenLED, HIGH);
delay(100);
}else{
Serial.println("Wrong");
WrongAttempts++;  //ADDED

if (WrongAttempts >= 3) { //UPS....
digitalWrite(alarmSiren, HIGH); //START THE SIREN
}


}

digitalWrite(redLED, HIGH);
delay(100);
digitalWrite(redLED, HIGH);
delay(100);

}
Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

PaulS

Code: [Select]
int i;
for(i=0; i<4;i+1);

if(i==3)

The for loop with curly braces added looks like:
Code: [Select]
for(i=0; i<4;i+1)
{
   ;
}

Such a loop hardly seems useful to me. Especially given that i never actually increments. So, the loop never ends.

Even if you correct that issue, i will be 4 when the loop does end, so comparing it to 3 doesn't make sense.

Code: [Select]
keypad.getKey();
The getKey() method returns a value. If you don't care what that value is, why bother calling the function?

If you want to trigger an alarm when there have been 3 invalid attempts to enter the password, checkPassword() needs to count the number of times it is called, in a static or global variable. Then, it needs to do some (or loop() does, if the variable is global) when that counter gets to 3 and the password is still wrong.

jdimpas



Quote
I know it's kind of basic, but I can't figure it out.

Can't figure what out?



I was thinking to add a bit, like making the siren switch On whenever I enter (3) wrong password repeatedly.

Seems obvious to me that this is what he needs figuring out... did you even tried to read the text?

Have a look at a different version of your software with some comments and the functionality you want... I haven't tested, but it should be something like this.

Code: [Select]

#include <Password.h>
#include <Keypad.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 = 3; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 5, 4, 3, 2 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 8, 7, 6 };

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


//PIN assignments

int alarmSiren = 11;
int redLED = 10;
int greenLED = 9;


void setup(){

Serial.begin(9600);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad

pinMode(alarmSiren, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);

}

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



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

void checkPassword(){
if (password.evaluate()){
Serial.println("Success! System Disarmed");
digitalWrite(alarmSiren, LOW); //DISCONNETC THE SIREN IF THE PASSWORD IS CORRECT
digitalWrite(greenLED, HIGH);
delay(100);

//WHAT DOES THIS DO??????
digitalWrite(greenLED, HIGH);
delay(100);
}else{
Serial.println("Wrong");
WrongAttempts++;  //ADDED

if (WrongAttempts >= 3) { //UPS....
digitalWrite(alarmSiren, HIGH); //START THE SIREN
}


}

digitalWrite(redLED, HIGH);
delay(100);
digitalWrite(redLED, HIGH);
delay(100);

}



thank you sir bubulindo. I will try this out :)

jdimpas



Quote
I know it's kind of basic, but I can't figure it out.

Can't figure what out?



I was thinking to add a bit, like making the siren switch On whenever I enter (3) wrong password repeatedly.

Seems obvious to me that this is what he needs figuring out... did you even tried to read the text?

Have a look at a different version of your software with some comments and the functionality you want... I haven't tested, but it should be something like this.

Code: [Select]

#include <Password.h>
#include <Keypad.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 = 3; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 5, 4, 3, 2 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 8, 7, 6 };

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


//PIN assignments

int alarmSiren = 11;
int redLED = 10;
int greenLED = 9;


void setup(){

Serial.begin(9600);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad

pinMode(alarmSiren, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);

}

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



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

void checkPassword(){
if (password.evaluate()){
Serial.println("Success! System Disarmed");
digitalWrite(alarmSiren, LOW); //DISCONNETC THE SIREN IF THE PASSWORD IS CORRECT
digitalWrite(greenLED, HIGH);
delay(100);

//WHAT DOES THIS DO??????
digitalWrite(greenLED, HIGH);
delay(100);
}else{
Serial.println("Wrong");
WrongAttempts++;  //ADDED

if (WrongAttempts >= 3) { //UPS....
digitalWrite(alarmSiren, HIGH); //START THE SIREN
}


}

digitalWrite(redLED, HIGH);
delay(100);
digitalWrite(redLED, HIGH);
delay(100);

}



this is a great help, thanks for being so nice Sir.

I did test it and it works well. Though made some changes in assigning LEDs function.





Go Up