How to control multiple leds using RFID

We are using two different tags and we want both tags to light up a different led.

The led should turn off after five seconds. The leds should turn off despite how many leds are on or off.

At the moment, we only get the code to work so that the leds turn off (after five seconds) only if both leds are on. If only one led is on, it wont turn off.

We are planning to use 3 leds and 3 tags (one tag for one led).

The current code

#include <MFRC522.h>
#include <SPI.h>
#include <avr/wdt.h>
#define SS_PIN 10
#define RST_PIN 9

const int LED1 = 4;
const int LED2 = 5;
int ledState1 = LOW;
int ledState2 = LOW;
unsigned long startTime1 = 0;
unsigned long startTime2 = 0;

MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup()
{
 Serial.begin(9600);   // Initiate a serial communication
 SPI.begin();      // Initiate  SPI bus
 mfrc522.PCD_Init();   // Initiate MFRC522
 pinMode(LED1, OUTPUT);
 pinMode(LED2, OUTPUT);
 digitalWrite(LED1, LOW);
 digitalWrite(LED2, LOW);
 Serial.println("Put your card to the reader...");
 Serial.println();
 MCUSR = 0;
 
}
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) == "15 00 CB 05") {
   Serial.println("Authorized access");
   Serial.println();
   delay(20);
   ledState1 = HIGH;
   digitalWrite(LED1, ledState1);
   startTime1 = millis();
   
 }

if (content.substring(1) == "20 50 EA 53") {
   Serial.println("Authorized access");
   Serial.println();
   delay(20);
   ledState2 = HIGH;
   digitalWrite(LED2, ledState2);
   startTime2 = millis();
 }
 
if (ledState1 == HIGH && ledState2 == HIGH){
 delay(5000);
 ledState1 = LOW;
 ledState2 = LOW;
 digitalWrite(LED2, ledState2);
 digitalWrite(LED1, ledState1);
 }
 
 else if ((ledState1 == HIGH && ledState2 == LOW) && (millis() > startTime1 + 5000)){
 ledState1 = LOW;
 digitalWrite(LED1, ledState1);
 }
 
  else if (ledState2 == HIGH && ledState1 == LOW && (millis() > startTime2 + 5000)){
 ledState2 = LOW;
 digitalWrite(LED2, ledState2);
 
 }
 
}

See how your code turns into italics half way through. That is because the normal forum software mistakes some code as instructions to change the text. This is why we ask you to use code tags.

You should have been directed to this page when you created this topic so please reread it:-
How to get the best out of this forum
And modify that first post to use them.

Using delay in your code stops other things from working so if you want it to behave then you will have to write the code as a state machine and remove all the delays.

See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once