cerradura

Cerradura con teclado y  passwords para cuatro Cerraduras

Código :Probado con Arduino mega y teclado matriz

Al presionar en el teclado el numero 1111 y luego * se accionara el re-lay 1 y por consiguiente se abrir la cerradura 1.

al ingresar 2222 mas asterisco se abre la 2

al ingresar 3333 mas asterisco se abre la 3

al ingresar 4444 mas asterisco se abre la 4

El código se puede cambiar dentro de la programación a gusto.

cerradura

Código

Debemos tener cargadas las librerias  Keypad  y  Password  en el software arduino

 

************************************************************************

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

/*
To enter a code, punch in a four digit number and press ‘*’.
If the password is correct an LED (or anything) will be turned on
for a certain amount of time. To set a code, press on hold the ‘#’
key until an LED starts to flash, enter in a desired 4 digit code
and press ‘*’ to set. Press and hold ‘#’ to exit programming mode.
At anytime when entering a code press ‘#’ to clear the current entry.

– ThatRusty 2012
*/

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
// Define the keymap
char PadKeys[ROWS][COLS] = {
{ ‘1’,’2′,’3′, ‘A’ },
{ ‘4’,’5′,’6′, ‘B’ },
{ ‘7’,’8′,’9′, ‘C’ },
{ ‘*’,’0′,’#’, ‘D’ }
};
boolean setPass = false; // Start with the entry keypad.

byte rowPins[ROWS] = {5, 4, 3, 2}; //FILAS
byte colPins[COLS] = {8, 7, 6,9}; //COLUMNAS

// Create «two» new keypads, one is a entry pad and the other is a programming pad.
Keypad entryPad( makeKeymap(PadKeys), rowPins, colPins, sizeof(rowPins), sizeof(colPins) );
Keypad setPad( makeKeymap(PadKeys), rowPins, colPins, sizeof(rowPins), sizeof(colPins) );
unsigned long startTime;

const byte ledPin1 = 51; //Green LED, correct code + key press
const byte ledPin2 = 50; //red LED, MAL
const byte ledPin = 53;// PUERTA 1 Programming LED, flashes when in program mode
const byte ledPin3 = 11; // PUERTA 2
const byte ledPin4 = 12; // PUERTA 3
const byte ledPin5 = 13; // PUERTA 4

Password password = Password( «1111» ); //Default password 1
Password password2 = Password( «2222» ); //Default password 2
Password password3 = Password( «3333» ); //Default password 3
Password password4 = Password( «4444» ); //Default password 4
char Password2[5]; //Array used to set password #2

char PasswordArray[5]; //General entry array, used to store key presses

int i = 0; //counter
void setup() {
Serial.begin(9600);

pinMode(ledPin, OUTPUT);
pinMode (ledPin1, OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(ledPin3,OUTPUT);
pinMode(ledPin4,OUTPUT);
pinMode(ledPin5,OUTPUT);

digitalWrite(ledPin, LOW); // start programming LED off

//Set up keypads
setPad.begin( makeKeymap(PadKeys) );
entryPad.begin( makeKeymap(PadKeys) );
setPad.addEventListener(keypadEvent_set); // Add an event listener for programming mode
setPad.setHoldTime(1000); // Set how long a key must be held before a hold key event
entryPad.addEventListener(keypadEvent_entry); // Add an event listener for entry mode
entryPad.setHoldTime(11000); // TIEMPO PARA ENTRAR EN PROGRAMACION #
entryPad.setDebounceTime(200);
setPad.setDebounceTime(200);
}

char key; //Define key button variable

void loop() {

if( setPass ) //if setpass == true, get key presses from the programming key pad
key = setPad.getKey( );
else
key = entryPad.getKey( ); //if setpass == false, get key presses from the entry key pad

if (setPass && millis()-startTime>100) { // Flash the programming LED when in program mode
digitalWrite(ledPin,!digitalRead(ledPin));
startTime = millis();

}
}
static byte kpadState; //Define variable to check key state (PRESSED, HOLD…)

// Take care of some special events.

void keypadEvent_set(KeypadEvent key) {
// in here when in program mode.

kpadState = setPad.getState( );
swOnState( key ); //send swOnState function what key was pressed
} // end set keypad events

void keypadEvent_entry( KeypadEvent key ) {
// in here when using the entry keypad

kpadState = entryPad.getState( );
swOnState( key ); //send swOnState function what key was pressed
} // end entry keypad events

void swOnState( char key ) {
switch( kpadState ) {
case PRESSED: //If a key was pressed
blink(ledPin1, 80); //Blink LED when key is pressed, send which LED and how long of a delay

if (setPass) { // In program mode so we’re using the programming keymap.

switch (key){
case ‘*’: setPassword(); //After you punch in you new password press ‘*’ to set it
break;

case ‘#’: resetPasswordArray(); //Clear the Password array
Serial.println(«Clear»);

default:
if (key != ‘#’){ //if the key pressed was not ‘#’ , append key to entry array
PasswordArray[i] = key; i++;} //increment counter
if (key != ‘#’) {Serial.print(key);} //Print out what key was pressed
}

}else{ //In entry mode, use entry keypad
//Code Entry goes here

switch(key){
case ‘*’: checkPassword();
break;
case ‘#’: resetPasswordArray();
Serial.println();
Serial.println(«Clear»);

default:
if (key != ‘#’){ //if the key pressed was not ‘#’ , append key to entry array
PasswordArray[i] = key; i++;} //increment counter
if (key != ‘#’) {Serial.print(key);} //Print out what key was pressed

}

}
break;

case HOLD:
if (key == ‘#’) { //If ‘#’ key was held toggle keypads
if (setPass == true) { // We are currently using a keymap with letters
setPass = false; // Now we want a keymap with numbers.
digitalWrite(ledPin, LOW);
}
else { //Currently using the entry keypad
setPass = true; // Now we want to use the programming keypad
}

}

break;
} // end switch-case
}// end switch on state function

void checkPassword() {
Serial.println();

if (i>3 && i<5){ //Only accept passwords that are 4 characters

if (password.is(PasswordArray)){ //check punched in code againsted password

Serial.println(«PUERTA 1 (ABIERTA)»);
blink (ledPin, 1700); //Blink LED TIEMPO DE RETARDO EN MANTENAR ABIERTA PUERTA 1

}else if (password2.is(PasswordArray)){ //If punched in code wasn’t the first password check the second

Serial.println(«PUERTA 2 (ABIERTA)»);
blink (ledPin3, 1700); //Blink LED TIEMPO DE RETARDO EN MANTENAR ABIERTA PUERTA 2

}else if (password3.is(PasswordArray)){ //If punched in code wasn’t the first password check the second

Serial.println(«PUERTA 3 (ABIERTA)»);
blink (ledPin4, 1700); //Blink LED TIEMPO DE RETARDO EN MANTENAR ABIERTA PUERTA 3
}else if (password4.is(PasswordArray)){ //If punched in code wasn’t the first password check the second

Serial.println(«PUERTA 4 (ABIERTA)»);
blink (ledPin5, 1700); //Blink LED TIEMPO DE RETARDO EN MANTENAR ABIERTA PUERTA 4
} else {

Serial.println(«ACCESO DENEGADO»);
blink(ledPin2, 700);

}

}else{
Serial.println(«CANTIDAD DE CARACTERES INCORECTO.»);
blink(ledPin2, 700);
}
i=0; //reset counter so another code can be entered
resetPasswordArray();
password.reset();
password2.reset();

}

void setPassword(){
Serial.println();
if (i>3 && i<5) { //Only accept passwords that are 4 characters
for (i=0;i<4;i++){
Password2[i] = PasswordArray[i]; //Set Password2 array to characters in entry array
}
password2.set(Password2); //Set password2 to the punched in code
Serial.println(«New password has been set…»);
}else{
Serial.println(«Your password must be 4 characters.»);
blink(ledPin, 700);
}
i=0; //reset counter
resetPasswordArray();
// User must exit program mode by holding the ‘#’ key

}

void resetPasswordArray() { //Clear entry array

for (i=0;i<5;i++){
PasswordArray[i] = ‘\0’; //set all characters in the entry array to null
}

i=0; //reset counter
}

void blink (const byte LED, int DELAY){

digitalWrite(LED, !digitalRead(LED));
delay (DELAY);
digitalWrite(LED, !digitalRead(LED));

}

**********************************************************************

Elementos utilizados

arduino_mega_rev_3_bb tecladorelay de cuatrocedadura1k-resistorledtrafo


 

 

Juantruta.com.ar