RFID and millis()

Hi,

I would like to active an output if the RFID is recognize the right ID and active an output during 10sec.
I have done the program with the delay but when I apply the millis all the times the output is on after recognize the right ID in RFID.

It's possible someone look the program and help me to see what is missing?

#include "plclib.h"

#include <SPI.h>

#include <MFRC522.h>

#define RST_PIN 9
#define SS_PIN 10
#define rele_pin1 8
#define rele_pin2 7

MFRC522 mfrc522(SS_PIN, RST_PIN);
String read_rfid;
String ok_rfid_1="8558a356";
String ok_rfid_2="78cd2e11";
bool rfid_accept;

TOF tof(2000); // Initialise ton delay object
F_TRIG ftrig;

void setup() {
Serial.begin(9600);
SPI.begin();
mfrc522.PCD_Init();
pinMode(rele_pin1, OUTPUT);
pinMode(rele_pin2, OUTPUT);
digitalWrite(rele_pin1, HIGH);
digitalWrite(rele_pin2, HIGH);
}

void dump_byte_array(byte buffer, byte bufferSize) {
read_rfid="";
for (byte i = 0; i < bufferSize; i++) {
read_rfid=read_rfid + String(buffer
, HEX);*

  • }*
    }

void loop() {

  • if ( ! mfrc522.PICC_IsNewCardPresent())*

  • return;*

  • if ( ! mfrc522.PICC_ReadCardSerial())*

  • return;*

  • dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);*
    ;

  • if (read_rfid==ok_rfid_1) {*

  • rfid_accept=1;*

  • }*

  • tof.process(rfid_accept);*

  • ftrig.process(tof.Q);*

  • if(rfid_accept==1){*

  • digitalWrite(rele_pin1, LOW);*

  • }*

  • if (ftrig.Q) {*

  • Serial.println("I waited 5 second.");*

  • digitalWrite(rele_pin1, HIGH);*

  • }*

  • else{*

  • rfid_accept=0;*

  • }*
    }
    Thank you

You are missing [ code ] tags. Some of your code is now italic and I know that the Arduino doesn't read italics. You can edit your post to correct the error.

#include "plclib.h"

#include <SPI.h>

#include <MFRC522.h>


#define RST_PIN   9
#define SS_PIN    10
#define rele_pin1 8 
#define rele_pin2 7

 
MFRC522 mfrc522(SS_PIN, RST_PIN);
String read_rfid;
String ok_rfid_1="8558a356";
String ok_rfid_2="78cd2e11"; 
bool rfid_accept;

TOF tof(2000);  // Initialise ton delay object
F_TRIG ftrig;

void setup() {
    Serial.begin(9600);        
    SPI.begin();                  
    mfrc522.PCD_Init();           
    pinMode(rele_pin1, OUTPUT);
    pinMode(rele_pin2, OUTPUT);
    digitalWrite(rele_pin1, HIGH);
    digitalWrite(rele_pin2, HIGH);   
}

void dump_byte_array(byte *buffer, byte bufferSize) {
    read_rfid="";
    for (byte i = 0; i < bufferSize; i++) {
        read_rfid=read_rfid + String(buffer[i], HEX);
    }
}

 

void loop() {

    if ( ! mfrc522.PICC_IsNewCardPresent())
        return;
    if ( ! mfrc522.PICC_ReadCardSerial())
        return;
    dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
;
     
    if (read_rfid==ok_rfid_1) {
      rfid_accept=1;
    }
                              
    tof.process(rfid_accept);
    ftrig.process(tof.Q);
    if(rfid_accept==1){
    digitalWrite(rele_pin1, LOW);

    }
    if (ftrig.Q) {
        Serial.println("I waited 5 second.");
        digitalWrite(rele_pin1, HIGH);

    }
    else{
            rfid_accept=0;
    }
}
String ok_rfid_1="8558a356";

String ok_rfid_2="78cd2e11";

Any time you have numbers in your variable names, you are doing something wrong. Read about the array concept and make your list of IDs into an array. Then you need another variable to keep track of how many there are but you can have a long, long list without copying out lots of code by hand.

..but you never use ok_rfid_2 anywhere, so this is just mess left behind from a previous edit?

;

The lonely semicolon is lonely.

  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

Don't use multi-line if() statements without braces ({}). You will cause yourself lots of forehead-slapping moments when you edit something and something else unrelated stops working.

Your code currently will always turn on the relay all the time that the tag is in range. Then it seems like you are using the F_TRIG object to turn it off at the moment that it goes out of range. But the text says "5 second"(sic) I don't see "5" anywhere in your program. I see 2 seconds in the TOF object?

What do you mean "During 10 sec"? You mean the relay only stays on for 10 seconds, even if the tag is still in range?

Can you please share with us exactly where you got plclib.h from?

Hi MorganS,

Thank you for your reply.

In attachment you have the plclib.h and link GitHub - stlehmann/arduino_plclib: An Arduino library with the basic PLC functions and function blocks referred to in IEC61131-3.

The main idea is when I read a RFID and if its in the list stay a xx seconds ON an OUTPUT and switch OFF after the time.

Thank you

arduino_plclib-master.zip (231 KB)

The main idea is when I read a RFID

I understood that part...

and if its in the list

and that part...

stay a xx seconds ON an OUTPUT and switch OFF after the time.

but what the heck does this mean?

The pin will be an OUTPUT pin regardless of whether or not the correct RFID card was read, so that part of the statement is irrelevant. Setting a pin HIGH is trivial. Waiting until an interval has passed is simple. There are two ways to do it, as illustrated in the blink and blink without delay examples. Setting a pin LOW is trivial.

It looks like you would be better served by the TP object which will give an output of a defined time span. Your current code seems like it turns the output off 2 seconds after the input goes off.