Marco Belliard / Mbed 2 deprecated LCD_RFID_Key_KeyPad_Emic

Dependencies:   mbed ID12RFID TextLCD keypad

main.cpp

Committer:
mabelliard
Date:
2021-11-28
Revision:
1:43e6a97f7d4d
Parent:
0:b79709bfa1e2
Child:
2:bcdd700feee0

File content as of revision 1:43e6a97f7d4d:

#include "mbed.h"
#include "TextLCD.h"
#include "ID12RFID.h"
#include "Keypad.h"
#include "emic2.h"


emic2 myTTS(p13, p14); //serial RX,TX pins to emic
Serial pc(USBTX, USBRX);
ID12RFID rfid(p27); // uart rx
TextLCD lcd(p15, p16, p21, p22, p23, p24); //rs,e,d0,d1,d2,d3
DigitalIn PhysicalKey(p5);
Keypad keypad(p7, p12, p11, p9, p8, p6, p10, NC);

// Define your own keypad values
char Keytable[] = {'1', '2', '3',   // r0
                   '4', '5', '6',   // r1
                   '7', '8', '9',   // r2
                   '*', '0', '#'}; // r3
                  // c0   c1   c2
                    
uint32_t Password[4]={1, 2, 3, 4};
uint32_t PasswordTry[4];
uint32_t Index;
 
uint32_t cbAfterInput(uint32_t index) {
     Index = index;
     return 0;
}

//Entry Verification Levels
DigitalOut CardAuthorized(LED1); //Authorized Card Notification
DigitalOut PinAuthorized(LED2); //Authorized Pin Notification
DigitalOut QuestionAuthorized(LED3); //Approved Secuirty Question
DigitalOut DoorUnlock(LED4); //Approved Entry

//Stored integers to manipulate security information
int Attempts = 0; //Counts how many times Timer ResetEvertyhing has been triggered, and failures
int CardDATA; //initializes the rfid.read() function early so theres no delay to display the Data
int Z;
int UserIdentifier = 0;
int CorrectNumbersCounter = 0;
int CorrectNumbersCounter1 = 0;
int SongCounter = 0;
int Card[10]={22352713, 22374742};
int SongEE[4]= {12, 12, 12, 12};
int CorrectCard = 0;
char Question[2][64]= {"What is your social security number",
                          "How many dicks can fit in your ass"};
uint32_t QuestionAnswers[2][4]={ {5, 5, 5, 5}, 
                                {6, 6, 6, 6} };
