Manual lock codes for MFRC522

So I’m new here , sorry for the title. I’ve got this shared project by Marcelo of Instructables. It’s an access control system. So program works like this , if a registered RFID card is scanned the servo motor will unlock and automatically lock after 5 secs. What I want to now is , I want the servo to lock again if the same tag is being read. Can you help me with this? I’m totally new with this. Thanks! By the way , here’s the code:

// libraries
#include <SPI.h>
#include <RFID.h>
#include <Servo.h>
#include "pitches.h"
#include <Wire.h>
  
// RFID definition
RFID rfid(10,5);

byte ealdsem[5]  = {0x14,  0xBA,  0xA2,  0x45};// Al (e+ald+sem)
byte emodsem[5] = {0xFF, 0xFF , 0xFF , 0xFF , 0xFF}; 
// Put here the another allowed cards


byte serNum[5];
byte data[5];

// Melodies definition: access, welcome and rejection
int access_melody[] = {NOTE_G4,0,NOTE_A4,0, NOTE_B4,0,NOTE_A4,0,NOTE_B4,0, NOTE_C5,0};
int access_noteDurations[] = {8,8,8,8,8,4,8,8,8,8,8,4};
int fail_melody[] = {NOTE_G2,0,NOTE_F2,0,NOTE_D2,0};
int fail_noteDurations[] = {8,8,8,8,8,4};

// pins definition - LED, Buzzer and Servo-motor
int LED_access = 3;
int LED_intruder = 4;
int speaker_pin = 8;
int servoPin = 9;


// servo motor definition
Servo doorLock;


void setup(){
  doorLock.attach(servoPin); // servo motor attaching
  Serial.begin(9600); // Serial communication initialization
  SPI.begin(); // SPI communication initialization
  rfid.init(); // RFID module initialization
  Serial.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
  Serial.println("+                  Welcome to my World                                      +"); 
  Serial.println("+        Access Control System with Log Manager                             +");
  Serial.println("+               Waiting for a valid card...                                 +");
  Serial.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
  delay(1000);
  pinMode(LED_access,OUTPUT);
  pinMode(LED_intruder,OUTPUT);
  pinMode(speaker_pin,OUTPUT);
  pinMode(servoPin,OUTPUT);
}
 
void loop(){
  // Here you can create a variable for each user
  // NAME_card ou KEY_card
  boolean ealdsem_card = true; // that is my card (e + ald + sem)...e + almod = ald, seldom = sem
  boolean emodsem_card = true;
  
  if (rfid.isCard()){ // valid card found
    if (rfid.readCardSerial()){ // reads the card
      delay(1000);
      data[0] = rfid.serNum[0]; // stores the serial number
      data[1] = rfid.serNum[1];
      data[2] = rfid.serNum[2];
      data[3] = rfid.serNum[3];
      data[4] = rfid.serNum[4];
    }
  
  rfid.halt(); // RFID module to standby
  Serial.print("Card found!");
 
  for(int i=0; i<5; i++){
    if(data[i] != ealdsem[i]) ealdsem_card = false;
    //if(data[i] != emodsem[i]) emodsem_card = false; // if it is not a valid card, put false to this user
    // Here you can check the another allowed users, just put lines like above with the user name
  }

  Serial.println();
  if (ealdsem_card){ // if a valid card was found
    Serial.println("Hello Al!"); // print a message
    for (int i = 0; i < 12; i++){ // play welcome sound
      int access_noteDuration = 1000/access_noteDurations[i];
      tone(speaker_pin, access_melody[i],access_noteDuration);
      int access_pauseBetweenNotes = access_noteDuration * 1.30;
      delay(access_pauseBetweenNotes);
      noTone(speaker_pin);
    }
  }
  
  // another cards analysis put many blocks like this as many user you have
  else if(emodsem_card){// put the another allowed user here
    Serial.println("Hello MODLES"); // put the user name in the "USER NAME"
    for (int i = 0; i < 12; i++){
      int access_noteDuration = 1000/access_noteDurations[i];
      digitalWrite(LED_access, HIGH);
      tone(speaker_pin, access_melody[i],access_noteDuration);
      int access_pauseBetweenNotes = access_noteDuration * 1.30;
      delay(access_pauseBetweenNotes);
      noTone(speaker_pin);
    }
  }
  
  else{ // if a card is not recognized
    Serial.println("Card not recognized! Please contact your administrator!"); // print a message
    digitalWrite(LED_intruder, HIGH); // turn on the red LED
    for (int i = 0; i < 6; i++){ // playa rejection sound
      int fail_noteDuration = 1000/fail_noteDurations[i];
      tone(speaker_pin, fail_melody[i],fail_noteDuration);
      int fail_pauseBetweenNotes = fail_noteDuration * 1.30;
      delay(fail_pauseBetweenNotes);
      noTone(speaker_pin);
    }
    delay(1000);
    digitalWrite(LED_intruder, LOW); // turn off the red LED
  }
  if (ealdsem_card || emodsem_card){// add another user using an logical or condition ||
    // Welcome messages and access permission  
    Serial.println("Access Granted!... Welcome!"); // print a message
    digitalWrite(LED_access,HIGH); // turn on the green LED
    doorLock.write(180); // releases the door
   Serial.println("This Door will automatically lock in 5 seconds!");
   delay(5000); // wait
   doorLock.write(0); // Locks the door
    digitalWrite(LED_access,LOW); // turn off the green LED
  }
  
  Serial.println();
  delay(500);
  rfid.halt();

  }
}

