I am using Arduino for a RFID project where the LED must switch on after either 12 readings of the tag or 2 minute, the tag reading happen every 10 seconds. I can't seem to figure out a way to have the LED switch on only after the above mentioned conditions.

I have tried doing various if loop much to my failure, below is the code I am currently using:

#include <RFID.h>
#include <SPI.h>
#define SS_PIN 10
#define RST_PIN 9
#define LED_PIN A0

int serNum[5];
String cardno;
int interval = 15000; // millisec
long now = 0;
long lasttime = millis();
// change Reader ID to your name
String readerID = "Reader_001";

void setup() {
  pinMode(A0, OUTPUT);

void loop() {
  now = millis();
  if (now > lasttime + interval) {
    lasttime = now;
    Serial.println("I am alive");
  if (rfid.isCard()) {
    if (rfid.readCardSerial()) {
      lasttime = now;
      cardno = String(rfid.serNum[0]) +
               String(rfid.serNum[1]) +
               String(rfid.serNum[2]) +
               String(rfid.serNum[3]) +


The expected outcome is the LED switching on after 2 minutes of contact between the tag and the reader.

  • first thing to do is to correct the indentation of your code so that the code blocks are clearly visible ...... that will help in determining where to place the code that counts the number of repetitions
    – jsotola
    Apr 19, 2019 at 18:14
  • Ditch the delay() and work only with millis(). Use a counter variable, that is incremented on every read. In the loop you can check for it being >=12 and turn on the LED.
    – chrisl
    Apr 19, 2019 at 18:22
  • since I am a beginner would it be too much to ask for an example?
    – Ayat.H
    Apr 19, 2019 at 18:51

1 Answer 1


You want to light the LED, when the same chip was read 12 consecutive times. So I would suggest something like this:

Inside your void loop() function you use the millis() coding style (refer to the BlinkWithoutDelay example, that comes with the Arduino IDE) to scan every 10s for a chip. Then you add a counter variable to count, how often the same chip was scanned. If this counter variable is >=12, you turn on the LED. If a different chip was scanned, you set the counter to 1 (since this chip was now scanned 1 time). This leads to a code something like this:

#define READ_INTERVAL 10000
int read_counter = 0;
unsigned long read_timestamp=0;
String last_identifier="";
void loop(){
        // Read the chip
        if(card_identifier == last_identifier)
            read_counter = 1;
            digitalWrite(LED_PIN, HIGH);

Be sure to use unsigned long with the millis() function, to prevent it from overflowing to a negative value.

  • Do you also have any suggestions for connecting two arduinos? both my arduino projects are rfid access control, this being the first one the second one is simple rfid but the access on that needs to depend on the output of the first rfid
    – Ayat.H
    Apr 19, 2019 at 22:53
  • i am thinking of doing a SPI master and slave? would you recommend that
    – Ayat.H
    Apr 19, 2019 at 23:19
  • Yes, that should be possible. This also depends on the distance between the Arduinos
    – chrisl
    Apr 20, 2019 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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