Lab 3
Lab 3
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
/*
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
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;
}
}