0% found this document useful (0 votes)
12 views

Cerradura Con Clave Arduino

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Cerradura Con Clave Arduino

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

#include <LiquidCrystal.

h>

#include <Keypad.h>

#include <Servo.h>

Servo servo1;

/*-------------------------------KEYPAD---------------------------------------*/

const byte numRows= 4; //number of rows on the keypad

const byte numCols= 4; //number of columns on the keypad

char keypressed;

char keymap[numRows][numCols]=

{'1', '2', '3', 'A'},

{'4', '5', '6', 'B'},

{'7', '8', '9', 'C'},

{'*', '0', '#', 'D'}

};

//Code that shows the the keypad connections to the arduino terminals

byte rowPins[numRows] = {0,1,2,3};//Rows 0 to 3

byte colPins[numCols] = {4,5,6,7};//Columns 0 to 3

//initializes an instance of the Keypad class

Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

/*-------------------------------CONSTANTS------------------------------------*/

LiquidCrystal lcd(A0,A1,A2,A3,A4,A5); //LCD

const int buzzer = 13; //Buzzer/small speaker


const int lock = 9; //Electric door opener

int LEDR =10;

int LEDV =11;

/*-------------------------------VARIABLES------------------------------------*/

String password="2580"; //Variable to store the current password

String tempPassword=""; //Variable to store the input password

int doublecheck; //Check twice the new passoword

boolean armed = false; //Variable for system state (armed:true / unarmed:false)

boolean input_pass; //Variable for input password (correct:true / wrong:false)

boolean storedPassword = true;

boolean changedPassword = false;

boolean checkPassword = false;

int i = 1; //variable to index an array

/*----------------------------------------------------------------------------*/

void setup(){

pinMode (LEDR, OUTPUT);

pinMode (LEDV, OUTPUT);

servo1.attach(9); //pin del servo

pinMode(lock,OUTPUT);

lcd.begin(16, 2); //Setup the LCD's number of columns and rows

//Print welcome message...


lcd.setCursor(0,0);

lcd.print("Arduino Segurity");

lcd.setCursor(0,1);

lcd.print(":::electronico:::");

delay(2000);

lcd.clear();

lcd.setCursor(0,0);

lcd.print(" MJTV ");

lcd.setCursor(0,1);

lcd.print("INGRESE PASSWORD ");

delay(2000);

lcd.clear();

void loop() { //Main loop

digitalWrite(LEDR,HIGH);

unlockTheDoor();

/********************************FUNCTIONS*************************************/

void unlockTheDoor(){

lockAgain: //goto label

tempPassword="";

lcd.clear();

i=6;

noTone(buzzer);
digitalWrite(lock, LOW);

while(!checkPassword){

lcd.setCursor(0,0);

lcd.print("INGRESE PASSWORD ");

lcd.setCursor(0,1);

lcd.print("PASS>");

keypressed = myKeypad.getKey(); //Read pressed keys

if (keypressed != NO_KEY){ //Accept only numbers and * from keypad

if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||

keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||

keypressed == '8' || keypressed == '9' ){

tempPassword += keypressed;

lcd.setCursor(i,1);

lcd.print("*"); //Put * on lcd

i++;

tone(buzzer,800,200); //Button tone

else if (keypressed == 'A'){

changePassword();

goto lockAgain;

else if (keypressed=='#'){

break;

else if (keypressed == '*'){ //Check for password

if (password==tempPassword){//If it's correct...


digitalWrite(LEDR, LOW);

digitalWrite(LEDV, HIGH);

servo1.write(90);

servo1.write(0);

delay(500);

lcd.clear();

lcd.setCursor(0,0);

lcd.print("PASSWORD CORRECTO");

lcd.setCursor(0,1);

lcd.print("abriendo pueta");

tone(buzzer, 1000); //Play a tone while door is unlocked

delay(5000);

digitalWrite(LEDV, LOW);

digitalWrite(LEDR, HIGH);

servo1.write(90);

goto lockAgain; //apagar buzzer

else{ //if it's false, retry

tempPassword="";

tone(buzzer,500,200);
delay(300);

tone(buzzer,500,200);

delay(300);

goto lockAgain;

//Change current password

void changePassword(){

retry: //label for goto

tempPassword="";

lcd.clear();

i=1;

while(!changedPassword){ //Waiting for current password

keypressed = myKeypad.getKey(); //Read pressed keys

lcd.setCursor(0,0);

lcd.print("CAMBIAR PASSWORD");

lcd.setCursor(0,1);

lcd.print(">");

if (keypressed != NO_KEY){

if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||

keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||


keypressed == '8' || keypressed == '9' ){

tempPassword += keypressed;

lcd.setCursor(i,1);

lcd.print("*");

i++;

tone(buzzer,800,200);

else if (keypressed=='#'){

break;

else if (keypressed == '*'){

i=1;

if (password==tempPassword){

storedPassword=false;

tone(buzzer,500,200);

newPassword(); //Password is corrent, so call the newPassword function

break;

else{ //Try again

tempPassword="";

tone(buzzer,500,200);

delay(300);

tone(buzzer,500,200);

delay(300);

goto retry;

}
}

String firstpass;

//Setup new password

void newPassword(){

tempPassword="";

changedPassword=false;

lcd.clear();

i=1;

while(!storedPassword){

keypressed = myKeypad.getKey(); //Read pressed keys

if (doublecheck==0){

lcd.setCursor(0,0);

lcd.print("SELC NUEVO PASSWORD");

lcd.setCursor(0,1);

lcd.print(">");

else{

lcd.setCursor(0,0);

lcd.print("CONFIRMAR PASSWORD");

lcd.setCursor(0,1);

lcd.print(">");

if (keypressed != NO_KEY){
if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||

keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||

keypressed == '8' || keypressed == '9' ){

tempPassword += keypressed;

lcd.setCursor(i,1);

lcd.print("*");

i++;

tone(buzzer,800,200);

else if (keypressed=='#'){

break;

else if (keypressed == '*'){

if (doublecheck == 0){

firstpass=tempPassword;

doublecheck=1;

newPassword();

if (doublecheck==1){

doublecheck=0;

if (firstpass==tempPassword){

i=1;

firstpass="";

password = tempPassword; // New password saved

tempPassword="";//erase temp password

lcd.setCursor(0,0);
lcd.print("PASSWORD CAMBIADO");

lcd.setCursor(0,1);

lcd.print("----------------");

storedPassword=true;

tone(buzzer,500,400);

delay(2000);

lcd.clear();

break;

else{

firstpass="";

newPassword();

You might also like