RFID controlled servo - please help

Hello,
I am completely new here and just needed to use Arduino for this one project. I have a case with an RFID lock. When a correct card is placed to the reader, the servo moves the lock to open the case. Now, I have this code:

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

RFID rfid(10, 9);

byte kart[5] = {237,68,25,198,123};
Servo myservo;
boolean card;

void setup()
{

  Serial.begin(9600);
  SPI.begin();
  rfid.init();
  myservo.attach(3);
  myservo.write(95);

}

void loop()
{

  if (rfid.isCard())
  {


    if (rfid.readCardSerial())
    {
      Serial.print("Found ID: ");
      Serial.print(rfid.serNum[0]);
      Serial.print(",");
      Serial.print(rfid.serNum[1]);
      Serial.print(",");
      Serial.print(rfid.serNum[2]);
      Serial.print(",");
      Serial.print(rfid.serNum[3]);
      Serial.print(",");
      Serial.println(rfid.serNum[4]);

    }
    for (int i = 1; i < 5; i++)
    {
      if (rfid.serNum[0] == kart[0] && rfid.serNum[1] == kart[1] && rfid.serNum[2] == kart[2] && rfid.serNum[3] == kart[3] && rfid.serNum[4] == kart[4])
      {
        card = true;
      }
      else {
        card = false;
      }
    }
    if (card == true)
    {
      Serial.println("Correct Card");
      myservo.write(-95);
      delay(5000);
      myservo.write(95);
    }
    else
    {
      Serial.println("Wrong Card");

    }
    rfid.halt();
  }
}

What it does it opens the lock, waits 5 seconds and then closes. What I want it to do is to open and then wait until the card is put to the reader the second time to close it. I tried to search for some kind of “wait until sth happens” function or something but I am completely green in this and had no idea how to apply it.

Any help would be appreciated, thank you in advance :slight_smile:

Change the state of a boolean each time the correct card is read If the boolean becomes true then open the lock. If it becomes false then close the lock

And how do I do that?

Start by declaring a global boolean variable. Let's name it open and set its initial value to false. Then when you determine that the card is the correct one set open to !open to toggle its value. Later in loop() test the value of open and if it is true open the lock else close the lock

NOTE : this not quite what I suggested because it detects when the boolean [u]is[/u] true rather than when if [u]becomes[/u] true, but it will do as a start

I am sorry but I don't understand. Could you tell me how to apply it in the code i posted?

I am sorry but I don't understand.

Which part ?

Could you tell me how to apply it in the code i posted?

You already have code to declare a boolean variable, read the card, determine if it is the target card and move the servo (apart from the fact that write() takes the target angle, 0 to 180, as its argument)

What is the for loop for?

I have no idea about programming, I found this code on the internet, on the website where I found it the author was a user Arca_Ege (to give the man credit).

UKHeliBob: Then when you determine that the card is the correct one set open to !open to toggle its value.

Which is where?

UKHeliBob: Later in loop() test the value of open and if it is true open the lock else close the lock

Where is later?

TheMemberFormerlyKnownAsAWOL: What is the for loop for?

I don't know