pasword

Dependencies:   Keypadlatest TextLCD

main.cpp

Committer:
mijimy
Date:
2017-11-16
Revision:
0:819b4e0e0ae6

File content as of revision 0:819b4e0e0ae6:


#include "mbed.h"
#include "Keypad.h"
#include "TextLCD.h"
#include "string.h"
TextLCD display(D8, D9, D4, D5, D6, D7); // rs, e, d4-d7

char kpdLayout[4][4] = {{'1' ,'2' ,'3' ,'A'},  //row0
                        {'4' ,'5' ,'6' ,'B'},  //row1
                        {'7' ,'8' ,'9' ,'C'},  //row2
                        {'*' ,'0' ,'#' ,'D'}}; //row3

Keypad kpad(PB_14, PB_15, PB_1, PB_2, PB_11, PB_12, PA_11, PA_12);
char password[20];
const char code1[]="12345678";
int main() {
     char key;
     int released = 1,count=0;
     display.locate(0,0);
     display.printf("insert password");
     while(1){
         key = kpad.ReadKey();                   //read the current key pressed
         if(key == '\0')
             released = 1;                       //set the flag when all keys are released            
         if((key != '\0') && (released == 1))
         {  //if a key is pressed AND previous key was released
             if(key!='#')
             {  password[count]=key;    // add pasword
                password[count+1]='\0';  // put end of password
                count++;   //counter
                if(count>8)  //max count
                    count=0;
                display.cls();
                display.locate(0,0);
                display.printf("insert password");
                display.locate(2,1);
                display.printf("pass:%s",password);
              }
              else if(key=='#')
              { bool accept=1;
                for(int i=0;password[i]!='\0';i++)
                {   if(password[i]!=code1[i])
                    {   accept=0;
                        break;
                    }
                }
                display.cls();
                if(accept==0)
                {    display.locate(0,0);
                    display.printf("wrong password ");
                }
                else
                {    display.locate(0,0);
                    display.printf("accept password");
                }
                count=0; // reset password counter
              }
              
                   
            
             released = 0;                       //clear the flag to indicate that key is still pressed
         }
     }
}