back lit keypad lock with 3 wrong code lockout?

Greetings all.

First off i am new to all things arduino and script making. i have soon got the hang of changing things in the script i am using though for this project to make it more suitable for me.

here is the script i am using:

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




Password password = Password( "123" ); //password to unlock, can be changed

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'},
  {'A', '0', 'B'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 5, 4, 2, 3 };// 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 );

void setup() {
  Serial.begin(9600);
  Serial.write(254);
  Serial.write(0x01);
  delay(200);
  pinMode(9, OUTPUT);  //green light
  pinMode(10, OUTPUT);  //red light
  pinMode(11, OUTPUT); //relay
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}

void loop() {
  keypad.getKey();

}
//take care of some special events
void keypadEvent(KeypadEvent eKey) {
  switch (keypad.getState()) {
    case PRESSED:

      Serial.print("Enter:");
      Serial.println(eKey);
      delay(10);

      Serial.write(254);

      switch (eKey) {
        case 'A': checkPassword(); delay(1); break;

        case 'B': password.reset(); delay(1); break;

        default: password.append(eKey); delay(1);
      }
  }
}
void checkPassword() {

  if (password.evaluate()) { //if password is right open

    Serial.println("Accepted");
    Serial.write(254); delay(10);
    //Add code to run if it works

    digitalWrite(9, HIGH);//turn on
    delay(1000); //wait 1 seconds
    digitalWrite(11, HIGH);//turn on
    delay(500); //wait 1 seconds
    digitalWrite(11, LOW);// turn off
    digitalWrite(9, LOW);// turn off
    password.reset();


  } else {
    Serial.println("Denied"); //if passwords wrong keep locked
    Serial.write(254); delay(10);
    //add code to run if it did not work
    digitalWrite(10, HIGH); //turn on
    delay(500); //wait 5 seconds
    digitalWrite(10, LOW);//turn off
    password.reset();

  }
}

is it possible to make it so one of the pins will pull high for a given length of time as soon as any button is pressed, to power a back light for the keypad?

also is it possible to make it so that every time a wrong code is entered it will make a pin(s) pull high (to power multiple small audio circuits) for a given length of time?

if so, how would i go about making those scripts.. could some one give me an example?

what i am trying to do is make a keypad lock with a pre-recorded computer generated voice.

access permitted, access denied, access denied. one attempt left, access denied lock down engaged

i will be using sound effects from the game Unreal and from the turrets on Unreal Tournament 99's "Chaos UT" expansion pack.

it will all become part of the door unlocking system on my project car for added effects :smiley:

i will also in the future be creating a system where it will auto detect if it is dark and turn on lighting automatically when unlocked, then maybe adding a fully computerised engine start procedure with computer voice, but that would be another project and maybe a different arduino chip. (bigger one as i am only using a uno at the moment)!

Thanks all!

Put into checkPassword() whatever shall happen on a valid/invalid entry. Connect whatever you need to the output pins used there.

See several things at a time for timed effects.

The rest depends on the used libraries, e.g. how to find out when a key was pressed, how to play sounds...

ahh thanks for that! ill have a good read through that thread :slight_smile:

i have already figured out that i will need way more output pins for what i have planed. i have to admit that i am really getting into this a lot faster than i thought i would!

oh the possibilities!!

well i tried to add a counter to this but sadly it did not work.

can anyone tell me what i did wrong?

#include <Password.h> //http://playground.arduino.cc/uploads/Code/Password.zip
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip




Password password = Password( "0" );

const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'A', '0', 'B'}
};
byte rowPins[ROWS] = { 5, 4, 2, 3 };
byte colPins[COLS] = { 8, 7, 6 };
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {
Serial.begin(9600);
Serial.write(254);
Serial.write(0x01);
delay(200);
pinMode(9, OUTPUT); //amber "busy" LED
pinMode(10, OUTPUT); //red "incorrect password" LED
pinMode(11, OUTPUT); //green "correct password" LED
pinMode(12, OUTPUT); // output to control flickering light when unlocked
pinMode(13, OUTPUT); // output to activate "access granted" sound recording
pinMode(20, OUTPUT); // output to activate "access denied" sound recording
pinMode(21, OUTPUT); // output to activate "access denied 2 attempts left" sound recording
pinMode(22, OUTPUT); // output to activate "access denied 1 attempt left" sound recording
pinMode(23, OUTPUT); // output to activate "system lock out" sound recording
keypad.addEventListener(keypadEvent);
}

