4x4 keypad multiuser multi-password system with outputs

Dependencies:   mbed TextLCD Keypad

Committer:
coryburke
Date:
Fri Dec 07 21:26:00 2018 +0000
Revision:
3:f9378196889c
Parent:
2:30db7d0108c1
MICRO_FINAL_PRJ_CBURKE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
coryburke 3:f9378196889c 1
habusaq 0:93cb8707aa8c 2 #include "mbed.h"
habusaq 0:93cb8707aa8c 3 #include "Keypad.h"
habusaq 0:93cb8707aa8c 4 #include "TextLCD.h"
coryburke 2:30db7d0108c1 5
coryburke 2:30db7d0108c1 6 //pin selection for keypad matrix
coryburke 2:30db7d0108c1 7
coryburke 3:f9378196889c 8 Keypad kpad(PTE2,PTE3,PTE4,PTE5,PTB8,PTB9,PTB10,PTB11);
coryburke 2:30db7d0108c1 9
coryburke 3:f9378196889c 10 // all digital inputs
coryburke 3:f9378196889c 11
coryburke 2:30db7d0108c1 12 /*
coryburke 3:f9378196889c 13 Keypad kpad(col1, col2, col3, col4, row1, row2, row3, row4)
habusaq 0:93cb8707aa8c 14 */
habusaq 0:93cb8707aa8c 15
coryburke 2:30db7d0108c1 16 // serial connection to pc for debugging and troubleshooting
coryburke 2:30db7d0108c1 17 Serial pc(USBTX, USBRX);
coryburke 2:30db7d0108c1 18
coryburke 2:30db7d0108c1 19 //define passwords of for individual users
coryburke 2:30db7d0108c1 20 const uint32_t number_of_chars = 5; //password length
coryburke 3:f9378196889c 21 const char pass_user1[number_of_chars] = {'1','2','3','4','*'}; //password user 1
coryburke 3:f9378196889c 22 const char pass_user2[number_of_chars] = {'1','4','7','4','*'}; // password user 2
coryburke 3:f9378196889c 23 const char pass_user3[number_of_chars] = {'0','0','0','0','0'}; // password user 3
coryburke 3:f9378196889c 24 const char pass_user4[number_of_chars] = {'6','5','4','C','*'}; // password user 4
coryburke 3:f9378196889c 25 const char pass_user5[number_of_chars] = {'A','A','A','A','A'}; // password user 5
habusaq 0:93cb8707aa8c 26
coryburke 2:30db7d0108c1 27 //choose the character length of valid passwords
coryburke 3:f9378196889c 28 char enterd_pass[number_of_chars] = {' ',' ',' ',' ',' '};
coryburke 3:f9378196889c 29
coryburke 3:f9378196889c 30 // each (‘ ‘) represents a location for inputs from keypad
habusaq 0:93cb8707aa8c 31
coryburke 2:30db7d0108c1 32
coryburke 2:30db7d0108c1 33 //initialize outputs
coryburke 3:f9378196889c 34 DigitalOut led_red(LED_RED); //red led turned on
coryburke 3:f9378196889c 35 DigitalOut led_green(LED_GREEN); //green led turned on
coryburke 3:f9378196889c 36 DigitalOut output2(PTA4); // normally off digital output
coryburke 3:f9378196889c 37 DigitalOut output1(PTA5); // normally on digital output
coryburke 3:f9378196889c 38
coryburke 3:f9378196889c 39
coryburke 2:30db7d0108c1 40 PwmOut beep(PTD4); // buzzer
habusaq 0:93cb8707aa8c 41
habusaq 0:93cb8707aa8c 42 int main()
habusaq 0:93cb8707aa8c 43 {
habusaq 0:93cb8707aa8c 44 char key;
habusaq 0:93cb8707aa8c 45 int i;
habusaq 0:93cb8707aa8c 46 int user_index1=0;
habusaq 0:93cb8707aa8c 47 int user_index2=0;
habusaq 0:93cb8707aa8c 48 int user_index3=0;
habusaq 0:93cb8707aa8c 49 int user_index4=0;
habusaq 0:93cb8707aa8c 50 int user_index5=0;
coryburke 3:f9378196889c 51 int j=0; // incorrect entry counter
coryburke 3:f9378196889c 52 int flag=0; //lockout flag
habusaq 0:93cb8707aa8c 53
coryburke 2:30db7d0108c1 54 printf("\n \n PASSWORD REQUIRED \n \n ");
habusaq 0:93cb8707aa8c 55
coryburke 3:f9378196889c 56 int released =1; // variable to detect if a key is being pressed on keypad
coryburke 2:30db7d0108c1 57
coryburke 3:f9378196889c 58 output1=0; // digital output
coryburke 2:30db7d0108c1 59 printf("\r\n Password Entered:");
coryburke 3:f9378196889c 60 led_green = 0; //LED green on
coryburke 3:f9378196889c 61 led_red = 0; //LED red on
coryburke 3:f9378196889c 62 //red and green led turned on to create yellow
coryburke 3:f9378196889c 63
coryburke 3:f9378196889c 64 output2 = 1; // buzzer alarm off
coryburke 3:f9378196889c 65
coryburke 3:f9378196889c 66 //buzzer period select,
coryburke 3:f9378196889c 67 beep.period(0.001); // determines pitch and sound frequency
coryburke 3:f9378196889c 68
coryburke 3:f9378196889c 69 wait(0.001); //off time
coryburke 3:f9378196889c 70
coryburke 3:f9378196889c 71
coryburke 3:f9378196889c 72
habusaq 0:93cb8707aa8c 73
coryburke 3:f9378196889c 74
coryburke 3:f9378196889c 75
coryburke 3:f9378196889c 76
coryburke 3:f9378196889c 77
coryburke 3:f9378196889c 78
coryburke 3:f9378196889c 79
coryburke 3:f9378196889c 80
coryburke 3:f9378196889c 81
habusaq 0:93cb8707aa8c 82
coryburke 3:f9378196889c 83
coryburke 3:f9378196889c 84
coryburke 3:f9378196889c 85
coryburke 3:f9378196889c 86
coryburke 3:f9378196889c 87 ////beginning of do-while loop to activate system lockout after too many login attemps
coryburke 2:30db7d0108c1 88 do{
coryburke 3:f9378196889c 89
coryburke 2:30db7d0108c1 90 while(1) {
coryburke 3:f9378196889c 91
coryburke 2:30db7d0108c1 92 led_green = 0;
coryburke 2:30db7d0108c1 93 led_red = 0;
coryburke 2:30db7d0108c1 94 printf("Enter Password:");
habusaq 0:93cb8707aa8c 95
habusaq 0:93cb8707aa8c 96 // Reading the password characters
habusaq 0:93cb8707aa8c 97 for(i=0; i<number_of_chars; i++) {
coryburke 3:f9378196889c 98
coryburke 3:f9378196889c 99 key = kpad.ReadKey(); // for loop reads each character entered up to
coryburke 3:f9378196889c 100 // number of chars in a password
coryburke 3:f9378196889c 101
coryburke 3:f9378196889c 102
coryburke 3:f9378196889c 103 if(key == '\0') // if no character is being typed
coryburke 3:f9378196889c 104 released = 1; //set the flag when all keys are released
coryburke 3:f9378196889c 105
coryburke 3:f9378196889c 106 if((key != '\0') && (released == 1)) //if key pressed AND previous key released
coryburke 3:f9378196889c 107 {
coryburke 3:f9378196889c 108
coryburke 3:f9378196889c 109 enterd_pass[i]=key;
coryburke 3:f9378196889c 110 printf("%c ",enterd_pass[i]);
coryburke 3:f9378196889c 111 released = 0; //flag to indicate that key is pressed
habusaq 0:93cb8707aa8c 112 }
habusaq 0:93cb8707aa8c 113 else
habusaq 0:93cb8707aa8c 114 i--;
habusaq 0:93cb8707aa8c 115
habusaq 0:93cb8707aa8c 116
habusaq 0:93cb8707aa8c 117
habusaq 0:93cb8707aa8c 118 }
habusaq 0:93cb8707aa8c 119
coryburke 2:30db7d0108c1 120 wait(0.003);
coryburke 3:f9378196889c 121
coryburke 3:f9378196889c 122 //// prompts entered passcode and is compared to preset user passcodes
coryburke 3:f9378196889c 123 //// comparing password entered via keypad to valid user passwords
habusaq 0:93cb8707aa8c 124
habusaq 0:93cb8707aa8c 125 ///////// user 1 ///////////////
habusaq 0:93cb8707aa8c 126 for(i=0; i<number_of_chars; i++) {
habusaq 0:93cb8707aa8c 127 if( enterd_pass[i] == pass_user1[i] ) {
habusaq 0:93cb8707aa8c 128 user_index1 = 1;
coryburke 3:f9378196889c 129 output2 = 1;
habusaq 0:93cb8707aa8c 130 } else {
habusaq 0:93cb8707aa8c 131 user_index1 = 0;
habusaq 0:93cb8707aa8c 132 break;
habusaq 0:93cb8707aa8c 133 }
habusaq 0:93cb8707aa8c 134 }
habusaq 0:93cb8707aa8c 135
habusaq 0:93cb8707aa8c 136
habusaq 0:93cb8707aa8c 137
habusaq 0:93cb8707aa8c 138 ///////// user 2 ///////////////
habusaq 0:93cb8707aa8c 139 for(i=0; i<number_of_chars; i++) {
habusaq 0:93cb8707aa8c 140 if( enterd_pass[i] == pass_user2[i] ) {
habusaq 0:93cb8707aa8c 141 user_index2 = 2;
habusaq 0:93cb8707aa8c 142 } else {
habusaq 0:93cb8707aa8c 143 user_index2 = 0;
habusaq 0:93cb8707aa8c 144 break;
habusaq 0:93cb8707aa8c 145 }
habusaq 0:93cb8707aa8c 146 }
habusaq 0:93cb8707aa8c 147 ///////// user 3 ///////////////
habusaq 0:93cb8707aa8c 148 for(i=0; i<number_of_chars; i++) {
habusaq 0:93cb8707aa8c 149 if( enterd_pass[i] == pass_user3[i] ) {
habusaq 0:93cb8707aa8c 150 user_index3 = 3;
habusaq 0:93cb8707aa8c 151 } else {
habusaq 0:93cb8707aa8c 152 user_index3 = 0;
habusaq 0:93cb8707aa8c 153 break;
habusaq 0:93cb8707aa8c 154 }
habusaq 0:93cb8707aa8c 155 }
habusaq 0:93cb8707aa8c 156 ///////// user 4 ///////////////
habusaq 0:93cb8707aa8c 157 for(i=0; i<number_of_chars; i++) {
habusaq 0:93cb8707aa8c 158 if( enterd_pass[i] == pass_user4[i] ) {
habusaq 0:93cb8707aa8c 159 user_index4 = 4;
habusaq 0:93cb8707aa8c 160 } else {
habusaq 0:93cb8707aa8c 161 user_index4 = 0;
habusaq 0:93cb8707aa8c 162 break;
habusaq 0:93cb8707aa8c 163 }
habusaq 0:93cb8707aa8c 164 }
habusaq 0:93cb8707aa8c 165 ///////// user 5 ///////////////
habusaq 0:93cb8707aa8c 166 for(i=0; i<number_of_chars; i++) {
habusaq 0:93cb8707aa8c 167 if( enterd_pass[i] == pass_user5[i] ) {
habusaq 0:93cb8707aa8c 168 user_index5 = 5;
habusaq 0:93cb8707aa8c 169 } else {
habusaq 0:93cb8707aa8c 170 user_index5 = 0;
habusaq 0:93cb8707aa8c 171 break;
habusaq 0:93cb8707aa8c 172 }
habusaq 0:93cb8707aa8c 173 }
coryburke 2:30db7d0108c1 174
coryburke 3:f9378196889c 175 // if passcode matches a user passcode
coryburke 3:f9378196889c 176 // continue to grant user accress and activate outputs
coryburke 2:30db7d0108c1 177
coryburke 3:f9378196889c 178 ////// correct password prompts and outputs //////
coryburke 3:f9378196889c 179
coryburke 3:f9378196889c 180 if( (1 == user_index1)||(2 == user_index2)||(3 == user_index3)||(4 == user_index4)||(5 == user_index5) )
coryburke 3:f9378196889c 181
coryburke 3:f9378196889c 182 // if user index is between 1 and 5 and not 0
coryburke 3:f9378196889c 183 {
coryburke 3:f9378196889c 184 printf("\r\n Passcode recognized \n \n "); // correct passcode prompt
coryburke 3:f9378196889c 185 led_red = 1; // red led has 0V, leaving green led on
coryburke 3:f9378196889c 186 output2 = 0; // activates short buzzer
coryburke 3:f9378196889c 187 output1=1; // activates short buzzer
coryburke 2:30db7d0108c1 188 printf("\r\n Welcome Cory Burke \n \n" );
coryburke 2:30db7d0108c1 189
coryburke 3:f9378196889c 190 output1=1; // output 1 activated
habusaq 0:93cb8707aa8c 191
habusaq 0:93cb8707aa8c 192 if(1 == user_index1) {
coryburke 3:f9378196889c 193 printf("\r Access granted User 1 \n \n"); // User 1’s name can be entered her
coryburke 3:f9378196889c 194 led_red = 1; //red led turned off
coryburke 3:f9378196889c 195 output2 = 1; // activates short buzzer
coryburke 2:30db7d0108c1 196 printf("\r\n Welcome User 1 \n" );
coryburke 2:30db7d0108c1 197 printf("\r User numer: 1 \n");
coryburke 2:30db7d0108c1 198 output1=0;
habusaq 0:93cb8707aa8c 199 }
habusaq 0:93cb8707aa8c 200 if(2 == user_index2) {
coryburke 2:30db7d0108c1 201 printf("\r\n Access granted: User 2 \n \n ");
coryburke 3:f9378196889c 202 led_red = 1; //red led turned off
coryburke 3:f9378196889c 203 output2 = 1; // activates short buzzer
coryburke 2:30db7d0108c1 204 printf("\r\n Welcome User 2 \n" );
coryburke 2:30db7d0108c1 205 printf("\r User numer: 2 \n");
coryburke 3:f9378196889c 206 output1=0; // output1 turned off
habusaq 0:93cb8707aa8c 207 }
habusaq 0:93cb8707aa8c 208 if(3 == user_index3) {
coryburke 2:30db7d0108c1 209 printf("\r\n Access granted: User 3 \n ");
coryburke 3:f9378196889c 210 led_red = 1; //red led turned off
coryburke 3:f9378196889c 211 output2 = 1; // activates short buzzer
coryburke 2:30db7d0108c1 212 printf("\r\n Welcome User 3 \n" );
coryburke 2:30db7d0108c1 213 printf("\r User numer: 3 \n ");
coryburke 3:f9378196889c 214 output1=0; // output1 turned off
habusaq 0:93cb8707aa8c 215 }
habusaq 0:93cb8707aa8c 216 if(4 == user_index4) {
coryburke 2:30db7d0108c1 217 printf("\r\n Access granted: User 4 \n ");
coryburke 3:f9378196889c 218 led_red = 1; //red led turned off
coryburke 3:f9378196889c 219 output2 = 1; // activates short buzzer
coryburke 2:30db7d0108c1 220 printf("\r\n Welcome User 4 \n" );
coryburke 2:30db7d0108c1 221 printf("\r User numer: 4 \n");
coryburke 3:f9378196889c 222 output1=0; // output1 turned off
habusaq 0:93cb8707aa8c 223 }
habusaq 0:93cb8707aa8c 224 if(5 == user_index5) {
coryburke 2:30db7d0108c1 225 printf("\r Access granted: User 5 \n ");
coryburke 3:f9378196889c 226 led_red = 1; //red led turned off
coryburke 3:f9378196889c 227 output2 = 1; // activates short buzzer
coryburke 2:30db7d0108c1 228 printf("\r\n Welcome User 5 \n " );
coryburke 2:30db7d0108c1 229 printf("\r User numer: 5 \n");
coryburke 3:f9378196889c 230 output1=0; // output1 turned off
habusaq 0:93cb8707aa8c 231 }
habusaq 0:93cb8707aa8c 232
habusaq 0:93cb8707aa8c 233 wait(1);
coryburke 3:f9378196889c 234 output2 = 0;
coryburke 3:f9378196889c 235 beep = 25.0/100.0; // beep duty cycle to control volume and pitch
coryburke 3:f9378196889c 236 led_red = 0; //
coryburke 3:f9378196889c 237 led_green=0; //red and green led turned on to create yellow
coryburke 3:f9378196889c 238 output1=1; // output1 turned off
habusaq 0:93cb8707aa8c 239 wait(1);
coryburke 2:30db7d0108c1 240
habusaq 0:93cb8707aa8c 241
habusaq 0:93cb8707aa8c 242
habusaq 0:93cb8707aa8c 243 }
coryburke 3:f9378196889c 244 ///// when an incorrect password was entered
habusaq 0:93cb8707aa8c 245 else {
coryburke 2:30db7d0108c1 246 printf("\r\n \n Incorrect password \n \n Access denied! \n \n ");
coryburke 3:f9378196889c 247 led_green = 1; //green led turned off
coryburke 3:f9378196889c 248 led_red=0; //red led turned on
coryburke 2:30db7d0108c1 249 wait(1);
coryburke 3:f9378196889c 250 output2 = 0; // buzzer off
coryburke 2:30db7d0108c1 251 beep = 50.0/100.0;
coryburke 3:f9378196889c 252 j=j+1; // records access attempt
coryburke 3:f9378196889c 253 //// too many attempts made
coryburke 3:f9378196889c 254
coryburke 2:30db7d0108c1 255 if(j==4){printf("\r\n \n TOO MANT ATTEMPTS, SYSTEM LOCKOUT \n \n ");
coryburke 3:f9378196889c 256 led_green = 1; //green led turned off
coryburke 3:f9378196889c 257 led_red=0; //red led turned on
coryburke 3:f9378196889c 258 flag=1; //lockout flag activated
habusaq 0:93cb8707aa8c 259 wait(1);
coryburke 3:f9378196889c 260 output2 = 1; // long buzzer when system lockout occurs
coryburke 3:f9378196889c 261 beep = 50.0/100.0;
coryburke 3:f9378196889c 262 output1=0; // output1 turned on
coryburke 2:30db7d0108c1 263 wait(1);
coryburke 2:30db7d0108c1 264 output1=0;
coryburke 2:30db7d0108c1 265 wait(1);
coryburke 2:30db7d0108c1 266 output1=1;
coryburke 2:30db7d0108c1 267 wait(1);
coryburke 2:30db7d0108c1 268 output1=0;
coryburke 2:30db7d0108c1 269 wait(1);
coryburke 2:30db7d0108c1 270 output1=1;
coryburke 3:f9378196889c 271 wait(1); //long buzzer ends
coryburke 2:30db7d0108c1 272 }
coryburke 2:30db7d0108c1 273
coryburke 2:30db7d0108c1 274 }
habusaq 1:5461144fd540 275
coryburke 3:f9378196889c 276 beep = 0.0/100.0; // buzzer turned off by duty cycle = 0
coryburke 3:f9378196889c 277 led_green = 0; //green led turned on
coryburke 3:f9378196889c 278 led_red=0; //red led turned on
coryburke 3:f9378196889c 279 //red and green led turned on to create yellow
coryburke 3:f9378196889c 280 output2 = 0;
coryburke 3:f9378196889c 281 output1=1;
coryburke 3:f9378196889c 282
habusaq 0:93cb8707aa8c 283 }
coryburke 2:30db7d0108c1 284
coryburke 2:30db7d0108c1 285
coryburke 2:30db7d0108c1 286
coryburke 2:30db7d0108c1 287
coryburke 2:30db7d0108c1 288
coryburke 3:f9378196889c 289 } while(flag==0); ////end of do-while loop, continues until flag =1 from too many attempts
coryburke 3:f9378196889c 290
coryburke 2:30db7d0108c1 291
coryburke 2:30db7d0108c1 292
coryburke 3:f9378196889c 293 ////end of program
coryburke 2:30db7d0108c1 294
coryburke 2:30db7d0108c1 295
habusaq 0:93cb8707aa8c 296 }
habusaq 0:93cb8707aa8c 297
coryburke 3:f9378196889c 298
coryburke 3:f9378196889c 299
coryburke 3:f9378196889c 300
coryburke 3:f9378196889c 301
coryburke 3:f9378196889c 302
coryburke 3:f9378196889c 303
coryburke 3:f9378196889c 304
coryburke 3:f9378196889c 305
coryburke 3:f9378196889c 306
coryburke 3:f9378196889c 307
coryburke 3:f9378196889c 308
coryburke 3:f9378196889c 309
coryburke 3:f9378196889c 310
coryburke 3:f9378196889c 311
coryburke 3:f9378196889c 312
coryburke 3:f9378196889c 313
coryburke 3:f9378196889c 314
coryburke 3:f9378196889c 315
coryburke 3:f9378196889c 316
coryburke 3:f9378196889c 317
coryburke 3:f9378196889c 318
coryburke 3:f9378196889c 319
coryburke 3:f9378196889c 320
coryburke 3:f9378196889c 321
coryburke 3:f9378196889c 322
coryburke 3:f9378196889c 323
coryburke 3:f9378196889c 324
coryburke 3:f9378196889c 325