void ResetLocks(void);
void ResetLocks(void){
    CardAuthorized = 0;
    PinAuthorized = 0;
    QuestionAuthorized = 0;
    DoorUnlock = 0;
    CorrectCard = 0;
    CorrectNumbersCounter = 0;
    CorrectNumbersCounter1 = 0;
    UserIdentifier = 0;  
    SongCounter = 0; 
}
int main(){
    myTTS.volume(18); //max volume
    keypad.attach(&cbAfterInput);
    keypad.start();  // energize the keypad via c0-c3
    int run_once1 = 0;
    int run_once2 = 0;
    int run_once3 = 0;
    myTTS.voice(2);
    while(1){
        while(PhysicalKey == 0) {   //key either not inserted and/or turned
            if (run_once1 == 0){
                ResetLocks();
                lcd.cls();
                pc.printf("Please Insert Physical Key\n\r");
                lcd.printf("Please Insert   Physical Key \n");
                myTTS.speakf("S");//Speak command starts with "S"
                myTTS.speakf("Please Insert Physical Key");  // Send the desired string to convert to speech
                myTTS.speakf("\r"); //marks end of speak command
                myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
                run_once1 = 1;         
            }
        }
        while(PhysicalKey == 1) { //key inserted and turned
            run_once1 = 0;
            if (run_once2 == 0){
                lcd.cls();
                pc.printf("Please Scan Card\n\r");
                lcd.printf("Please Scan Card \n");
                myTTS.speakf("S");//Speak command starts with "S"
                myTTS.speakf("Please Scan Card");  // Send the desired string to convert to speech
                myTTS.speakf("\r"); //marks end of speak command
                myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
                run_once2 = 1;         
            }
            else if (Attempts > 3) {
                lcd.cls();
                pc.printf("Too many Attempts\n\r");
                lcd.printf("Too many        Attempts\n");
                myTTS.speakf("S");//Speak command starts with "S"
                myTTS.speakf("Too many Attempts");  // Send the desired string to convert to speech
                myTTS.speakf("\r"); //marks end of speak command
                myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
                lcd.cls();
                pc.printf("LOCKED for 30 Minutes\n\r");
                lcd.printf("LOCKED for 30   Minutes\n");
                myTTS.speakf("S");//Speak command starts with "S"
                myTTS.speakf("Locked for 30 Minutes");  // Send the desired string to convert to speech
                myTTS.speakf("\r"); //marks end of speak command
                myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
                
                wait(1800);
    
                Attempts = 0;
            }
            else if((rfid.readable())&&(Attempts < 4)) {
                lcd.cls();
                CardDATA = rfid.read();
                pc.printf("RFID Tag Number:%d\n\r",CardDATA);
                lcd.printf("RFID Tag Number:%d\n",CardDATA);
                wait(1);
                for(Z = 0; Z<100; Z++){
                    if(CardDATA==Card[Z]){
                        UserIdentifier = Z;
                        CorrectCard = CorrectCard + 1;
                            //pc.printf("Counter :%d\r\n", CorrectNumbersCounter); 
                    }
                }
                if(CorrectCard == 0){
                    lcd.cls();
                    lcd.printf("Fuck You\n"); 
                    pc.printf("Fuck You\n\r"); 
                    myTTS.speakf("S");//Speak command starts with "S"
                    myTTS.speakf("Fuck You");  // Send the desired string to convert to speech
                    myTTS.speakf("\r"); //marks end of speak command
                    myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
                    wait(2);
                } 
                else if(CorrectCard == 1){
                    CorrectCard = 0;
                    lcd.cls();
                    lcd.printf("Card Accepted\n");  
                    pc.printf("Card Accepted\n\r");  
                    wait(2);
                    CardAuthorized = 1;
                    Attempts = 0;
                    ////////////////////Beginning of Keypad Section
                    while ((CardAuthorized == 1)&& (PhysicalKey == 1)) {
                        lcd.cls();
                        lcd.printf("Type Pin: ");
                        pc.printf("Type Pin: ");
                        myTTS.speakf("S");//Speak command starts with "S"
                        myTTS.speakf("Type Common Pin");  // Send the desired string to convert to speech
                        myTTS.speakf("\r"); //marks end of speak command
                        myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
                        for(int A=0; A<4; A++){ 
                            __wfi();
                            PasswordTry[A]= Index + 1;
                            lcd.printf("%c", Keytable[Index]);
                            pc.printf("%c", Keytable[Index]);   
                            wait(0.3);
                        }
                        for(int B=0; B<4; B++){
                            if(PasswordTry[B]==Password[B]){
                                CorrectNumbersCounter = CorrectNumbersCounter + 1;
                                }
                        }
                        for(int Song=0; Song<4; Song++){
                            if(PasswordTry[Song]==SongEE[Song]){
                                SongCounter = SongCounter + 1;
                            }
                        }
                        if(SongCounter == 4){
                            lcd.cls();
                            lcd.printf("Easter Egg ON\n");
                            pc.printf("\n\rEaster Egg ON\n\r");  
                            myTTS.speakf("D1\r");//Sing Song Demo
                            myTTS.ready(); //member function wait
                            SongCounter = 0;
                            }
                        else if(CorrectNumbersCounter == 4){
                            SongCounter = 0;
                            lcd.cls();
                            lcd.printf("Pin Accepted\n");
                            pc.printf("\n\rPin Accepted\n\r");  
                            wait(2);
                            PinAuthorized = 1;
                            Attempts = 0;
                        }
                        else if (CorrectNumbersCounter < 4){
                            SongCounter= 0;
                            CorrectNumbersCounter = 0;
                            lcd.cls();
                            lcd.printf("FAIL"); 
                            pc.printf("FAIL");
                            myTTS.speakf("S");//Speak command starts with "S"
                            myTTS.speakf("Incorrect Entry");  // Send the desired string to convert to speech
                            myTTS.speakf("\r"); //marks end of speak command
                            myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
                            Attempts = Attempts + 1;
                            if(Attempts == 4){
                                ResetLocks();
                            } 
                        }
                        while ((PinAuthorized == 1)&&(PhysicalKey == 1)) {
                        //security question
                            lcd.cls();
                            lcd.printf("Security\n"); 
                            lcd.printf("Question\n"); 
                            pc.printf("Security Question\n\r");                             
                            myTTS.speakf("S");//Speak command starts with "S"
                            myTTS.speakf("Security Question");  // Send the desired string to convert to speech
                            myTTS.speakf("\r"); //marks end of speak command
                            myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
                            lcd.cls();
                            //lcd.printf("%s\n", Question[UserIdentifier]); 
                            pc.printf("%s\n\r", Question[UserIdentifier]);                            
                            myTTS.speakf("S");//Speak command starts with "S"
                            myTTS.speakf("%s\r", Question[UserIdentifier]) ; //desired string to convert to speech
                            myTTS.speakf("\r"); //marks end of speak command
                            myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
                            lcd.cls();
                            lcd.printf("Type Answer: \n"); 
                            pc.printf("Type Answer: \n\r"); 
                            for(int C=0; C<4; C++){ 
                                __wfi();
                                PasswordTry[C]= Index + 1;
                                pc.printf("%d", PasswordTry[C]);
                                lcd.printf("%d", PasswordTry[C]);  
                                wait(0.5);
                            }
                            for(int D=0; D<4; D++){
                                if(QuestionAnswers[UserIdentifier][D]==PasswordTry[D]){
                                    CorrectNumbersCounter1 = CorrectNumbersCounter1 + 1;
                                }
                            }
                            if(CorrectNumbersCounter1 == 4){
                                lcd.cls();
                                lcd.printf("Answered\n");
                                lcd.printf("Accepted\n");
                                pc.printf("\n\rAnswer Accepted\n\r"); 
                                myTTS.speakf("S");//Speak command starts with "S"
                                myTTS.speakf("Answer Accepted");  // Send the desired string to convert to speech
                                myTTS.speakf("\r"); //marks end of speak command
                                myTTS.ready(); //ready waits for speech to finish from last command with a ":" respo
                                QuestionAuthorized = 1;
                                Attempts = 0;
                            }
                            else if(CorrectNumbersCounter1 <4) {
                            CorrectNumbersCounter1 = 0;
                                lcd.cls();
                                lcd.printf("FAIL"); 
                                pc.printf("FAIL");
                                myTTS.speakf("S");//Speak command starts with "S"
                                myTTS.speakf("Incorrect Entry");  // Send the desired string to convert to speech
                                myTTS.speakf("\r"); //marks end of speak command
                                myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
                                Attempts = Attempts + 1;
                                if(Attempts == 4){
                                    ResetLocks();
                                } 
                            }                            
            
                            while((QuestionAuthorized == 1)){
                                if(PhysicalKey == 1){
                                    if (run_once3 == 0){
                                        lcd.cls();
                                        pc.printf("Please Remove Key\n\r");
                                        lcd.printf("Please Remove   Key\n");
                                        myTTS.speakf("S");//Speak command starts with "S"
                                        myTTS.speakf("Please Remove Key");  // Send the desired string to convert to speech
                                        myTTS.speakf("\r"); //marks end of speak command
                                        myTTS.ready(); //ready waits for speech to finish from last command with a ":" respo
                                        run_once3 = 1;
                                    }
                                }
                                else if(PhysicalKey == 1){
                                    run_once3 = 0;
                                    lcd.cls();
                                    pc.printf("Key Removed\n\r");
                                    lcd.printf("Key Removed\n");
                                    wait(2);
                                    DoorUnlock = 1;
                                    myTTS.speakf("S");//Speak command starts with "S"
                                    myTTS.speakf("Seconds until closed");  // Send the desired string to convert to speech
                                    myTTS.speakf("\r"); //marks end of speak command
                                    myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
                                    for(int Y=5; Y>0; --Y){
                                        lcd.cls();
                                        pc.printf("Door Open For %d Seconds\n\r",Y);
                                        lcd.printf("Door Open For:\n");
                                        lcd.printf("%d Seconds\n",Y);
                                        myTTS.speakf("S");//Speak command starts with "S"
                                        myTTS.speakf("%d",Y);  // Send the desired string to convert to speech
                                        myTTS.speakf("\r"); //marks end of speak command
                                        myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
                                    }
                                    ResetLocks();
                                    lcd.cls();
                                    pc.printf("Door Locked\n\r");
                                    lcd.printf("Door Locked\n");
                                    myTTS.speakf("S");//Speak command starts with "S"
                                    myTTS.speakf("Door Locked");  // Send the desired string to convert to speech
                                    myTTS.speakf("\r"); //marks end of speak command
                                    myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
                                    wait(1);
                                    Attempts = 0;  
                                }
                            }
                        }
                    }
                } 
                } 
                else if (run_once2 == 0){
                    lcd.cls();
                    pc.printf("Please Scan Card\n\r");
                    lcd.printf("Please Scan Card\n");
                    myTTS.speakf("S");//Speak command starts with "S"
                    myTTS.speakf("Please Scan Card");  // Send the desired string to convert to speech
                    myTTS.speakf("\r"); //marks end of speak command
                    myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
                    run_once2 = 1;         
                }               
        if(PhysicalKey == 0){
            run_once2 = 0;
        }
        }//Second While Loop Braket
    }// While(1)
}//Main Braket