RDFI CODING PROBLEM on Mac

Im trying to do an automatic door lock with arduino uno and Rfid and I want to change the access to another card. For example, it says that my card is denied. I want to have a certain card that grants access but when I copie the number id of the card i want in the access granted string, i doesn't work. It is always going to show me denied access even if I put that we needed the number id of the card i want in the granted access part. It always denies and show the red led(which is set for denied access), my circuit is well connected its really a coding problem. Please help me, HERE'S MY CODE AND PICTURE OF THE CODE.

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

// Pin definitions for the MFRC522
#define SS_PIN 10       // SDA pin for RFID reader
#define RST_PIN 9       // RST pin for RFID reader

MFRC522 rfid(SS_PIN, RST_PIN);  // Create MFRC522 instance

// Define the RFID tag to be accepted
String accessGranted[2] = {"8F5C7F6"};  // RFID serial numbers to grant access
int accessGrantedSize = 2;  // The number of serial numbers

Servo lockServo;  // Servo for the locking mechanism
int lockPos = 15;   // Position for locked
int unlockPos = 75;  // Position for unlocked
boolean locked = true;

int redLEDPin = 5;    // Pin for the red LED
int greenLEDPin = 6;  // Pin for the green LED

void setup() {
  Serial.begin(9600);  // Initialize serial communication
  SPI.begin();         // Start SPI communication
  rfid.PCD_Init();     // Initialize the RFID reader
  pinMode(redLEDPin, OUTPUT);  // Initialize red LED
  pinMode(greenLEDPin, OUTPUT);  // Initialize green LED
  
  lockServo.attach(3);  // Attach the servo to pin 3
  lockServo.write(lockPos);  // Set servo to locked position
  
  Serial.println("Place card/tag near reader...");
}

void loop() {
  // Look for new RFID cards
  if (rfid.PICC_IsNewCardPresent()) {
    Serial.println("Card found");
    String temp = "";  // Temporary variable to store the card ID

    // Read the RFID card's ID
    if (rfid.PICC_ReadCardSerial()) {
      for (byte i = 0; i < rfid.uid.size; i++) {
        temp += String(rfid.uid.uidByte[i], HEX);  // Convert card UID to hex string
      }
      temp.toUpperCase();  // Convert the ID to uppercase
      Serial.print("The card's ID number is: ");
      Serial.println(temp);
      checkAccess(temp);  // Check if the card is authorized
    }
    rfid.PICC_HaltA();  // Halt the RFID communication
  }
  rfid.PCD_StopCrypto1();  // Stop the encryption
}

void checkAccess(String temp) {
  boolean granted = false;
  Serial.print("Checking UID: ");
  Serial.println(temp);  // Debugging: Print the UID being checked

  // Check if the card ID matches the authorized cards
  for (int i = 0; i < accessGrantedSize; i++) {
    Serial.print("Comparing with stored UID: ");
    Serial.println(accessGranted[i]);  // Debugging: Print the stored UID
    if (accessGranted[i] == temp) {  // If the UID matches
      Serial.println("Access Granted");
      granted = true;
      // Unlock the servo if it's locked
      if (locked) {
        lockServo.write(unlockPos);
        locked = false;
      } else {
        // Lock the servo if it's unlocked
        lockServo.write(lockPos);
        locked = true;
      }

      // Blink the green LED to show access is granted
      digitalWrite(greenLEDPin, HIGH);
      delay(200);
      digitalWrite(greenLEDPin, LOW);
      delay(200);
      digitalWrite(greenLEDPin, HIGH);
      delay(200);
      digitalWrite(greenLEDPin, LOW);
      delay(200);
      break; // Exit the loop once access is granted
    }
  }

  if (granted) {
    // Access granted, perform the desired action
    // (e.g., unlocking a door, etc.)
  } else {
    // Access denied, show message and blink red LED
    Serial.println("Access Denied");
    digitalWrite(redLEDPin, HIGH); // Blink red LED
    delay(200);
    digitalWrite(redLEDPin, LOW);
    delay(200);
    digitalWrite(redLEDPin, HIGH);
    delay(200);
    digitalWrite(redLEDPin, LOW);
    delay(200);
  }  // End of 'else' block

}  // End of checkAccess function

Did you discover this line of code? The comment says these are the serial numbers to grant access. Can you change that line of code to match what you want?

1 Like

These prints don’t show in your screen grab, so either you did not share the code that is actually running on your board or you have another issue like memory full or something.

1 Like

So remember that an RFID chip works with 3V3 voltages and so using a Uno, which sends out 5V signals into a 3V3 device will destroy the operation of the card reader over time. Almost all of the internet has the wiring for this wrong. This is what it should look like:-

Also you code looks wrong.
This line:-

Sets up an array of strings, but it only initiates one of the strings, why are you doing this?

Finally this is nothing to do with a Mac, it would equally not work on a Linus or Windose machine.

1 Like

Thank you I missed the 2, I guess I typed too fast. Also, thank you for the Schematic you are a savior, I missed some things but now they work fine. Thanks a lot!! :slight_smile:

1 Like

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