RFID to open and close "door" help

Hey guys, can someone tell me how can I code the arduino to do this:
-pass card
-open door (relay 1)
-pass card
close door (relay 2)

so far I have de first part (open door) working but I can't make the second part work, and i've been looking online but I can't find nothing, can someone help me? :confused:

Read this, with particular attention to point 6.

ok you're right sorry :confused:

so, my objective is to open with the first card swipe reading, and close with the second same card swipe,
and yes i am new at arduino programing, i know a little bit about electronics but this is my first time using arduino and programing

as is, when I swipe the card it sends the signal to relay 1 to open, and half a second later to the second to close...I would like to send the comand to close only with a second card swipe

if someone can help me i would thank you

this is what I have so far:

#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
#define RELAY1  6
#define RELAY2  5
 
void setup() 
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  Serial.println("Approximate your card to the reader...");
  Serial.println();
  
}
void loop() 
{
  // Initialise the Arduino data pins for OUTPUT

  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();

  if (content.substring(1) == "83 9B B1 AB" || content.substring(1) == "16 AF 86 A5") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("Open");
    Serial.println();
    delay(100);
    digitalWrite(RELAY1,HIGH);           // Turns ON Relay 1
    delay(500);                                      // Wait 1\2 second
    digitalWrite(RELAY1,LOW);          // Turns Relay Off
  }

 else (content.substring(1) == "83 9B B1 AB" || content.substring(1) == "16 AF 86 A5") //change here the UID of the card/cards that you want to give access
;  {
    Serial.println("Closed");
    Serial.println();
    delay(100);
    digitalWrite(RELAY2,HIGH);           // Turns ON Relay 2
    delay(500);                                      // Wait 1\2 second
    digitalWrite(RELAY2,LOW);          // Turns Relay Off
  }
 }
void loop()
{
  // Initialise the Arduino data pins for OUTPUT

  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);

How many times do you need to set the pins to output? The correct answer is ONCE.

The example you started from is garbage. If there is not a card present, and it is not a new card, the Arduino does NOTHING except uselessly set the mode of two pins to output, on every pass through loop().

 else (content.substring(1) == "83 9B B1 AB" || content.su...

There is an if keyword missing, or you are abusing the language syntax.

thanks for your repply,
like I said, I'm new at this... :neutral_face:
if this is garbage can you tell me a good example to start from?
is it that hard to achieve what i'm trying to achieve?

IRSantos8:
if this is garbage can you tell me a good example to start from?
is it that hard to achieve what i'm trying to achieve?

It does look a bit dodgy, but you may learn something from it nonetheless.
Try fixing the else if as recommended by PaulS - as well as removing the spurious ; on the next line - and see what happens.

thank you for the repply

when I remove the ";" and put the "else if" relay 1 works with the first swipe and opens, but any other swipe it repeat the open with relay 1, and not "close" with relay 2

i'm sorry if I'm not understanding what you are trying to tell me, i'm portuguese, and my english is a bit rusty :sweat_smile:

i'm what to achieve a working code to open and close my car doors swiping de card or the key chain to open and close with the 2 relays

like this it works like I said

  if (content.substring(1) == "83 9B B1 AB" || content.substring(1) == "16 AF 86 A5") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("Open");
    Serial.println();
    delay(100);
    digitalWrite(RELAY1,HIGH);           // Turns ON Relays 1
    delay(500);                                      // Wait 1\2 seconds
    digitalWrite(RELAY1,LOW);          // Turns Relay Off
  }

else if (content.substring(2) == "83 9B B1 AB" || content.substring(2) == "16 AF 86 A5") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("Closed");
    Serial.println();
    delay(100);
    digitalWrite(RELAY2,HIGH);           // Turns ON Relays 1
    delay(500);                                      // Wait 1\2 seconds
    digitalWrite(RELAY2,LOW);          // Turns Relay Off
  }
 }

No, you understand just fine, don't worry about that.

What is missing from your code is knowledge of whether the door is open or whether it is closed.

Probably the best option would be in hardware; so a switch that detects whether the door is open or closed and then your sketch can read the state of the switch.

Alternatively, for the quick and dirty software approach, you can assume that the door is closed at the beginning of the sketch (when the Arduino is turned on), or even perhaps force close it.
You need a global variable to store the door state, eg. bool isDoorOpen = false;
Then, when a valid card is swiped, you check if the door is open or closed and act accordingly, toggling the value of isDoorOpen afterwards.
You only need a simple if block to check if the card is valid. Then the if...else for the isDoorOpen part.

thank you arduarn,
i'm going to try what you said, I don't know to code those things yet, but i'll try to learn :slight_smile:
the other option I can think is if the car central locking has a module that suports only one signal to open and close, i'm gonna try to find out, if it does i can do it with only one relay instead

thank's for all your repplys