void loop() {
keypad.getKey();

}
void keypadEvent(KeypadEvent eKey) {
switch (keypad.getState()) {
case PRESSED:

Serial.print("Enter:");
Serial.println(eKey);
delay(10);

Serial.write(254);

switch (eKey) {
case 'A': checkPassword(); delay(1); break;
case 'B': password.reset(); delay(1); break;

default: password.append(eKey); delay(1);
}
}
}
void checkPassword() {

int count = 0;
if (password.evaluate()) {

Serial.println("Accepted");
Serial.write(254); delay(10);
count= 0;

password.reset();
digitalWrite(9, HIGH);
delay(300);
digitalWrite(9, LOW);
delay(300);
digitalWrite(9, HIGH);
delay(300);
digitalWrite(9, LOW);
delay(300);
digitalWrite(9, HIGH);
delay(300);
digitalWrite(9, LOW);
delay(300);
digitalWrite(9, HIGH);
digitalWrite(11, HIGH);
delay(300);
digitalWrite(9, LOW);
digitalWrite(11, LOW);
delay(300);
digitalWrite(9, HIGH);
digitalWrite(11, HIGH);
delay(300);
digitalWrite(9, LOW);
digitalWrite(11, LOW);
delay(300);
digitalWrite(9, HIGH);
digitalWrite(11, HIGH);
delay(300);
digitalWrite(9, LOW);
digitalWrite(11, LOW);
digitalWrite(11, HIGH);
delay(1000);
digitalWrite(12, HIGH);
delay(100);
digitalWrite(12, LOW);
delay(200);
digitalWrite(12, HIGH);
delay(400);
digitalWrite(12, LOW);
delay(100);
digitalWrite(12, HIGH);
delay(600);
digitalWrite(12, LOW);
delay(150);
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
delay(1000);
digitalWrite(12, LOW);
delay(100);
digitalWrite(12, HIGH);
delay(5100);
digitalWrite(12, LOW);



} else if (count == 1){
digitalWrite(21, HIGH);
delay(300);
digitalWrite(21, LOW);
count = 0;
password.reset();

} else {
Serial.println("Denied");
Serial.write(254); 
delay(10);
count = count + 1;
digitalWrite(9, HIGH);
delay(300);
digitalWrite(9, LOW);
delay(300);
digitalWrite(9, HIGH);
delay(300);
digitalWrite(9, LOW);
delay(300);
digitalWrite(9, HIGH);
delay(300);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(20, HIGH);
delay(200);
digitalWrite(20, LOW);
delay(1300);
digitalWrite(10, LOW);
password.reset();
  }
}

i have to admit, i am finding this quite frustrating as i have no idea what to search for.. what do i call this other than something like " keypad lock attempt counter", but searching that comes back with nothing :frowning: i cant be the first person to make this can i?

thanks

A look into state machines would be helpful.

Start with the obvious states: Idle, ValidEntry, InvalidEntry. Then determine whether and how one state can be reached from some other state. Then you'll notice that you need more states for IncompleteKey, CommandA, CommandB etc.

Then put a switch(state) into loop(), and add to each distinct state the code to simply wait for a key pressed or some other event, process that input and eventually change the state.

hey guys,

i finally got this thing to do what i wanted! here is how i did it:

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

Password password = Password( "0" );
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'A', '0', 'B'}
};
byte rowPins[ROWS] = { 5, 4, 2, 3 };
byte colPins[COLS] = { 8, 7, 6 };

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

int ledPin =  10;
int maxnum = 3;
int count = 0;

void setup() {
  pinMode(9, OUTPUT);  //orange "working" light
  pinMode(10, OUTPUT); //red "access denied" light
  pinMode(11, OUTPUT); //green "access granted" light
  pinMode(12, OUTPUT); //sound access denied 2
  pinMode(13, OUTPUT); //sound access denied 3
  pinMode(14, OUTPUT); //sound access denied 1

  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}

void loop() {
  keypad.getKey();

}
void keypadEvent(KeypadEvent eKey) {
  switch (keypad.getState()) {
    case PRESSED:

      switch (eKey) {
        case 'A': checkPassword(); delay(1); break;
        case 'B': password.reset(); delay(1); break;
        default: password.append(eKey); delay(1);
      }
  }
}
void checkPassword() {


  //access granted
  if (password.evaluate()) {

    count = 0;
    password.reset();
    digitalWrite(9, HIGH);
    delay(300);
    digitalWrite(9, LOW);
    digitalWrite(11, HIGH);
    delay(1000);
    digitalWrite(11, LOW);
    return;


    //access denied
  } else {
    if (count < maxnum)
      password.reset();
    digitalWrite(9, HIGH);
    delay(300);
    digitalWrite(9, LOW);

  }

  if (count == 0)
  {
    digitalWrite(14, HIGH);
    digitalWrite(10, HIGH);
    delay(1000);
    digitalWrite(14, LOW);
    digitalWrite(10, LOW);
    count++;
    return;
  }

  if (count == 1)
  {
    digitalWrite(12, HIGH);
    digitalWrite(10, HIGH);
    delay(1000);
    digitalWrite(12, LOW);
    digitalWrite(10, LOW);
    count++;
    return;
  }

  if (count == 2)
  {
    digitalWrite(13, HIGH);
    digitalWrite(10, HIGH);
    delay(1000);
    digitalWrite(13, LOW);
    digitalWrite(10, LOW);
    count = 0;
    return;
  }
}

i'm still learning code.. till a few hours ago i never knew what "enter" did!

happy its working!

i can add other outputs later on.

what i would not mind doing next is connecting an old black and white TV to it as a display.
i already have my UNO running TVout.
it would be nice if i can make it so when i enter the password (passnumber??) it will input a "*" star rather than the number, then display text to confirm or deny entry.