1

hey guys I have a access control system working perfect with rfid and keypad.

I would like to implement : If the PIN is entered more than 3 times incorrectly the card is disabled.

someone could give a help or a tip?

here is my code: https://pastiebin.com/5e4c315d97d5e

#include <Keypad.h>                 // This sketch uses the Keypad.h library
#include <SPI.h>                    // This sketch uses the SPI.h library
#include <MFRC522.h>                // This sketch uses the MFRC522.h library

#define SS_PIN 10                   // Define SS_PIN of the RC522 RFID Reader to digital pin 10 of the Arduino
#define RST_PIN 9                   // Define RST_PIN of the RC522 RFID Reader to digital pin 9 of the Arduino
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance. 

int RedLed    = A3;                 // Red LED output is analog pin A3
int GreenLed  = A4;                 // Green LED output is analog pin A4
int Buzzer    = A5;                 // Buzzer output is analog pin A5
unsigned long Timer;                // Variable for the Timer



const byte ROWS = 4;                // Four rows
const byte COLS = 3;                // Three columns


char Keys[ROWS][COLS] = {           // Define the symbols on the buttons of the keypads
  {'1','2','3',},
  {'4','5','6',},
  {'7','8','9',},
  {'*','0','#',}
};

byte rowPins[ROWS] = {5, 4, 3, 2};  // Connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6};     // Connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS); // Initialize an instance of class NewKeypad

int RightCard;                      // Variable for a known Mifare Card
int RightPinCode;                   // Variable for valid key input in combination with a known Mifare Card
int WrongPinCode;                   // Variable for a invalid key input in combination with a known Mifare Card
int PinCodeCounter;                 // Variable Counter for counting the number of pincode inputs on the keypad

int Code1Correct;                   // Variable for the first correct digit (Code) input on the keypad
int Code2Correct;                   // Variable for the second correct digit (Code) input on the keypad
int Code3Correct;                   // Variable for the third correct digit (Code) input on the keypad
int Code4Correct;                   // Variable for the fourth correct digit (Code) input on the keypad
int Code5Correct;                   // Variable for the fifth correct digit (Code) input on the keypad
int Code6Correct;                   // Variable for the sixth correct digit (Code) input on the keypad
int Reset;                          // Reset for the loop

int MifareCard1;                               // We need this specific variable to determine the valid PinCode in combination with the UID nummer of the Mifare Card
const int Code1MifareCard1 = '6';              // Determine here the first digit of your 6 digit PIN
const int Code2MifareCard1 = '6';              // Determine here the second digit of your 6 digit PIN
const int Code3MifareCard1 = '6';              // Determine here the third digit of your 6 digit PIN
const int Code4MifareCard1 = '2';              // Determine here the fourth digit of your 6 digit PIN
const int Code5MifareCard1 = '2';              // Determine here the fifth digit of your 6 digit PIN
const int Code6MifareCard1 = '2';              // Determine here the sixth digit of your 6 digit PIN

int MifareCard2;                               // We need this specific variable to determine the valid PinCode in combination with the UID nummer of the Mifare Card
const int Code1MifareCard2 = '1';              // Determine here the first digit of your 6 digit PIN
const int Code2MifareCard2 = '1';              // Determine here the second digit of your 6 digit PIN
const int Code3MifareCard2 = '1';              // Determine here the third digit of your 6 digit PIN
const int Code4MifareCard2 = '3';              // Determine here the fourth digit of your 6 digit PIN
const int Code5MifareCard2 = '3';              // Determine here the fifth digit of your 6 digit PIN
const int Code6MifareCard2 = '3';              // Determine here the sixth digit of your 6 digit PIN





void setup() 

{  
  Serial.begin(9600);                                     // Initialize serial communications with the PC
  SPI.begin();                                            // Initialize the SPI bus
  mfrc522.PCD_Init();                                     // Initialize the MFRC522 
  pinMode (RedLed, OUTPUT);                               // Define RedLed as OUTPUT
  pinMode (GreenLed, OUTPUT);                             // Define GreenLed as OUTPUT
  pinMode (Buzzer, OUTPUT);                               // Define Buzzer as OUTPUT
}





