0
\$\begingroup\$

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
RFID rfid(SS_PIN, RST_PIN);
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() {
Serial.begin(9600);
SPI.begin();
rfid.init();
pinMode(A0, OUTPUT);
}

void loop() {
now = millis();
if (now > lasttime + interval) {
lasttime = now;
Serial.print(readerID);
Serial.print(":");
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]) +
String(rfid.serNum[4]);

Serial.print(readerID);
Serial.print(":");
Serial.println(cardno);
}
}
rfid.halt();
delay(10000);
}

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

\$\endgroup\$
  • \$\begingroup\$ Presumably you want a counter that increments on each success, and some kind of time limit that resets the count after a long enough gap, for which you probably want to be storing the time of the last success. \$\endgroup\$ – Chris Stratton Apr 19 at 16:31
  • \$\begingroup\$ yes exactly but I can't figure out how to do that \$\endgroup\$ – Ayat.H Apr 19 at 16:49
  • \$\begingroup\$ First get rid of the delay(). Then consider making a sketch that prints a running count of the number of reads. Then think about how you can determine how much time has elapsed since the last read and print that. Then think about how you can take action when that amount of time since the last read exceeds some threshold... \$\endgroup\$ – Chris Stratton Apr 19 at 16:52
  • \$\begingroup\$ i am a complete beginner at using arduino, can you suggest some commands? do you think millis would work? \$\endgroup\$ – Ayat.H Apr 19 at 17:10
  • \$\begingroup\$ I had another question, is there a way to connect 2 arduino boards as in where the output on one board determines the output on the other? \$\endgroup\$ – Ayat.H Apr 19 at 17:10

Your Answer

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

Browse other questions tagged or ask your own question.