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

Lab 3

Sorry, WESE331479_Bao Mat Web (7ELO).doc already exists on Scribd, please upload new content that other Scribd users will find valuable. Eg. class notes, research papers, presentations.

Uploaded by

Luyện Lê Văn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lab 3

Sorry, WESE331479_Bao Mat Web (7ELO).doc already exists on Scribd, please upload new content that other Scribd users will find valuable. Eg. class notes, research papers, presentations.

Uploaded by

Luyện Lê Văn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Guide for Digital Lock

1. Requirements
 You have to create a digital lock with Arduino, keypad 4x4 and a
servo motor (to move the latch for locking or unlocking)
 Your lock has a default password is 0000
 You lock can change password
 The # key will be used as function key
 Press # key less than 3s: call the unlock function
 Press # key more than 3s: call the change password function
2. Connected cuicuit

Arduino I/O pin Keypad Keypad Pin


12 R1 8
11 R2 7
10 R3 6
9 R4 5
8 C1 4
7 C2 3
6 C3 2
5 C4 1
3. Coding the SKETCH:
#include <Keypad.h>
#include <Servo.h>
#include <string.h>

/*
Lib: https://github.com/Chris--A/Keypad/archive/master.zip
*/
// Define some constants and variables
const byte rows = 4;
const byte columns = 4;
int holdDelay = 700;
int n =3;
int state = 0;
char key = 0;
int pos = 0;
//Default password is 0000
String default_passwd = "0000";
//variable to store the user input for password
String input_passwd = "";
//Define keys for lock and unlock or change password function

char lock_key = '*';


char unlock_key = '#';
char change_pass_key = '-'; //press * key for more than 3 second
// Create an instance for servo motor
Servo servo_4;

//Define characters matrix


char keys[rows][columns] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};

//Define pins for every row of keypad


byte rowPins[rows] = {12, 11, 10, 9};
//Define pins for every column of keypad
byte columnPins[columns] = {8, 7, 6, 5};
// Create an instance for our keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, columnPins, rows,
columns);
// Define function for key
char function_key(int n)
{
char temp = keypad.getKey();
if ((int)keypad.getState() == PRESSED)
{
if (temp != 0) {key = temp;}
}
if ((int)keypad.getState() == HOLD)
{
state++;
state = constrain(state, 1, n);
delay(holdDelay);
}
if ((int)keypad.getState() == RELEASED)
{
key += state;
state = 0;
}
delay(100);
Serial.println(key);
return key;
}
// Define function input_password
String input_password(int num_char)
{
String passwd = "";
//Serial.print("Input password: ");
do
{
char temp = keypad.getKey();
if (temp != 0) {Serial.print(temp); passwd += temp;}
delay(100);
}
while (passwd.length() < num_char);
Serial.println();
return passwd;
}
// Define function change_password
String change_password(int num_char, String current_passwd)
{
//Authenticate the old password:
Serial.print("Input old password: ");
String old_passwd = input_password(num_char);
if (old_passwd != current_passwd)
{
Serial.println("Password does not match! Nothing changes");
return current_passwd;
}
//New password
Serial.print("Input new password: ");
String new_passwd = input_password(num_char);
//Confirm passwd
Serial.print("Input new password again: ");
String confirm_passwd = input_password(num_char);

if (confirm_passwd == new_passwd)
{
Serial.println("Password has changed!!!");
return confirm_passwd;
}
else
{
Serial.println("Password does not match! Nothing changes");
return current_passwd;
}
}
void setup()
{
//Setting serial with baudrate 9600
Serial.begin(9600);
// connect signal pin of servo to pin number 4 on Uno
servo_4.attach(4);
servo_4.write(pos);
}
void unlock()
{
Serial.print("Input password: ");
input_passwd = input_password(4);
if (input_passwd == default_passwd)
{
//Unlock by servo_4
for (pos = 0; pos <= 180; pos += 1)
{
// tell servo to go to position in variable 'pos'
servo_4.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
delay(3000); //open door 3s then close
//lock by servo_4
for (pos = 180; pos >= 0; pos -= 1)
{
// tell servo to go to position in variable 'pos'
servo_4.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
}
else
{
Serial.println("Wrong password!");
}
}
void loop()
{
key = function_key(n);

if (key == unlock_key)
{
unlock()
//Reset input_passwd
input_passwd = "";
key = 0;
}

if (key == change_pass_key)
{
default_passwd = change_password(4, default_passwd);
delay(2000);
key =0;
}
}

You might also like