void loop() {   


//----------------------------------------------Code for the reset function of the loop--------------------------------------------------------------------------------------------------------


if (Reset == 1)                                           // If Reset has value 1 (HIGH) all variables will be reset to 0 and the RC522 RFID reader will be waiting for a new Mifare Card.
{                                                         
  RightCard = 0;
  MifareCard1 = 0; 
  MifareCard2 = 0;                                      
  RightPinCode = 0;
  WrongPinCode = 0;
  Code1Correct = 0;
  Code2Correct = 0;
  Code3Correct = 0;
  Code4Correct = 0;
  Code5Correct = 0;
  Code6Correct = 0;
  PinCodeCounter = 0;  
  delay (50);
  Reset = 0;
}


//----------------------------------------------Code for the control for a valid pincode within the timelimit of 7 seconds after presenting/offering a valid Mifare Card ---------------------------


if (millis()- Timer > 7000 && RightCard == 1)            // If the Timer will be larger as 7 seconds and a valid Mifare Card is offered, reset value will be high.
   {                                                     // This means that you have after presenting a valid card, 7 seconds to enter a valid code or the loop resets.
    Reset = 1;
    Serial.println("CardAccesOff");
   }




//----------------------------------------------Reading the presented/offered Mifare Card at the MFRC522 ----------------------------------------------------------------------------------------- 


if   (mfrc522.PICC_IsNewCardPresent() &&                
      mfrc522.PICC_ReadCardSerial())
    { 


//----------------------------------------------Code for detect the first Mifare Card in the Database. This is a UID number with 4 pairs of digits. Example: 69 7C 01 9C------------------------------------
     if                                                  
     (mfrc522.uid.uidByte[0] == 0x69   &&                // Fill in the first digits of the UID number of your Mifare card
      mfrc522.uid.uidByte[1] == 0x7C   &&                // Fill in the seconds digits of the UID number of your Mifare card              
      mfrc522.uid.uidByte[2] == 0x01   &&                // Fill in the third digits of the UID number of your Mifare card
      mfrc522.uid.uidByte[3] == 0x9C)                    // Fill in the fourth digits of the UID number of your Mifare card

     {
      RightCard = 1;                                     // The offered Mifare Card is known in the database, set variable "RightCard" to 1
      MifareCard1 = 1;                                   // This Mifare Card is known as Mifare Card 2 in the Database, so set variable MifareCard2 to value 1. 
      digitalWrite (Buzzer, HIGH);                       // Make a beep for the accepted Mifare Card             
      delay (150);                                       //
      digitalWrite (Buzzer, LOW);                        //

      PinCodeCounter = 0;                                // Reset the PinCodeCounter to 0
      Timer =  millis();                                 // Reset the Timer. The 7 seconds limit is running now for a valid PinCode
      Serial.println("CardAccesOn");                     // Print the text "CardAccesOn" to the serial monitor 
      delay (200);                                       // Wait 200 milliseconds
      }


//-----------------------------------------------Code for detect the second Mifare Card in the Database. This is a UID number with 4 pairs of digits. Example: EB 70 C0 BC----------------------------------------------
     if                                                    
     (mfrc522.uid.uidByte[0] == 0xEB   &&                 // Fill in the first digits of the UID number of your Mifare card
      mfrc522.uid.uidByte[1] == 0x70   &&                 // Fill in the seconds digits of the UID number of your Mifare card              
      mfrc522.uid.uidByte[2] == 0xC0   &&                 // Fill in the third digits of the UID number of your Mifare card
      mfrc522.uid.uidByte[3] == 0xBC)                     // Fill in the fourth digits of the UID number of your Mifare card

    {     
      RightCard = 1;                                       // If the offered Mifare Card is known in the database, set variable "RightCard" to 1
      MifareCard2 = 1;
      digitalWrite (Buzzer, HIGH);                         // Make a beep for the accepted Mifare Card             
      delay (150);                                         //
      digitalWrite (Buzzer, LOW);                          //

      PinCodeCounter = 0;                                  // Reset the PinCodeCounter to 0
      Timer =  millis();                                   // Reset the Timer. The 7 seconds limit is running now for a valid PinCode
      Serial.println("CardAccesOn");                       // Print the text "CardAccesOn" to the serial monitor 
      delay (200);                                         // Wait 200 milliseconds
    } 

    }   

//-----------------------------------------------Code for accepting a valid MifareCard + valid PinCode-------------------------------------------------------------------------------------------------



if (Code6Correct == 1 && RightCard == 1)                    // If the PinCode is the correct and you did enter this within 7 seconds
  {
    RightPinCode = 1;                                       // Variable RightPinCode will be set to 1 

    digitalWrite (GreenLed, HIGH);                          // Make a nice beep for the correct Pincode and set the Green LED high for 1 second
    delay (150);                                            // 
    digitalWrite (Buzzer, HIGH);                            //
    delay (150);                                            //
    digitalWrite (Buzzer, LOW);                             //
    delay (50);                                             //
    digitalWrite (Buzzer, HIGH);                            //
    delay (150);                                            //
    digitalWrite (Buzzer, LOW);                             //
    delay (500);                                            //          
    digitalWrite (GreenLed, LOW);                           //                          

    Serial.println("Correct PinCode");                      // Print the text "Correct PinCode" to the serial monitor              
    Reset = 1;                                              // Reset the loop
  }

//-----------------------------------------------Code for refusing a valid MifareCard + invalid PinCode------------------------------------------------------------------------------------------------



if ((Code6Correct == 0) && (PinCodeCounter >= 6) && (RightCard == 1))       // If you offered a valid Mifare Card and the Pincode is not correct and you already entered 6 digitkeys 
  {  
    WrongPinCode = 1;                                                       // Variable WrongPinCode will be set to 1
    Serial.println("WrongCode");                                             // Print the text "WrongKey" to the serial monitor
    Reset = 1;                                                              // Reset the loop
  } 


//-----------------------------------------------Code for refusing a Wrong/Invalid PinCode or the Timer (7seconds) expired----------------------------------------------------------------------------



if ((WrongPinCode == 1) || (millis()- Timer > 7000 && RightCard == 1))      // If you offered a valid Mifare Card and  you entered a wrong PinCode or the timer (7 seconds) expires
  { 
    digitalWrite (Buzzer, HIGH);                                            // Make a long beep and set the Red LED HIGH for the wrong code or timer expiry
    digitalWrite (RedLed, HIGH);                                           //
    delay(1500);                                                            //
    digitalWrite (Buzzer, LOW);                                             //
    digitalWrite (RedLed, LOW);                                             //

    Serial.println("WrongCode or Timer expired");                           // Print the text "WrongCode or Timer expired" to the serial monitor                      
    Reset = 1;                                                              // Reset the loop
  }


//-----------------------------------------------Code for counting the inputs on the keypad----------------------------------------------------------------------------------------------------------



char KeyDigit = keypad.getKey();                                           // Get the DigitKey from the keypad

if ((RightCard == 1) &&                                                    // If you offered a valid Mifare Card and any DigitKey on the keypad is pressed
    ((KeyDigit == '1') || 
    (KeyDigit == '2')  || 
    (KeyDigit == '3')  || 
    (KeyDigit == '4')  || 
    (KeyDigit == '5')  || 
    (KeyDigit == '6')  || 
    (KeyDigit == '7')  || 
    (KeyDigit == '8')  || 
    (KeyDigit == '9')  || 
    (KeyDigit == '0')  || 
    (KeyDigit == '*')  || 
    (KeyDigit == '#')))

    {                               
      PinCodeCounter++;                                                       // PinCodeCounter value +1 for every press on any Digitkey on the keypad
      digitalWrite (Buzzer, HIGH);                                            // Make a short beep for a DigitKey press on the keypad
      delay (50);                                                             //
      digitalWrite (Buzzer, LOW);                                             //
    }





//-----------------------------------------------Code for determine the correct PinCode for MifareCard1----------------------------------------------------------------------------------------------------------





if ((KeyDigit == Code1MifareCard1) && (RightCard == 1) && (Code1Correct == 0) && (MifareCard1 == 1))           // If you offered the valid MifareCard1, first PinCode and you did not entered it before (Otherwise we conflict with same KeyDigits)
    {
      Code1Correct = 1;                                                                                        // Variable Code1Correct is set to 1
      return;                                                                                                  // Return to begin loop
    } 

if ((KeyDigit == Code2MifareCard1) && (Code1Correct == 1) && (Code2Correct == 0) && (MifareCard1 == 1))        // If you offered the valid MifareCard1, second PinCode and you did not entered it before (Otherwise we conflict with same KeyDigits)
    {
      Code2Correct = 1;                                                                                        // Variable Code2Correct is set to 1
      return;                                                                                                  // Return to begin loop
    } 

if ((KeyDigit == Code3MifareCard1) && (Code2Correct == 1) && (Code3Correct == 0) && (MifareCard1 == 1))         // If you offered the valid MifareCard1, third PinCode and you did not entered it before (Otherwise we conflict with same KeyDigits)
    {
      Code3Correct = 1;                                                                                         // Variable Code3Correct is set to 1
      return;                                                                                                   // Return to begin loop
    } 

if ((KeyDigit == Code4MifareCard1) && (Code3Correct == 1) && (Code4Correct == 0) && (MifareCard1 == 1))         // If you offered the valid MifareCard1, fourth PinCode and you did not entered it before (Otherwise we conflict with same KeyDigits)
    {
      Code4Correct = 1;                                                                                         // Variable Code4Correct is set to 1
      return;                                                                                                   // Return to begin loop
    }  
if ((KeyDigit == Code5MifareCard1) && (Code4Correct == 1) && (Code5Correct == 0) && (MifareCard1 == 1))         // If you offered the valid MifareCard1, fifth PinCode and you did not entered it before (Otherwise we conflict with same KeyDigits)
    {
      Code5Correct = 1;                                                                                         // Variable Code5Correct is set to 1
      return;                                                                                                   // Return to begin loop
    } 

if ((KeyDigit == Code6MifareCard1) && (Code5Correct == 1) && (Code6Correct == 0) && (MifareCard1 == 1))         // If you offered the valid MifareCard1, sixth PinCode and you did not entered it before (Otherwise we conflict with same KeyDigits)
    {
      Code6Correct = 1;                                                                                         // Variable Code6Correct is set to 1
      return;                                                                                                   // Return to begin loop
    }




//-----------------------------------------------Code for determine the correct PinCode for MifareCard2----------------------------------------------------------------------------------------------------------





if ((KeyDigit == Code1MifareCard2) && (RightCard == 1) && (Code1Correct == 0) && (MifareCard2 == 1))              // If you offered a the valid MifareCard2, first PinCode and you did not entered it before (Otherwise we conflict with same KeyDigits)
    {
      Code1Correct = 1;                                                                                           // Variable Code1Correct is set to 1
      return;                                                                                                     // Return to begin loop
    } 

if ((KeyDigit == Code2MifareCard2) && (Code1Correct == 1) && (Code2Correct == 0) && (MifareCard2 == 1))           // If you offered a the valid MifareCard2, second PinCode and you did not entered it before (Otherwise we conflict with same KeyDigits)
    {
      Code2Correct = 1;                                                                                           // Variable Code2Correct is set to 1
      return;                                                                                                     // Return to begin loop
    } 

if ((KeyDigit == Code3MifareCard2) && (Code2Correct == 1) && (Code3Correct == 0) && (MifareCard2 == 1))           // If you offered a the valid MifareCard2, third PinCode and you did not entered it before (Otherwise we conflict with same KeyDigits)
    {
      Code3Correct = 1;                                                                                           // Variable Code3Correct is set to 1
      return;                                                                                                     // Return to begin loop
    } 

if ((KeyDigit == Code4MifareCard2) && (Code3Correct == 1) && (Code4Correct == 0) && (MifareCard2 == 1))          // If you offered a the valid MifareCard2, fourth PinCode and you did not entered it before (Otherwise we conflict with same KeyDigits)
    {
      Code4Correct = 1;                                                                                          // Variable Code4Correct is set to 1
      return;                                                                                                    // Return to begin loop
    }  
if ((KeyDigit == Code5MifareCard2) && (Code4Correct == 1) &&  (Code5Correct == 0) && (MifareCard2 == 1))         // If you offered a the valid MifareCard2, fifth PinCode and you did not entered it before (Otherwise we conflict with same KeyDigits)
    {
      Code5Correct = 1;                                                                                          // Variable Code5Correct is set to 1
      return;                                                                                                    // Return to begin loop
    } 

if ((KeyDigit == Code6MifareCard2) && (Code5Correct == 1) &&  (Code6Correct == 0) && (MifareCard2 == 1))         // If you offered a the valid MifareCard2, sixth PinCode and you did not entered it before (Otherwise we conflict with same KeyDigits)
    {
      Code6Correct = 1;                                                                                          // Variable Code6Correct is set to 1
      return;                                                                                                    // Return to begin loop
    } 

}

