Card UID placement for RFID

Hi! I have a quick question. I have this code that I got from Arduino RFID Motor Stepper V2 - YouTube, and I'm trying to implement it with my arduino mega. The code that they gave doesn't seem to have a clear place for me to put the Valid UID for the RFID Tag. Could someone please help?

Here is the code:

/* 
 *  RFID stepper motor control
 *  By James "Nadrew" Smith
 *  
 *  Elegoo Mega 2560 R3
 *  
 *  MB102 Power Supply
 *    Connect to stepper controller 5v.
 *  
 *  
 *  
 *  RFID (RC522) scanner, and MIFARE tags.
 *    REMINDER: The scanner is a 3.3v device, don't connect it to 5v!
 *    PINS:
 *      RST = 5 (must be PWM)
 *      IRQ = NA
 *      MISO = 50
 *      MOSI = 51
 *      SCK = 52
 *      SDA = 53
 *      
 *  A stepper motor and controller.
 *    PINS:
 *      IN1 = 8
 *      IN2 = 9
 *      IN3 = 10
 *      IN3 = 11
 *      
 *  A basic passive buzzer/speaker
 *    Connected to pin 6 (must be PWM)
 *    
 *  A RGB LED
 *     PINS: (all PWM)
 *      R = 2
 *      G = 3
 *      B = 4
 */
 
#include <SPI.h>
#include <MFRC522.h>
#include <Stepper.h>
 
#define readerRST        5          // This is the RST pin on your RFID reader.
#define readerSS         53         // And this is the SS pin on the RFID reader.
 
#define stepperIN1       8
#define stepperIN2       9
#define stepperIN3       10
#define stepperIN4       11
 
// The three pins for the RGB LED output.
#define ledR             2
#define ledG             3
#define ledB             4
 
 
// The positive pin on your passive speaker.
#define speakerPin 6
 
#define STATUS_OK       MFRC522::STATUS_OK
 
// How bright your RGB LED is.
#define color_intensity 50
 
MFRC522 mfrc522(readerSS, readerRST);  // Create MFRC522 object for the card reader.
 
Stepper stepper(32,stepperIN1,stepperIN3,stepperIN2,stepperIN4);
 
void SetLed(int red,int green, int blue){
  // Sets the colors of the RGB LED.
  analogWrite(ledR,red);
  analogWrite(ledG,green);
  analogWrite(ledB,blue);
}
 
void setup() {
  // Set our RGB LED pins to OUTPUT.
  pinMode(ledR,OUTPUT);
  pinMode(ledG,OUTPUT);
  pinMode(ledB,OUTPUT);
  
  Serial.begin(9600);
 
  // card reader init
  SPI.begin();      // Initialize SPI
  mfrc522.PCD_Init();   // Initialize the reader.
  mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max); // Maximize the reader's antenna gain.
  
  SetLed(0,0,color_intensity);
  Serial.println("Waiting for card...");
}
 
void Authenticated() {
  // This is called when a valid ID is read.
  Serial.println("Auth passed");
 
  // Play a quick 'good job!' tone.
  tone(speakerPin,2500,50);
  delay(500);
  tone(speakerPin,2500,50);
 
  // Set the LED to green and start the motor.
  SetLed(0,color_intensity,0);
  stepper.setSpeed(500);
  stepper.step(-580);
  // Wait and step it back and set the LED back to blue.
  delay(4000);
  stepper.step(580);
  SetLed(0,0,color_intensity);
}
 
void Failed() {
  // Called when an invalid password is read.
  Serial.println("Auth failed");
 
  // A bad tone, baaaad.
  tone(speakerPin,300,350);
 
  // Flash the LED red a couple times.
  SetLed(0,0,0);
  delay(100);
  SetLed(color_intensity,0,0);
  delay(100);
  SetLed(0,0,0);
  delay(100);
  SetLed(color_intensity,0,0);
  delay(100);
  // And back to blue.
  SetLed(0,0,color_intensity);
}
 
void loop() {
  // Look for new cards
 
  // This is your MIFARE key, by default it's set to FFFFFFFFFFFF, but if you've changed it, you'll need to do that here.
  // Otherwise your card will fail to read.
  MFRC522::MIFARE_Key key;
  for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
 
  byte block, len;
  MFRC522::StatusCode status;
  
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    // No reason to go further if there's no card detected.
    // Or the same card is detected without being removed.
    return;
  }
 
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    // Bail out if it can't read the card for whatever reason.
    return;
  }
  
  Serial.println("Card detected, scanning...");
 
  // I'm storing the passcode (18 bytes) in block 4 of the card.
  // Ideally, this would be spread across the card and encoded somehow.
  byte passcode[18];
  block = 4;
  len = 18;
 
  String code_found;
 
  // Authenticate the card with your key, and proceed if you get an OK, you can add error checking here if you want.
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid));
  if(status == STATUS_OK) {
    // The authentication was good, so lets try to read the bytes from the block.
    status = mfrc522.MIFARE_Read(block,passcode,&len);
    if(status == STATUS_OK) {
      // If the read is good, loop from 0 to 16 and build a string out of each byte in the block
      for (uint8_t i = 0; i < 16; i++) {
        if(!passcode[i] != 32) {
          code_found += (char) passcode[i];
        }
      }
      code_found.trim(); // Cut any fat here.
      if(code_found == "VALID") {
        // If the code is valid, we authenticate and run the motor, this should be turned into something properly secure, of course.
        Authenticated();
      }
      else {
        // If not, fail.
        Failed();
      }
    }
  }
  
  // Now that everything is done, we're ready to shut the stepper control down, and wait for the next card.
  // No cards can be read until this part of the code is reached.
  delay(50);
  digitalWrite(11,LOW);
  digitalWrite(10,LOW);
  digitalWrite(9,LOW);
  digitalWrite(8,LOW);
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}

That code is not checking the UID of the card. It is looking for the string "VALID" in block 4 of any card presented. It is a difference scheme is you want to just have a list of valid UID codes that will work vs. the code you provide.

Look at the examples that come with the library: read_personal_data and write_personal_data to see how you would put "VALID" onto an RFID card and then give it to somebody to use.

Okay thank you! I'll try that and let you know!

Yup that worked thank you very much!

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