Assignment2Reportaa03980 PDF
Assignment2Reportaa03980 PDF
Assignment2Reportaa03980 PDF
Homework 2
Name: Arsalan Ahmed
ID: aa03980
Instructor: Dr. Muhammed Farhan
Problem Statement:
Build a simple password protected door lock system which stores user specified 4-digit password
in microcontroller. If user enters wrong password a suitable message should be displayed on LCD
and if the password entered is correct, LCD will display corresponding suitable message and
microcontroller will send open command to servo motor connected to door assembly to open i.e. go
from 0° to 180°. Door closes automatically after 10 seconds. The password should be changeable
from old password verified user. Building door assembly is not required for this task.
Approach:
This task was not very complex and had a very simple solution. The gist of which is that we had to
take in an entered password from the user, match it with the set password and do the required task
in both the conditions where the entered password matched with the original password and also
where it did not.
So to start with I configured my servo motor, keypad and lcd by including all the libraries required
and initialized all the required objects using those libraries. Making the object of the keypad
required me to define the number of rows and columns on the keypad and also the map of the
keypad that is specifying individual keys in a 3x3 array.
In the setup, the program was initialized by starting the lcd by giving it the beginning command and
displaying “HELLO” on the first row of the LCD. Here also the connection of the servo motor with
the Arduino was specified. A delay of 2 sec was also used in order to hold the message of 2 sec.
Then for the main program I made several functions which could take care of different tasks. First
of all, I made a function named EnterPassword() which was the only function called in the program
loop. Other functions were called inside the EnterPassword() function. Its function was to ask the
user to enter password and then match it with the correct password and perform the required
tasks accordingly. For that I made 2 global character arrays that would save the saved password
and the entered password respectively. The saved password array had the correct password while
the entered password array had spaces initially which were to fill up when the user will press the
digits. I also defined a counter global variable to keep track of the number of digits that the user has
pressed. In the function what I did was I kept monitoring the digits that the user had pressed and
kept displaying them on the lcd using the appropriate commands (I made separate functions for
displaying the passwords) and also saving the digits at their appropriate position in the entered
password using the digits counter. Once the 4 digits were pressed I moved onto the comparison of
the passwords. If the password did not match I displayed the message that the password is
incorrect on the lcd. If the password was correct that is the entered password matched with the
saved password I again displayed the message that the password is correct and then performed 2
tasks through 2 functions. The first task was to open the door for 10 sec and the other was to give
the user the option of changing the password which was done with the OpenDoor() function and
ChangePassword() function respectively which will be discussed in the subsequent paragraphs.
Before finishing the EnterPassword() function, in the comparison if statement I reset the digit
counter and again filled spaces in the entered password array for the next iteration of the program.
The OpenDoor() function was fairly simple as I just rotated the servo motor from position 0 to 180
using a for loop where the position was monitored by a global pos variable. Once the servo reached
180 degrees I held it there for 10 sec using a delay and then again ran the for loop in reverse to
bring it back to its original position.
In the ChangePassword() function I again monitored the user input but this time only for the keys
‘*’ and ‘#’ as ‘*’ meant that the user wanted to change the password and ‘#” meant that he wanted
the password to stay the same. So I continuously displayed the message for the input of these 2
keys until any of these 2 keys were pressed. If the user pressed ‘#’ key the function ended as it
meant the user did not want to change the password and the program went back to the starting. If
the user pressed ‘*’ I started monitoring user input from the keyboard and saving it in the saved
password array and also displayed it on the lcd. The saved password array was filled with spaces
before that so that when it is displayed on the lcd only the new digits are visible. Once the user had
entered 4 digits I displayed the message on the screen that the new password has been saved and
the program moved back to the starting.
Besides these I also had two functions to display the elements of both the arrays called
ShowSavedPassword() and ShowEnteredPassword().
Results:
All the tasks of the assignment were successfully completed as the servo motor rotated 180 degrees
and stayed there for 10 sec when the correct password was entered and also gave an option to set a
new password. The user could save the new password when he pressed ‘*’ or could go on with the
same password by pressing ‘#’ after the correct entering of the current password. If any user
entered a wrong password appropriate message was displayed. Initially I set the correct password
as ‘0000’.
Appendix:
/*
Microcontrollers and Interfacing
//Including the header files of all the libraries required for this program.
#include <LiquidCrystal.h>
#include <Servo.h>
#include <Keypad.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Using the lcd() function to configure the lcd according to the
standard pin connections.
//Assigning the corresponding pin numbers.
byte rowPins[ROWS] = {10,9,8,7}; //Connecting the keypad ROW0, ROW1, ROW2 and ROW3 to
these Arduino pins.
byte colPins[COLS] = {A2,A1,A0}; //Connecting the keypad COL0, COL1 and COL2 to these Arduino
pins.
Servo myservo; //We create our own name (or called "object" in programming) to use for
calling servo commands
int pos = 0; //Assigning a variable for storing the value of the servo position from 0 to
180 degrees
void setup() {
lcd.begin(16, 2); //To start using the LCD, we first need to set up the LCD’s number of
columns and rows:
//Thus, putting (16 columns and 2 rows) using the "lcd.begin(x,y)" command as
we have 16x2 lcd.
myservo.attach(13); //This command tells the program the pin where we have attached our
servo motor so that it gives corresponding signals
//to that pin.
delay(2000); //Giving a delay of 2 sec so that the message is displayed for reasonable
time on the lcd.
}
void loop()
{
EnterPassword(); //Calling the EnterPassword function which is repeated in the loop till
the arduino is powered.
}
//This function is for the 180 degree rotation of the servo(door opening), staying there for 10 sec and
then again returning back to its original position.
void DoorOpen()
{
//using a "for" loop function to count from 0 to 180 degrees as the servo turns with a 1 degree per
step.
for (pos = 0; pos <= 180; pos += 1)
{
myservo.write(pos); //telling the servo to go to position in variable ’pos’(which is initialized
at 0).
delay(10); //waits 10ms for the servo to reach the position
}
delay(10000); //Giving a delay of 10 sec so that the servo remains at 180 degrees(door is
open) for 10 sec.
//using another "for" loop to reverse the spin of the servo from 180 to 0 degrees(to close the door).
for (pos = 180; pos >= 0; pos -= 1)
{
myservo.write(pos); //telling servo to go to position in variable ’pos’.
delay(10); //waits 10ms for the servo to reach the position
}
}
char saved_password[4] = {'0','0','0','0'}; //Creating a character array of length 4 store the saved
password.
char entered_password[4] = {' ',' ',' ',' '}; //Creating a character array of length 4 store the entered
password.
int entered_pass_digit = 0; //This variable is to keep count of the digits the user has
pressed.
//This function tkes in user input and matches it with the correct password and does the required in both
the cases where the password matches or does not match.
void EnterPassword()
{
char key = kpd.getKey(); //This character variable saves the digit the user has pressed
every time. This will only store
//the current key pressed as this variable is created and destroyed with
avery iteration of the loop where this
//function is called.
if (entered_pass_digit < 4) //Keeps taking the input until 4 digits are pressed.
{
lcd.setCursor(0, 0); //setting the cursor to column 0, line 0
lcd.print("Enter Password:"); //Printing the message "Enter Password" in the first line
ShowEnteredPassword(); //Calling this function to display the entered password in the
second row of lcd.
//Once the comparison is over reset the digit counter back to 0 and also emptying the entered
password array by storing spaces.
//(which cannot be the user input).
entered_pass_digit = 0;
entered_password[0] = ' ';
entered_password[1] = ' ';
entered_password[2] = ' ';
entered_password[3] = ' ';
}
}
//This function is for the functionality of changing the password and set a new user given password.
void ChangePassword()
{
char key = kpd.getKey();
//Until and unless '*' key or '#' key is pressed the program stays in the while loop.
while (key != '*' && key != '#')
{
key = kpd.getKey(); //Checking what the input is from the user.
//The following lines displays the message on the lcd regarding how to change the password.
//('*' for changing the password and '#' for not).
lcd.setCursor(0, 0);
lcd.print("Change Password?");
lcd.setCursor(0, 1);
lcd.print("YES(*) or No(#)");
}
int count = 0; //This variable keeps count of the digits the user has pressed during
password change.
lcd.clear(); //Clears the lcd of previous text.
while (count < 4) //Until and unless four digits has been pressed it remains in while
loop.
{
lcd.setCursor(0, 0); //Displaying the appropriate message on the first line.
lcd.print("New Password:");
char key = kpd.getKey(); //Saving the key user has pressed in this variable.
//This function is for displaying the entered password on the second row of the lcd.
void ShowEnteredPassword()
{
//Setting the cursor on the start of second line and then displaying one by one all members of the
array. The column increments by itself when
//a new byte is printed.
lcd.setCursor(0, 1);
lcd.print(entered_password[0]);
lcd.print(entered_password[1]);
lcd.print(entered_password[2]);
lcd.print(entered_password[3]);
}
//This function is for displaying the saved password on the second row of the lcd.
void ShowSavedPassword()
{
//Setting the cursor on the start of second line and then displaying one by one all members of the
array. The column increments by itself when
//a new byte is printed.
lcd.setCursor(0, 1);
lcd.print(saved_password[0]);
lcd.print(saved_password[1]);
lcd.print(saved_password[2]);
lcd.print(saved_password[3]);
}