Stopped RFID multiple read/unlocks using Millis()

This is a follow up on my other topic;
“Combined 2 sketchs cat door SUCCESS”
Maybe should have posted this there but this is kind of a different subject for folks searching millis() usage.

That sketch worded fine except had multiple read/unlock problem.
After reading several tutorials and examples on use of millis() I was able to eliminate the multiple unlock cycles in my sketch.
Replaced this command:

RFID.flush(); // stops multiple reads

With this:

if (millis() - RFID.read() >= 20000)
newtag[14] = millis();

Still an newbie cause I'm not 100% sure why it works. It just felt like the right thing to do.
Tried other variations of command with no luck until this one. All is good for now. Working as intended.

Components in project:
*Arduino UNO r3
*Seeed RDM630 125Khz RFID module-UART (pin 2, INPUT)
w/homemade 12” round antenna (was able to achieved about 5” read range)
*125Khz rfid tag

  • 5v 1-channel Relay (to trigger XHQ-PT solenoid for door lock) (pin 9, OUTPUT)
    *HC-SR501 PIR Motion Detector (pin 6, INPUT)

FYI: I bought 3 rfid tags. They look identical yet one has almost twice the range of the other two.

Here's the final sketch:

#include <SoftwareSerial.h>
SoftwareSerial RFID(2, 3); // RX and TX

int data1 = 0;
int ok = -1;
int yes = 9;
int no = 11;
#define led 13
#define pirSensor 6

// use first sketch in http://wp.me/p3LK05-3Gk to get your tag numbers
int tag1[14] = {2, 51, 49, 48, 48, 66, 49, 67, 48, 68, 52, 57, 52, 3};
int tag2[14] = {2, 52, 48, 48, 48, 56, 54, 67, 54, 54, 66, 54, 66, 3};
int newtag[14] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // used for read comparisons

void setup()
{
  pinMode(13, OUTPUT);
  RFID.begin(9600);    // start serial to RFID reader
  Serial.begin(9600);  // start serial to PC
  pinMode(yes, OUTPUT); // for status LEDs
  pinMode(no, OUTPUT);
  pinMode(pirSensor, INPUT);
}

boolean comparetag(int aa[14], int bb[14])
{
  boolean ff = false;
  int fg = 0;
  for (int cc = 0 ; cc < 14 ; cc++)
  {
    if (aa[cc] == bb[cc])
    {
      fg++;
    }
  }
  if (fg == 14)
  {
    ff = true;
  }
  return ff;
}

void checkmytags() // compares each tag against the tag just read
{
  ok = 0; // this variable helps decision-making,
  // if it is 1 we have a match, zero is a read but no match,
  // -1 is no read attempt made
  if (comparetag(newtag, tag1) == true)
  {
    ok++;
  }
  if (comparetag(newtag, tag2) == true)
  {
    ok++;
  }
}

void readTags()
{
  ok = -1;

  if (RFID.available() > 0)
  {
    // read tag numbers
    delay(100); // needed to allow time for the data to come in from the serial buffer.

    for (int z = 0 ; z < 14 ; z++) // read the rest of the tag
    {
      data1 = RFID.read();
      newtag[z] = data1;
    }
   if (millis() - RFID.read() >= 20000)
    newtag[14] = millis();

    // do the tags match up?
    checkmytags();
  }

  // now do something based on tag type
  if (ok > 0) // if we had a match
  {
    Serial.println("Accepted");
    digitalWrite(yes, HIGH);
    delay(20000);
    digitalWrite(yes, LOW);

    ok = -1;
  }
  else if (ok == 0) // if we didn't have a match
  {
    Serial.println("Rejected");
    digitalWrite(no, HIGH);
    delay(1000);
    digitalWrite(no, LOW);

    ok = -1;
  }
}

void loop() {
  {
    readTags();
  }
  int x = digitalRead(pirSensor);
  if (x == LOW)
  {

    digitalWrite(led, LOW);

    digitalWrite(yes, LOW);

    Serial.println(x);

  }

  else

  {

    digitalWrite(led, HIGH);

    digitalWrite(yes, HIGH);

    Serial.println(x);

  }
}

I'm sure it can be tweaked to work better and/or more efficiently. Any input is welcome.
This project has been an interesting learning experience. Not sure when I'll attempt another one put now I have the basic knowledge and won't be afraid to try. Thanks to everyone who helped me through it. This forum has been a great resource.

Here's the final sketch:

Nah... That's you current sketch, there never is a final sketch because it's always possible to make improvements :slight_smile:

I'm sure it can be tweaked to work better and/or more efficiently. Any input is welcome.

There is absolutely no need for a single millisecond of delay in your sketch, let alone some of the quite long delays you have.

I suggest you read and understand these two tutorials and use the methods in there to get rid of any delays:
Using millis for timing
Demonstration for several things at the same time

I don't know if you've read it already but if not then robin2's serial input basics is also useful.

Well done one what you've done so far, but there's still more to learn!