Embedded System Design Assignement
Dependencies: Keypad Servo TextLCD mbed
main.cpp
00001 #include "mbed.h" // Library for mbed 00002 #include "Servo.h" // Library for servo motor 00003 #include "Keypad.h" // Library for 4x4 matrix keypad 00004 #include "TextLCD.h" // Library for 16x2 LCD 00005 #include <string> // Library for string class 00006 00007 Servo myservo(PTB3); // Signal pin of servo motor attach to pin PTB3 00008 DigitalIn IR_sensor(PTA12); // IR obstacle sensor connect to digital pin PTA12 00009 DigitalOut LED(PTD4); // Green LED connect to digital pin PTD4 00010 DigitalOut LCD(PTC12); // Cathode pin of LCD connect to PTC12 00011 00012 // Keypad connection (col 0, col1, col2, col3, row0, row1, row2, row3) 00013 Keypad keypad(PTC9,PTC8,PTA5,PTA4,PTD2,PTD0,PTD5,PTA13); 00014 // LCD connection ( RS, E, D4, D5, D6, D7) 00015 TextLCD lcd(PTE20, PTE21, PTE22, PTE23, PTE29, PTE30, TextLCD::LCD16x2); 00016 00017 char pass_set[4] = {'1','2','3','4'}; // Predefined password 00018 char password[4] ; // String used to store password input 00019 00020 int main() { // Main function 00021 char key, old_key; // Declare char variables key and old_key 00022 int i=0,j=0; // Declare integer variables i and j and initialize to 0 00023 int released = 1; // Declare integer variable ‘released’ used to indicate whether the key is pressed 00024 int ret = 2; // Declare integer variable ‘ret’ used for comparison of predefined password and input password 00025 myservo = 0.0; // Initialize the position of servo motor to 0 (locked) 00026 lcd.cls(); // Clear the LCD 00027 00028 while(IR_sensor == 1){ // No obstacle detected, IR_sensor returns 1 00029 LED = 0; // Green LED turns off 00030 LCD = 1; // Backlight of LCD turns off 00031 } 00032 // While loop is break once the IR_sensor returns 0 which means obstacle is detected 00033 LED = 1; // Green LED turns on 00034 LCD = 0; // Backlight of LCD turns on 00035 lcd.printf("Welcome Home!"); // Print welcome message 00036 lcd.locate(0,1); // Print message on second line 00037 lcd.printf("Enter Password."); // Ask user to enter password 00038 wait(2); // Wait for 2 seconds 00039 lcd.cls(); 00040 lcd.printf("Password Entered"); 00041 00042 while(1) { // Infinite loop 00043 key = keypad.ReadKey(); // Read the current key pressed from the keypad 00044 00045 if (key == '\0'){ // If no key is pressed or all the keys are released 00046 released = 1;} // Set the released 00047 00048 if ((key != '\0') && (released == 1)){ //If a key is pressed and previous key is released 00049 if (key != old_key){ // If the key pressed is different as the previous key 00050 password[i++] = key; // Store the input key in password string 00051 lcd.locate(j++,1); // Print the key pressed on LCD 00052 lcd.printf("%c",key); 00053 old_key = key; // Store the latest key pressed on old_key 00054 released = 0;} // Clear the flag indicate the key is still pressed 00055 } 00056 00057 if (i == 4){ // If the number of keys pressed is equal to 4 00058 // Compare the input password with predefined password 00059 // 4 is the number of character to be compared 00060 ret = strncmp(password, pass_set, 4); 00061 wait(0.5); // Wait for 0.5 seconds 00062 } 00063 00064 if (ret ==0){ // The input password matches the predefined password 00065 lcd.cls(); // Print message on LCD 00066 lcd.printf("Password Correct"); 00067 lcd.locate(0,1); 00068 lcd.printf("Access Allowed."); 00069 myservo = 1.0; // Rotate the servo motor to unlock the door 00070 wait(2); // Wait for 2 seconds 00071 main(); // Jump back to main function to start everything again 00072 } 00073 else if (ret ==1){ // The input password does not matches the predefined password 00074 lcd.cls(); // Print message on LCD 00075 lcd.printf("Wrong Password."); 00076 lcd.locate(0,1); 00077 lcd.printf("Access Denied."); 00078 wait(2); // Wait for 2 seconds 00079 main(); // Jump back to main function to start everything again 00080 } 00081 } 00082 }
Generated on Tue Jul 12 2022 23:52:05 by 1.7.2