Rfid MFRC 522 and keyboard lib

Hi,
I am trying to get keyboard output from the arduino after scanning respective card but I need an output when there is no card. It comes but so rapidly and doesn't stop.
I need only one output when there is no card.

#include <SPI.h>
#include <RFID.h>
#include<Keyboard.h>
#define SS_PIN 10
#define RST_PIN 9
RFID rfid(SS_PIN, RST_PIN);
bool flag = false;
int btn = 4;
String rfidCard;
String Prev_Card;
String Card1= "f3 3c 7c 1a";
String Card2= "c3 69 81 1a";
String Card3= "63 2f 7d 1a";
String Card4= "4a 8d bc ed";
void setup() {
  Serial.begin(9600);
  Keyboard.begin();
  pinMode(btn, INPUT);
  Serial.println("Starting the RFID Reader...");
  SPI.begin();
  rfid.init();
}

void loop() {
  if (digitalRead(btn)== HIGH){
    resett:
    if (rfid.isCard()){                                     // checks if the card is present
       //rfid.halt();                                          // it stops the rfid reader to read the hex id simultaniously
      if (flag == false) {                                  // check flag if true then run cmd and return flag to false to run stop running same cmd 
        //Serial.println("Card is present");
          
        
          if (rfid.readCardSerial()) {
            rfidCard = String(rfid.serNum[0], HEX) + " " + String(rfid.serNum[1],HEX) + " " + String(rfid.serNum[2], HEX) + " " + String(rfid.serNum[3], HEX);
            
           if (rfidCard==Prev_Card){
          //Prev_Card= rfidCard;
          goto resett;}
          else{
            Prev_Card= rfidCard;
              }
            Serial.println(rfidCard);
            if (rfidCard==Card1){
                Keyboard.write('b');
                Serial.println("Card 1 is tapped");}
            else if (rfidCard==Card2){
                 Keyboard.write('c');
                Serial.println("Card 2 is tapped");}
            else if (rfidCard==Card3){ 
                Keyboard.write('d'); 
                Serial.println("Card 3 is tapped");}
            else if (rfidCard==Card4){
                Keyboard.write('e');
                Serial.println("Card 4 is tapped");
           }
          //rfid.halt();
          delay(100);
      }
      else{
        Serial.println("Card  is not tapped");
      }
      
        //rfid.halt();
        flag = true;
        
      }
    }
    else{                                                   // if the card is not present 
      if (flag == true){                                    // check if the flag is true or not means not runit if previosly card is not  present there.
          Serial.println("Card is not present");
          Keyboard.write('a');
          flag =false;
          }
    }
  }
}

My scope is just to get keyboard output when card is tapped and when there is no card it trigger once and then stop sending trigger but in state of reading card again.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for advice on (nor for problems with) your project.

Your description is not quite clear to me. Can you try to rephrase.

It will be a good idea if you post your current code; just in case don't forget to use code tags when posting code.

If I got your desire correctly, just move the flag = true; line in front of the "rfidCard = ..." line. That way the flag is activated only if there was a card available and once it was reset it won't be activated again until a new card is presented.

BTW, learn to use the "Auto Format" function (Ctrl-T) of the IDE to format your code to make it readable.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.