thank you very much.

2

Add int passCount = '0'; to the top of your file and then increment that value by one every time the wrong password gets entered (For example replace Reset = 1; with passCount ++; on line 199) And add passCount = '0' to your reset on line 85. And add another if statement to reset loop if password has been entered more than 3 times. if (passCount == 3) {Reset = 1;}

Hope this helps :-)

EDIT:

You can comment out/delete the If statement on lines 222 - 232 if ((WrongPinCode == 1) || (millis()- Timer > 7000 && RightCard == 1)).

And add your Buzzer and Led code to the if statement above (same statement with passCount ++; in it) The timeout reset will be take care of code on lines 108 - 112.

4
  • did not work :(
    – raduken
    Feb 24 '20 at 18:55
  • 2
    @raduken The idea of David seems correct to me. But: I would use int passCount = 0; instead of const int passCount = '0';. The const makes the variable immutable and '0' is not the digit zero but the character zero so you start the count with '0' = 48. Likewise you should reset the variable with passCount = 0 instead of passCount = '0'. Feb 29 '20 at 15:33
  • did not work, my code gets in a loop and never stops :(, could you help me? thank you
    – raduken
    Mar 1 '20 at 8:10
  • 1
    @raduken Add some outputs at strategic places. Follow the logic and the contents of variables with them. Invest some thinking, try to understand. This is one kind of debugging. Mar 2 '20 at 7:37
1
+50

Your code written more complex, than it has to be, and you have much repeated code, that makes it harder to understand and change the code. So I will first describe, how to shorten and generalize your code. That will make it easier to implement new features, like invalidating a card after 3 wrong PIN entries.

  • Handling the RFID cards: Currently you are using single variables for each card (for example RightCard). Your if statements all have to reference these single variables for each card, which makes it hard to not write repeated code. Instead you should use arrays to store the information. See this piece of code:

    #define MAX_CARD_N        2
    #define SERIAL_LENGTH     4
    uint8_t card_serials[MAX_CARD_N][SERIAL_LENGTH] = {
        {0x69, 0x7C, 0x01, 0x9C},
        {0xEB, 0x70, 0xC0, 0xBC}
    };
    
    #define PIN_LENGTH        6
    char card_pins[MAX_CARD_N][PIN_LENGTH] = {
        {'6','6','6','2','2','2'},
        {'1','1','1','3','3','3'}
    };
    
    int current_card = -1;
    uint8_t current_pin_position = 0;
    bool pin_correct = true;
    

    What happens here? First we set defines for later use, where we can centrally set the number of cards, that we want to use, and the length of their serial data. Then we declare a 2 dimensional array, which stores the serial for each card (for example in a loop). We can now access the serials of the cards by simple integer indices. Then we do the same with the PINs of these cards. (And declare variables for holding the current card index, the current pin position and the correctness of the pin for later use.) We can implement a function, that will give us the index of the currently scanned card in our card arrays:

    int find_card(){
        if(mfrc522.uid.uidByte[0] == 0x00) return -1;
        for(uint8_t card_index=0; card_index < MAX_CARD_N; card_index++){
            uint8_t found_card_flag = 1;
            for(uint8_t serial_index=0; serial_index < SERIAL_LENGTH; serial_index++){
                if(card_serials[card_index][serial_index] != mfrc522.uid.uidByte[serial_index]){
                    found_card_flag = 0;
                    break;
                }
           }
           if(found_card_flag){
               return card_index;
           }
       }
       return -1;
    }
    

    This function will return -1, if the scanned card serial can not be found in our card_serials array. If it was found, the function returns the index of the card. I also added an if statement at the start, that forbids to find cards with the first element being zero, which will come in handy at a later time (I only used the first element here for making it easy, but you can also check every of the 4 serial bytes, if you want to forbid zeros everywhere in the serial).

  • Organizing your code: Currently you have many individual if statements, which all need extra variables, so that you can execute the right code at the right situation. A better approach for something like this is a Finite State Machine (FSM). It is an important concept, that sound more difficult, than it is. Basically you divide your code into different states, where specific things should happen. Actions or specific situations can do a transition to a different state. Every state only executes it's own code. The current state is held in a variable, called state variable. In my answer to this question I already explained the principle in detail (including some graphs). You should really learn this principle; it changed my coding style completely for better.

    I would use 2 different states: WAIT_FOR_CARD and WAIT_FOR_PIN, so that we can seperate the RFID and the keypad code. Look at this piece of code:

    enum State {WAIT_FOR_CARD, WAIT_FOR_PIN};
    State state = WAIT_FOR_CARD;
    
    void loop() {
        switch(state){
          case WAIT_FOR_CARD:
              if(mfrc522.PICC_IsNewCardPresent() &&                
                 mfrc522.PICC_ReadCardSerial()){
                  int scanned_card = find_card();
                  if(scanned_card != -1){
                      current_card = scanned_card;
                      current_pin_position = 0;
                      code_correct = true;
                      // Insert code for correct card in here (buzzer, setting timestamp, ...)
                      state = WAIT_FOR_PIN;
                  } else {
                      // Insert code for incorrect card in here
                  }
              }
              break;
          case WAIT_FOR_PIN:
              char KeyDigit = keypad.getKey();
              if(KeyDigit != NO_KEY){
                if(KeyDigit != card_pins[current_card][current_pin_position]) pin_correct = false;
                current_pin_position++;
                if(current_pin_position >= PIN_LENGTH){
                    if(pin_correct){
                        // Insert code for correct pin in here
                    } else {
                        // Insert code for wrong pin in here
                    }
                    current_card = -1;
                    state = WAIT_FOR_CARD;
                }
              }
              break;
        }
    }
    

    We first define our states as an enum, since this makes the code more readable. Then we declare a state variable from that enum. In the loop() function we use a switch case statement to execute the code corresponding to the current state. In the WAIT_FOR_CARD state, we are constantly scanning for a new card. If we found one, we check with the find_card() function, if we have the serial of this card saved in our array. If yes, we set our variables accordingly, execute the needed code for a valid card and then set the state variable to the next state. The break will get us out of the switch statement, but on the next loop iteration, we are now executing the other case (since we changed the state variable). We are now constantly checking for a pressed key. If the pressed key was not the one defined in our PIN array for the current position, we set pin_correct to false (If every key is correct, it will stay true. If any is incorrect, it will get set to false). We increment the PIN position and then check if we reached the end of the PIN, where we decide, if the PIN was correct or not, doing the appropriate things, and at the end reset our current_card variable and the state variable. (I left out the timeout feature. When you have understood the FSM principle, it should be easy to add this to the code.)

    This code is better structured and way shorter. Also it can easily handle extensions (more cards, other functions,...). You can add more states and transitions to insert new features.

  • Disabling cards on 3 wrong pin entries: I assume, that you want, that 3 incorrect PIN entries will disable the RFID card, so that it is no longer accepted as valid. For that you could remove the corresponding serial from the card_serials array. First we need to track the number of consecutive wrong PIN entries. For that I would use another array and a define for the max number of wrong entries (here 2, since 2 wrong entries are still allowed; the third wrong entry will trigger the invalidation of the card):

    uint8_t pin_strikes[MAX_CARD_N] = {0};
    #define MAX_STRIKES    2
    

    When we have a wrong PIN, you can increment this counter by 1:

    pin_strikes[current_card]++;
    

    When we have a correct PIN, we want to reset this counter:

    pin_strikes[current_card] = 0;
    

    After incrementing in the case of a wrong PIN we can check, how many times the pin was already wrong consecutively, and then remove the serial, if it was too much:

    if(pin_strikes[current_card] > MAX_STRIKES){
        for(uint8_t i=0; i < SERIAL_LENGTH; i++) card_serials[current_card][i] = 0;
        current_card = -1;
    }
    

    We should reset the current_card variable here, since this index doesn't point to a valid card anymore. We can invalidate the card by setting the serial to zero, since we ruled out all card with a zero in the first byte at the start of the find_card() function.

2
  • many thanks for your time and explanation, i got a bit confused, do you mind put all this code in a pastebin please? thank you.
    – raduken
    Mar 7 '20 at 17:25
  • 1
    @raduken Why should I copy above code into a pastebin? You can copy the code from the question. Or do you ask for a full working code? If yes, then you will be disappointed. I don't have a full working code. I habe written the above snippets solely for this answer. It is your part to put that all together. If something in my answer is confusing, please explain what exactly, and I will try to explain it better
    – chrisl
    Mar 7 '20 at 19:27
1

1.initialize a global variable int failcount=0,MiFareCardMode=0;

  1. for reset put it in an if statement that checks fail count
       if(failcount==0)
                    {reset code}```
    
  2. in this piece of code increase the failcount number(line 195:200) also add an if statement to check which card was detected initialize a global variable to set the card status MiFareCardMode(0=unblocked,1=card1 block,2=card2 block)
if ((Code6Correct == 0) && (PinCodeCounter >= 6) && (RightCard == 1))      
  {  
    WrongPinCode = 1;                                                        
    Serial.println("WrongCode");                                             
    Reset = 1;
    failcount++       ///here increase the value with each wrong password
    if (failcount>=3)
     {   if(MifareCard1==1)
              MifareCardMode=1; ///create a variable for mode (blocked OR non blocked)
         if(MiFareCard2==1)
               MifareCardMode=2;
}                                                      
 failcount=0; } 

4.In lines 125:142 put them in an if statement which check the variable MiFareCardMode

       if(MiFareCardMode==0)
                 {
                    lines 125 to 142
                    }
         if(MiFareCardMode==1)
                   { Serial.println("Card 1 blocked permanently");
                    //add anything you want to do after blocking }

4.In lines 146:163 put them in an if statement which check the variable MiFareCardMode for card 2

    if(MiFareCardMode==0)
          {   lines 146:163  }
    if(MiFareCardMode==2)
           {Serial.println("Card2 blocked permanently");}

2
  • 1
    Please format your answer correctly, so that it readable.
    – chrisl
    Mar 6 '20 at 12:18
  • i got a bit confused, do you mind put all this code in a pastebin please? thank you.
    – raduken
    Mar 7 '20 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.