RFID with relays

Posted this in another section, before I saw this section. I would like to understand the coding, but it may make more sense for me to have someone else do it as I'm learning. I have an Arduino Uno, RFID RC522, with two cards and dual relay module. I've been able to read the cards and that's about it. I need to trigger one of the relays when the RFID card is swiped. I need the trigger to be 2 seconds. I also need for a green led to light when the correct card is scanned and a red led to light when an incorrect card is scanned. Thank you

Can you post whatever code you currently have for reference?

#include <SPI.h>
#include <MFRC522.h>

#define MAX_UID_LEN         10              //#     maximum length of UID
#define RFID_NO_MATCH       0xff            //#     return code for no-match

#define RELAY_ON            HIGH
#define RELAY_OFF           LOW
#define LED_ON              HIGH
#define LED_OFF             LOW

#define TIME_EVENT          2000ul          //mS    time relay is turned on when authorized RFID tag seen
#define RFID_READ_INTERVAL  100ul           //mS    time between RFID reads

//SPI pins are:
//  MOSI    11
//  MISO    12
//  SCK     13
//  SS      10 (as slave)
const uint8_t pinSlaveSelect = 10;
const uint8_t pinReset = 9;

const uint8_t pinGRNLed = 2;
const uint8_t pinREDLed = 3;
const uint8_t pinRelay = 4;

enum eRFIDStates

typedef struct
    uint8_t     uidLength;              //RFID tags may return 4, 7 or 10 byte UIDs
    uint8_t     grUID[MAX_UID_LEN];     

//put the UIDs of your valid tags in this structure
const ApprovedUID_t gr_ApprovedUIDList[] = 
        //zero-length indicates end of valid UIDs list

MFRC522 rfID( pinSlaveSelect, pinReset );

void setup() 
    pinMode( pinGRNLed, OUTPUT );
    pinMode( pinREDLed, OUTPUT );
    pinMode( pinRelay, OUTPUT );    

void loop() 
    static uint32_t
    static uint8_t
        stateRFID = ST_RFID_IDLE;
        timeNow = millis();
    switch( stateRFID )
        case    ST_RFID_IDLE:
            // check for a new tag every RFID_READ_INTERVAL mS
            if( (timeNow - timeReadRFID) >= RFID_READ_INTERVAL )
                timeReadRFID = timeNow;
                //if a new tag is detected...
                if( rfID.PICC_IsNewCardPresent() ) 
                    //move to the check authorization state
                    Serial.println( "RFID tag detected:" );
                    stateRFID = ST_RFID_CHECK_AUTHORIZATION;

            //try to read the UID
            if( rfID.PICC_ReadCardSerial() ) 
                Serial.println( "\tReading tag UID..." );
                //get the length of the UID into the candidate variable
                candidateRFID.uidLength = rfID.uid.size;
                //if it's a valid #...
                if( candidateRFID.uidLength <= MAX_UID_LEN )                    
                    //read the UID into the candidate variable
                    for( uint8_t idx=0; idx<candidateRFID.uidLength; idx++ )                                   
                        candidateRFID.grUID[idx] = rfID.uid.uidByte[idx];

                    //go see if we find a match in the authorized UID table above
                    if( VerifyCard() != RFID_NO_MATCH )
                        //card is authorized; green LED and relay on
                        Serial.println( "\tAuthorized RFID tag." );
                        digitalWrite( pinRelay, RELAY_ON );
                        digitalWrite( pinGRNLed, LED_ON );
                        //card is not authorized; red LED on
                        Serial.println( "\tTag is not authorized." );
                        digitalWrite( pinREDLed, LED_ON );

                    //in this example both the red LED and green LED/relay use the same timing
                    //(i.e. 2-sec on); once set, move to time the event
                    timeRelay = timeNow;
                    stateRFID = ST_RFID_TIMEEVENT;                   
                //was unable to read the UID for some reason. Go back to check for tags again
                Serial.println( "\tReading UID failed." );
                stateRFID = ST_RFID_IDLE;

        case    ST_RFID_TIMEEVENT:
            //when the event time has expired...
            if( timeNow - timeRelay >= TIME_EVENT )
                //turn everything off (both LEDs and the relay)
                Serial.println( "\tEvent done." );
                digitalWrite( pinRelay, RELAY_OFF );
                digitalWrite( pinGRNLed, LED_OFF );
                digitalWrite( pinGRNLed, LED_OFF );

                //and go back to idle state to wait for a new tag
                stateRFID = ST_RFID_IDLE;


uint8_t VerifyCard( void )

    idx = 0;
        //are we at the last entry in the approved-UID list?
        if( gr_ApprovedUIDList[idx].uidLength == 0 )
            //yes; return no match
            Serial.println( "\tNo authorization match found." );
            return RFID_NO_MATCH;
            //assume card is a match by setting bPass to true
            bPass = true;
            //does the UID field length match that of the current approved list index?
            if( candidateRFID.uidLength == gr_ApprovedUIDList[idx].uidLength )
                //yes; check all members of the UID array
                for( uint8_t n=0; n<candidateRFID.uidLength; n++ )
                    //if there is a mismatch, candidate does not match current index
                    if( candidateRFID.grUID[n] != gr_ApprovedUIDList[idx].grUID[n] )
                        bPass = false;
                //field length did not match so this one fails
                bPass = false;

            if( !bPass )
    }while( bPass == false );

    //if here we saw that bPass stayed true through all checks; return the index number
    Serial.print( "\tIndex : " ); Serial.print( idx ); Serial.println( " " );
    Serial.print( "\tUID : " );
    for( uint8_t j=0; j<candidateRFID.uidLength; j++ )
        Serial.print( candidateRFID.grUID[j], HEX );
        Serial.print( " " );
    return idx;

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.