title: How to stop multiple reads of an RFID card
body: <p>I'm using an 125Khz RFID module RDM6300 with arduino nano.
  While the card is near the RFID reader the loop will read the card multiple times. I want it to read only once while the card is near the reader then read it again if a new connection is being made.
   *This code is not writen by me, this is the source:
  <a href="https://github.com/Wookai/arduino-rfid" rel="nofollow noreferrer">https://github.com/Wookai/arduino-rfid</a></p>
  
  <pre><code>    // define constants for pins
  //int SUCCESS = 10;
  //int ERROR = 13;
  
  // variables to keep state
  int readVal = 0; // individual character read from serial
  unsigned int readData[10]; // data read from serial
  int counter = -1; // counter to keep position in the buffer
  char tagId[11]; // final tag ID converted to a string
  
  char* authorizedTags[4]; // array to hold the list of authorized tags
  
  // fills the list of authorzied tags
  void initAuthorizedTags() {
    // add your own tag IDs here
    authorizedTags[0] = "0400680B85";
    authorizedTags[1] = "0400063EB9";
    authorizedTags[2] = "040004F3F5";
    authorizedTags[3] = "04006813AB";
  }
  
  void setup() {
    Serial.begin(9600);
    // pinMode(SUCCESS, OUTPUT);
    //pinMode(ERROR, OUTPUT);
    initAuthorizedTags();
  }
  
  // check if the tag ID we just read is any of the authorized tags
  int checkTag() {
    int i;
  
    for (i = 0; i &lt; 4; ++i) {
      if (strcmp(authorizedTags[i], tagId) == 0) {
        return 1;
      }
    }
    return 0;
  }
  
  // convert the int values read from serial to ASCII chars
  void parseTag() {
    int i;
    for (i = 0; i &lt; 10; ++i) {
      tagId[i] = readData[i];
    }
    tagId[10] = 0;
  }
  
  // once a whole tag is read, process it
  void processTag() {
    // convert id to a string
    parseTag();
  
    // print it
    printTag();
  
    // check if the tag is authorized
    if (checkTag() == 1) {
      tagSuccess(); // if so, perform an action (blink a led, open a door, etc...)
    } else {
      tagFailed(); // otherwise, inform user of failure
    }
  }
  
  void printTag() {
    Serial.print("Tag value: ");
    Serial.println(tagId);
  }
  
  // perform an action when an authorized tag was read
  void tagSuccess() {
    Serial.println("Tag authorized.");
  
    // here, we simply turn on the success LED for 2s
    // digitalWrite(SUCCESS, HIGH);
    //digitalWrite(ERROR, LOW);
    // delay(2000);
  }
  
  // inform the user that the tag is not authorized
  void tagFailed() {
    Serial.println("Unauthorized access!");
  
    //digitalWrite(SUCCESS, LOW);
    // digitalWrite(ERROR, HIGH);
    // delay(2000);
  }
  
  // this function clears the rest of data on the serial, to prevent multiple scans
  void clearSerial() {
    while (Serial.read() &gt;= 0) {
      ; // do nothing
    }
  }
  
  void loop() {
    // turn LEDs off
    // digitalWrite(SUCCESS, LOW);
    //  digitalWrite(ERROR, LOW);
  
    if (Serial.available() &gt; 0) {
      // read the incoming byte:
      readVal = Serial.read();
  
      // a "2" signals the beginning of a tag
      if (readVal == 2) {
        counter = 0; // start reading
      }
      // a "3" signals the end of a tag
      else if (readVal == 3) {
        // process the tag we just read
        processTag();
  
        // clear serial to prevent multiple reads
        clearSerial();
  
        // reset reading state
        counter = -1;
      }
      // if we are in the middle of reading a tag
      else if (counter &gt;= 0) {
        // save valuee
        readData[counter] = readVal;
  
        // increment counter
        ++counter;
  
      }
    }
  }
  </code></pre>
  
  <p>Thank you.</p>
  
  <p>Thank you for your answers. I tried to accept the multiple reads and print only one but it keeps printing "already read" instead of reading the card first time, this is the code: </p>
  
  <pre><code>void printTag() {
    if(strcmp(tagId,previous)==1){
    strcpy(previous, tagId);
  
    Serial.print("Tag value: ");
    Serial.println(tagId);
    }
    else
    {
      Serial.print("already read");
    }
  
  }
  </code></pre>
  
  <p>I also tried to put the delay after end of tag but it still reads the card multiple times.</p>
  
  <p>I tried another code, it still reads the tag multiple times.</p>
  
  <pre><code>#include &lt;SoftwareSerial.h&gt;
  
  // RFID  | Nano
  // Pin 1 | D2
  // Pin 2 | D3
  SoftwareSerial Rfid = SoftwareSerial(2,3);
  int timer=0;
  int reference = 1000;
  int card_status = 0;
  void setup() {
    // Serial Monitor to see results on the computer
    Serial.begin(9600);
    // Communication to the RFID reader
    Rfid.begin(9600);
  }
  
  void read() {
    // check, if any data is available
  
      // as long as there is data available...
      while(Rfid.available() &gt; 0 ){
        // read a byte
        int r = Rfid.read();
        // print it to the serial monitor
        Serial.print(r, DEC);
        Serial.print(" ");
      }
      // linebreak
      Serial.println();
    timer=0;
  }
  
  void loop()
  {
   if((Rfid.available() &gt; 0 ) &amp;&amp; (card_status == 0) )
   {
   read();
   }
   if((!Rfid.available() &gt; 0 ) &amp;&amp; (card_status == 1) )
   {
    card_status=0;
   }
  }
  </code></pre>
  
  <p>I'm sorry for the late response. I forgot about this topic.</p>
  
  <p>I solved the problem by making the arduino wait for a response after writing the RFID code for the frist time. 
  I was able to do that because my arduino was sending the code to a C# application via serial port. </p>
  
  <p>Here is how it works: the arduino prints the RFID code on the serial, from there it is picked up by the C# application which searches a database to see if the code is stored there. Depending on the result, the application prints a character('y' or 'n') which is picked up by the arduino. Depending on the character recieved, the arduino lights up a led ( green or red) and makes a noise. Now a new RFID reading can be made.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;SoftwareSerial.h&gt;
  #include "RDM6300.h"
  
  SoftwareSerial rdm_serial(8, 9);
  RDM6300&lt;SoftwareSerial&gt; rdm(&amp;rdm_serial);
  String comanda;
  char c="";
  
  int led_verde = 2;
  int led_rosu = 7;
  int buzzer = 12;
  int i;
  
  void buzz(int n = 1)
  {
    for (int i = 0; i &lt; n; i++) {
      digitalWrite(buzzer, LOW);
      delay(200);
      digitalWrite(buzzer, HIGH);
      delay(200);
    }
  }
  
  void ledVerde()
  {
    digitalWrite(led_verde, HIGH);
    buzz(1);
    delay(1000);
    digitalWrite(led_verde, LOW);
  }
  
  void ledRosu()
  {
    digitalWrite(led_rosu, HIGH);
    buzz(3);
    delay(1000);
    digitalWrite(led_rosu, LOW);
  }
  
  
  void setup()
  {
    pinMode(led_verde, OUTPUT);
    pinMode(led_rosu, OUTPUT);
    pinMode(buzzer, OUTPUT);
    digitalWrite(led_verde, LOW);
    digitalWrite(led_rosu, LOW);
    digitalWrite(buzzer, HIGH);
    Serial.begin(9600);
  
  }
  
  void loop()
  {
  
    static unsigned long long last_id = 0;
    last_id = rdm.read();
      rdm.print_int64(last_id);
      Serial.println();
  
    rdm_serial.end();
    Serial.flush();
    while(!Serial.available());
     c=Serial.read();
  
    if(c=='y')
    {
    ledVerde();
    c="";
    }
   if(c=='n')
    {
      ledRosu();
    }
  
    Serial.flush();
    last_id="";
    c="";
    rdm_serial.begin(9600);
  }
  
  </code></pre>
  
  <p>You can find the RDM6300 library here: <a href="https://github.com/arliones/RDM6300-Arduino" rel="nofollow noreferrer">https://github.com/arliones/RDM6300-Arduino</a></p>