4x4 keypad multiuser multi-password system with outputs
Dependencies: mbed TextLCD Keypad
main.cpp
00001 00002 #include "mbed.h" 00003 #include "Keypad.h" 00004 #include "TextLCD.h" 00005 00006 //pin selection for keypad matrix 00007 00008 Keypad kpad(PTE2,PTE3,PTE4,PTE5,PTB8,PTB9,PTB10,PTB11); 00009 00010 // all digital inputs 00011 00012 /* 00013 Keypad kpad(col1, col2, col3, col4, row1, row2, row3, row4) 00014 */ 00015 00016 // serial connection to pc for debugging and troubleshooting 00017 Serial pc(USBTX, USBRX); 00018 00019 //define passwords of for individual users 00020 const uint32_t number_of_chars = 5; //password length 00021 const char pass_user1[number_of_chars] = {'1','2','3','4','*'}; //password user 1 00022 const char pass_user2[number_of_chars] = {'1','4','7','4','*'}; // password user 2 00023 const char pass_user3[number_of_chars] = {'0','0','0','0','0'}; // password user 3 00024 const char pass_user4[number_of_chars] = {'6','5','4','C','*'}; // password user 4 00025 const char pass_user5[number_of_chars] = {'A','A','A','A','A'}; // password user 5 00026 00027 //choose the character length of valid passwords 00028 char enterd_pass[number_of_chars] = {' ',' ',' ',' ',' '}; 00029 00030 // each (‘ ‘) represents a location for inputs from keypad 00031 00032 00033 //initialize outputs 00034 DigitalOut led_red(LED_RED); //red led turned on 00035 DigitalOut led_green(LED_GREEN); //green led turned on 00036 DigitalOut output2(PTA4); // normally off digital output 00037 DigitalOut output1(PTA5); // normally on digital output 00038 00039 00040 PwmOut beep(PTD4); // buzzer 00041 00042 int main() 00043 { 00044 char key; 00045 int i; 00046 int user_index1=0; 00047 int user_index2=0; 00048 int user_index3=0; 00049 int user_index4=0; 00050 int user_index5=0; 00051 int j=0; // incorrect entry counter 00052 int flag=0; //lockout flag 00053 00054 printf("\n \n PASSWORD REQUIRED \n \n "); 00055 00056 int released =1; // variable to detect if a key is being pressed on keypad 00057 00058 output1=0; // digital output 00059 printf("\r\n Password Entered:"); 00060 led_green = 0; //LED green on 00061 led_red = 0; //LED red on 00062 //red and green led turned on to create yellow 00063 00064 output2 = 1; // buzzer alarm off 00065 00066 //buzzer period select, 00067 beep.period(0.001); // determines pitch and sound frequency 00068 00069 wait(0.001); //off time 00070 00071 00072 00073 00074 00075 00076 00077 00078 00079 00080 00081 00082 00083 00084 00085 00086 00087 ////beginning of do-while loop to activate system lockout after too many login attemps 00088 do{ 00089 00090 while(1) { 00091 00092 led_green = 0; 00093 led_red = 0; 00094 printf("Enter Password:"); 00095 00096 // Reading the password characters 00097 for(i=0; i<number_of_chars; i++) { 00098 00099 key = kpad.ReadKey(); // for loop reads each character entered up to 00100 // number of chars in a password 00101 00102 00103 if(key == '\0') // if no character is being typed 00104 released = 1; //set the flag when all keys are released 00105 00106 if((key != '\0') && (released == 1)) //if key pressed AND previous key released 00107 { 00108 00109 enterd_pass[i]=key; 00110 printf("%c ",enterd_pass[i]); 00111 released = 0; //flag to indicate that key is pressed 00112 } 00113 else 00114 i--; 00115 00116 00117 00118 } 00119 00120 wait(0.003); 00121 00122 //// prompts entered passcode and is compared to preset user passcodes 00123 //// comparing password entered via keypad to valid user passwords 00124 00125 ///////// user 1 /////////////// 00126 for(i=0; i<number_of_chars; i++) { 00127 if( enterd_pass[i] == pass_user1[i] ) { 00128 user_index1 = 1; 00129 output2 = 1; 00130 } else { 00131 user_index1 = 0; 00132 break; 00133 } 00134 } 00135 00136 00137 00138 ///////// user 2 /////////////// 00139 for(i=0; i<number_of_chars; i++) { 00140 if( enterd_pass[i] == pass_user2[i] ) { 00141 user_index2 = 2; 00142 } else { 00143 user_index2 = 0; 00144 break; 00145 } 00146 } 00147 ///////// user 3 /////////////// 00148 for(i=0; i<number_of_chars; i++) { 00149 if( enterd_pass[i] == pass_user3[i] ) { 00150 user_index3 = 3; 00151 } else { 00152 user_index3 = 0; 00153 break; 00154 } 00155 } 00156 ///////// user 4 /////////////// 00157 for(i=0; i<number_of_chars; i++) { 00158 if( enterd_pass[i] == pass_user4[i] ) { 00159 user_index4 = 4; 00160 } else { 00161 user_index4 = 0; 00162 break; 00163 } 00164 } 00165 ///////// user 5 /////////////// 00166 for(i=0; i<number_of_chars; i++) { 00167 if( enterd_pass[i] == pass_user5[i] ) { 00168 user_index5 = 5; 00169 } else { 00170 user_index5 = 0; 00171 break; 00172 } 00173 } 00174 00175 // if passcode matches a user passcode 00176 // continue to grant user accress and activate outputs 00177 00178 ////// correct password prompts and outputs ////// 00179 00180 if( (1 == user_index1)||(2 == user_index2)||(3 == user_index3)||(4 == user_index4)||(5 == user_index5) ) 00181 00182 // if user index is between 1 and 5 and not 0 00183 { 00184 printf("\r\n Passcode recognized \n \n "); // correct passcode prompt 00185 led_red = 1; // red led has 0V, leaving green led on 00186 output2 = 0; // activates short buzzer 00187 output1=1; // activates short buzzer 00188 printf("\r\n Welcome Cory Burke \n \n" ); 00189 00190 output1=1; // output 1 activated 00191 00192 if(1 == user_index1) { 00193 printf("\r Access granted User 1 \n \n"); // User 1’s name can be entered her 00194 led_red = 1; //red led turned off 00195 output2 = 1; // activates short buzzer 00196 printf("\r\n Welcome User 1 \n" ); 00197 printf("\r User numer: 1 \n"); 00198 output1=0; 00199 } 00200 if(2 == user_index2) { 00201 printf("\r\n Access granted: User 2 \n \n "); 00202 led_red = 1; //red led turned off 00203 output2 = 1; // activates short buzzer 00204 printf("\r\n Welcome User 2 \n" ); 00205 printf("\r User numer: 2 \n"); 00206 output1=0; // output1 turned off 00207 } 00208 if(3 == user_index3) { 00209 printf("\r\n Access granted: User 3 \n "); 00210 led_red = 1; //red led turned off 00211 output2 = 1; // activates short buzzer 00212 printf("\r\n Welcome User 3 \n" ); 00213 printf("\r User numer: 3 \n "); 00214 output1=0; // output1 turned off 00215 } 00216 if(4 == user_index4) { 00217 printf("\r\n Access granted: User 4 \n "); 00218 led_red = 1; //red led turned off 00219 output2 = 1; // activates short buzzer 00220 printf("\r\n Welcome User 4 \n" ); 00221 printf("\r User numer: 4 \n"); 00222 output1=0; // output1 turned off 00223 } 00224 if(5 == user_index5) { 00225 printf("\r Access granted: User 5 \n "); 00226 led_red = 1; //red led turned off 00227 output2 = 1; // activates short buzzer 00228 printf("\r\n Welcome User 5 \n " ); 00229 printf("\r User numer: 5 \n"); 00230 output1=0; // output1 turned off 00231 } 00232 00233 wait(1); 00234 output2 = 0; 00235 beep = 25.0/100.0; // beep duty cycle to control volume and pitch 00236 led_red = 0; // 00237 led_green=0; //red and green led turned on to create yellow 00238 output1=1; // output1 turned off 00239 wait(1); 00240 00241 00242 00243 } 00244 ///// when an incorrect password was entered 00245 else { 00246 printf("\r\n \n Incorrect password \n \n Access denied! \n \n "); 00247 led_green = 1; //green led turned off 00248 led_red=0; //red led turned on 00249 wait(1); 00250 output2 = 0; // buzzer off 00251 beep = 50.0/100.0; 00252 j=j+1; // records access attempt 00253 //// too many attempts made 00254 00255 if(j==4){printf("\r\n \n TOO MANT ATTEMPTS, SYSTEM LOCKOUT \n \n "); 00256 led_green = 1; //green led turned off 00257 led_red=0; //red led turned on 00258 flag=1; //lockout flag activated 00259 wait(1); 00260 output2 = 1; // long buzzer when system lockout occurs 00261 beep = 50.0/100.0; 00262 output1=0; // output1 turned on 00263 wait(1); 00264 output1=0; 00265 wait(1); 00266 output1=1; 00267 wait(1); 00268 output1=0; 00269 wait(1); 00270 output1=1; 00271 wait(1); //long buzzer ends 00272 } 00273 00274 } 00275 00276 beep = 0.0/100.0; // buzzer turned off by duty cycle = 0 00277 led_green = 0; //green led turned on 00278 led_red=0; //red led turned on 00279 //red and green led turned on to create yellow 00280 output2 = 0; 00281 output1=1; 00282 00283 } 00284 00285 00286 00287 00288 00289 } while(flag==0); ////end of do-while loop, continues until flag =1 from too many attempts 00290 00291 00292 00293 ////end of program 00294 00295 00296 } 00297 00298 00299 00300 00301 00302 00303 00304 00305 00306 00307 00308 00309 00310 00311 00312 00313 00314 00315 00316 00317 00318 00319 00320 00321 00322 00323 00324 00325
Generated on Sat Jul 16 2022 03:25:43 by 1.7.2