First, it would help if your code were in code tags as requested in the forum stickies above. Look at your post. As you will see most of your code is in italics because code tags were not used.

Secondly, what do you mean by

I want the servo to lock again if the same tag is being read

Should access to the same tag only be allowed once however long the gap between attempts or only if the second attempt is immediately after the first ? How many tags are in use ?

I apologize for that , I've read the rules and must have missed it. Anyway sir, what I'm trying to say is like this:

First scan: the servo unlocks
second scan: the servo locks
3rd scan: the servo unlocks
4th scan: the servo locks

and so on...

The core part you need to change is here

      Serial.println("This Door will automatically lock in 5 seconds!");
      delay(5000); // wait
      doorLock.write(0); // Locks the door

If you removed the 3 lines the door would stay unlocked, so take them out.

Now you need another global variable. Let's name it locked and set it to true at the start of the program. Then, when a valid card is read you check the locked variable. If it is true you unlock the door as now and set the variable to false. If it is false you lock the door as the lines you removed did and set the variable to true.

Okay , now things get a little more complicated here. Sorry sir I'm just a newbie. Can you construct the lines for me? I'm really confused right now.

Also , how do I get the RFID tag's ID?? So I can assign it as registered tag on the system.

If you want someone to write the code for you then this is the wrong section of the forum in which to ask. Gigs and Collaborations would be better. The moderators can move this thread there if you want rather than you cross posting the query, which is considered bad form.

How many of the examples in the IDE have you loaded and run ? If you want to learn to use the Arduino they are a good start.

So I did another mistake here , my bad. How can I ask them to move this thread? I'm just confused in which part of the sketch I'd add and what codes to add. Actually I understand your tips but then again it just confuses me.

If you really want the thread moved then you could PM a moderator such as AWOL and ask him to move it.

Of course, you could also leave it here, get advice and modify the program yourself. Which would you prefer ?
How much experience of the Arduino do you have and what inspired you to try this project ?

I started a month ago , but I’m familiar with C++ language. It’s just that , this is kinda complicated. I’ve seen a lot of arduino projects and it amazed me. Using microcontroller is also fun.

The Arduino IDE uses C++ so that is a good start for you.
Whatever language it used the logic of what you want to do would be the same

initialise variables
set up environment

start of loop
  read from RFID reader
  if a valid card ID is found
    if the card ID matches a registered one 
      if the door is flagged as locked
        unlock the door
        output tones, print to serial monitor, whatever
        flag the door as unlocked
      end if  // end of locked door actions
      else
        lock the door
        output tones, print to serial monitor, whatever
        flag the door as locked
      end else  // end of unlocked locked door actions
    end if  // end of registered card actions
  end if // end of valid card actions
end of loop

You have the vast majority of this in the program already and only the sections that I previously indicated need changing.

thanks! I appreciate the help. Now I'm having troubles with the reading of serial port for vb6 , I mean , I can read what's being posted there (through vb6) but the results are in a single line horizontally. I tried adding "\n" on all Serial.println in my arduino sketch but no luck. Do you have any idea sir?? Here's the vb code:

EDIT: I also made a vb6 program to read the outputs from my arduino Uno. I'm improving it to a "Log manager"
soon if I can

Private Sub Form_Load()

With MSComm1
'make sure the serial port is not open (by this program)
If .PortOpen Then .PortOpen = False
'set the active serial port
.CommPort = 10
'set the badurate,parity,databits,stopbits for the connection
.Settings = "9600,N,8,1"
'set the DRT and RTS flags
.DTREnable = True
.RTSEnable = True
'enable the oncomm event for every reveived character
.RThreshold = 1
'disable the oncomm event for send characters
.SThreshold = 0
'open the serial port
.PortOpen = True
End With 'MSComm1

With Text5
'set the properties for the displaying textbox
.BackColor = vbCyan
.Locked = True
.Text = ""
End With 'Text1

Private Sub MSComm1_OnComm()

Dim strInput As String
Dim Dec_Value

With MSComm1
'test for incoming event
Select Case .CommEvent
Case comEvReceive
'display incoming event data to displaying textbox
strInput = .Input

Text5.SelText = strInput
End Select
End With 'MSComm1

End Sub