Vault Project - IF statement

Hi,

I'm new in this forum, and I'm developing a Vault controled by Arduino. I've the all code made, but I've a problem on this. I'm a begginer and I´don´t know how to close its door, I´ve tried with a push button but it doesn't work, and now I'm trying with variables but I continue not solving my problem: close the door (turning 100º with a SG90 Servo) after open it with RFID Cards. In one code (button one) door opens but don't close; in the other code (variables one) door opens and closes after 3 seconds automatically.
al

The 2 codes are attached.

Does someone knows how to solve it?
Many Thanks

Code with button:

#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.


#include <Servo.h>
Servo mm;

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();

  mm.attach(2);
  mm.write(10);

  pinMode(4,INPUT);
  
  pinMode(7,OUTPUT); // Green LED
  digitalWrite(7,LOW);
  pinMode(5,OUTPUT);// Red LED
  digitalWrite(5,LOW);
  pinMode(6,OUTPUT); // Orange LED - Power Supply
  digitalWrite(6,HIGH);
  delay(3000);
  digitalWrite(6,LOW);

}
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) == "4C 8E 7F 2B" || content.substring(1) == "A6 9F DC A0")  //change here the UID of the card/cards that you want to give access
  {
    mm.write(100);
    Serial.println("Authorized access");
    Serial.println();
    digitalWrite(7,HIGH);
    delay(3000);
    digitalWrite(7,LOW);
    
    }
 
 else   {
    Serial.println(" Access denied");
    digitalWrite(5,HIGH);
    delay(2000);
    digitalWrite(5,LOW);
    
  }


if (digitalRead(4) == HIGH)  // If button is pressed - SO - close the locker and blink the Red LED
  {
  mm.write(10);
  digitalWrite(15,HIGH);
  delay(3000);
  digitalWrite(15,LOW);
  
}


  

}

Code with variables

#include <Servo.h>
#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

Servo mm;

int a = 0; // 0 means closed; 1 means open, (The Door)

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();

  mm.attach(2);
  mm.write(10);

  
  pinMode(7,OUTPUT); // Green LED
  digitalWrite(7,LOW);
  pinMode(5,OUTPUT);// Red LED
  digitalWrite(5,LOW);
  pinMode(6,OUTPUT); // Orange LED - Power Supply, only blinks at the beggining
  digitalWrite(6,HIGH);
  delay(3000);
  digitalWrite(6,LOW);

}
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) == ("4C 8E 7F 2B")) || (content.substring(1) == ("A6 9F DC A0")) && (a = 0))  //If one of the 2 keys registed were detected and the door is closed, THEN modify its state to open and open it
  {
   
    mm.write(100);
    Serial.println("Authorized access");
    Serial.println();
    digitalWrite(7,HIGH);
    delay(3000);
    digitalWrite(7,LOW);
    int a = 1;
    
    }
 
 else   { // Otherwise dont open the door
    Serial.println(" Access denied / Door Closed");
    digitalWrite(5,HIGH);
    delay(2000);
    digitalWrite(5,LOW);
    
  }

 if ((content.substring(1) == ("4C 8E 7F 2B")) || (content.substring(1) == ("A6 9F DC A0")) && (a = 1))  //If one of the 2 keys registed were detected and the door is open, THEN modify its state to close and close it
  {
    
    mm.write(10);
    Serial.println("Authorized access");
    Serial.println();
    digitalWrite(7,HIGH);
    delay(3000);
    digitalWrite(7,LOW);
    int a = 0;
    
  }


}

migascool123:
In one code (button one) door opens but don't close

I notice that the one place in the code where you don't have Serial output is in the part that you're having trouble with. Add some more Serial.println() calls at strategic points in your code and then run it with the Serial Monitor open so you can see what's happening in your code.

migascool123:
in the other code (variables one) door opens and closes after 3 seconds automatically.

Here is your declaration of a global variable named a:

migascool123:

int a = 0; // 0 means closed; 1 means open, (The Door)

Here you have declared a variable named a that is local to that if statement's block:

migascool123:

int a = 1;

Here you have declared another variable named a that is local to that if statement's block:

migascool123:

int a = 0;

Although these variables have the same name, they are three separate variables, at three different scopes. I guess you think that you were defining the value of the global variable named a in your local declarations, but you weren't. This is called "variable shadowing", and is the source of a lot of confusion.

You need to understand the difference between a variable declaration:

int a = 1;

and a variable definition:

a = 1;

Hi @perl ,

I've already put more Serial.println() in strategic points of my code, put nothing happend, am I doing something wrong?

I've also changed the variables in the code (only the declarations, during the code) and the door continue closing after 2 seconds.

Do you know any way more to solve this problem?

Thank You

migascool123:
I've already put more Serial.println() in strategic points of my code, put nothing happend

I don't know what you mean by "nothing happend". Did you open Serial Monitor while the program was running? What was the output you saw?