NEED HELP with RFID-RC522 with automated shed door

Hi all,

I hope you can help me with my project. I want to do the following:
I want to be able to open my sliding door with an RFID tag. Currently the sliding door opens only with a remote control. I figured out that can connect a relay parrallel to the receiver unit on the sliding controlboard to be able to open the door with remote and with RFID. After opening the door with RFID, i want to be able to close it with a pushbutton (non-stick). I have edited existing code so that the relay closes when the right TAG is presented but i can't seem to figure out how to implement the pushbutton into the code. It seems the code runs over the last part and opens the relay despite the pushbutton not pushed. I used the println to print the value of the PUSHBUTTON, it displays the right value, so its not a problem with reading the pushbutton value.

My Code:

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


#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

int REDLED = 2;
int RELAY = 3;
int PUSHBUTTON = 0;

void setup()
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  Serial.print("SHED DOOR.");
  pinMode (REDLED, OUTPUT);
  pinMode (RELAY, OUTPUT);
  pinMode (PUSHBUTTON, INPUT);

}
void loop(){
  // 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) == "14 6C B9 A7" || content.substring(1) == "14 99 1B A7" ) //change here the UID of the card/cards that you want to give access
   {
    Serial.println("Authorized access");
    digitalWrite (RELAY, HIGH);
     }
  else   {
    Serial.println(" Access denied");
    digitalWrite (RELAY, LOW);
  }
PUSHBUTTON = digitalRead (7);
Serial.print(PUSHBUTTON);
if (PUSHBUTTON = LOW){
  digitalWrite (RELAY, LOW);
}
}

I hope you guys can help me

Typo = s/b ==

2 Likes

Ignore the history on this one; I didn't read the sketch carefully enough. Mea culpa.

2 Likes

On a second read, I see that PUSHBUTTON isn't the name of the pin you have connected to the pushbutton. Okay, that confused me. So ignore the bits I said about digitalRead(PUSHBUTTON) and pinMode(PUSHBUTTON, INPUT_PULLUP). @sonofcy had it right.

But your code appears to rely on the default mode for all pins to be INPUTs. I'd still explicitly set your pushbutton pin to be an INPUT_PULLUP if you don't have a pullup resistor. And pinMode(PUSHBUTTON, INPUT) isn't going to do what you may think it does; PUSHBUTTON is initialized to 0, which is the Serial Rx pin.

2 Likes