Do two things at the same time

Hi, i’ve been strugling about this, my problem is that i have an RFID and a SWITCH button, the arduino reads the switch and then waits for the RFID to read a card…
what i want is that it can do both things at the same time, so if a tag is presented it opens the door, and if the switch is pressed it opens the door too.
Any help would be appreciated.

#include <LCD4Bit.h> 
/**
 * author Benjamin Eckel
 * date 10-17-2009
 *
 */
#define RFID_ENABLE 10   //to RFID ENABLE
#define CODE_LEN 10      //Max length of RFID tag
#define VALIDATE_TAG 1  //should we validate tag?
#define VALIDATE_LENGTH  200 //maximum reads b/w tag read and validate
#define ITERATION_LENGTH 2000 //time, in ms, given to the user to move hand away
#define START_BYTE 0x0A 
#define STOP_BYTE 0x0D
char tag[CODE_LEN];  
char validos[11] = "0F0302867B";
char validos2[11] = "0415D95D69";

int switchPin = 14;      //  digital input pin for a switch
int ledpin = 13;
int RED = 4;
int GREEN = 2;
int BLUE = 3;
int doorPin = 12;
LCD4Bit lcd = LCD4Bit(2); 
int switchState = 0;    // the state of the switch

void setup() { 
  Serial.begin(2400);
  pinMode(RFID_ENABLE,OUTPUT);   
  pinMode(ledpin, OUTPUT);
  pinMode(switchPin, INPUT);       // set the switch pin to be an input
  pinMode(doorPin, OUTPUT); 
  digitalWrite(doorPin, LOW); // lock door
  pinMode(RED, OUTPUT); 
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT); 
  inicialize();
  lcd.init();
  lcd.commandWrite(0x0F);
  lcd.printIn("BEM-VINDO!");
  delay(3000);
  lcd.clear();
  lcdMessage();
}
 
void loop() {
  interrupts();
  lerSwitch();
  enableRFID(); 
  getRFIDTag();
  if(isCodeValid()) {
    disableRFID();
    sendCode();
    eValido();
    delay(ITERATION_LENGTH);
  } else {
    disableRFID();
    Serial.println("Got some noise");  
  }
  Serial.flush();
  digitalWrite(GREEN, LOW);
  digitalWrite(RED, LOW);
  digitalWrite(BLUE, HIGH);
  clearCode();
} 

void lcdMessage(){
  lcd.printIn("   Apresente   "); 
  lcd.cursorTo(2, 0);
  lcd.printIn("      ID!      "); 
}

void lerSwitch() {
  switchState = digitalRead(switchPin);
  Serial.println("switch:");
  Serial.println(switchState);
  if (switchState==1) { // if the switch is closed
    digitalWrite(doorPin, HIGH);
    digitalWrite(ledpin, HIGH);
    delay(9000);
    digitalWrite(ledpin, LOW);
    digitalWrite(doorPin, LOW);
  }
}

void eValido() {
  if (checkTag(validos) || checkTag(validos2)) {
    lcd.clear();
    lcd.printIn("       ID       ");
    lcd.cursorTo(2, 0);
    lcd.printIn("     VALIDA!     ");
    sucess();
    lcd.clear();
    lcdMessage();
  } else {
    lcd.clear();
    lcd.printIn("       ID       ");
    lcd.cursorTo(2, 0);
    lcd.printIn("    INVALIDA!    ");
    failed();
    lcd.clear();
    lcdMessage();
  }
}

boolean checkTag(char *validos){
  for (int x=0;x<10;x++){
    if( validos[x] != tag[x]){
      return false;
    }
  }
  return true;
}

void failed()
{
  digitalWrite(doorPin, LOW);
  digitalWrite(GREEN, LOW);   // Make sure green LED is off
  digitalWrite(BLUE, LOW);  // Make sure blue LED is off
  // Blink red fail LED 3 times to indicate failed key
  for(int i=0; i<5; i++) {
    digitalWrite(RED, HIGH);  // Turn on red LED
    delay(500);
    digitalWrite(RED, LOW);   // Turn off red LED
    delay(500);
  }
}

void sucess()
{     
  digitalWrite(doorPin, HIGH);
  digitalWrite(RED, LOW);
  digitalWrite(BLUE, LOW);
  digitalWrite(GREEN, HIGH);
  delay(9000);
  digitalWrite(GREEN, LOW);
  digitalWrite(doorPin, LOW);
}

void inicialize() {
  for(int i=0; i<8; i++)
  {
    digitalWrite(BLUE, HIGH);
    delay(100);
    digitalWrite(BLUE, LOW);
    delay(150);
  }
  digitalWrite(BLUE, HIGH);
}

/**
 * Clears out the memory space for the tag to 0s.
 */
void clearCode() {
  for(int i=0; i<CODE_LEN; i++) {
    tag[i] = 0; 
  }
}
 
/**
 * Sends the tag to the computer.
 */
void sendCode() {
    Serial.print("TAG:");  
    //Serial.println(tag);
    for(int i=0; i<CODE_LEN; i++) {
      Serial.print(tag[i]); 
    } 
}
 
/**************************************************************/
/********************   RFID Functions  ***********************/
/**************************************************************/
 
void enableRFID() {
   digitalWrite(RFID_ENABLE, LOW);    
}
 
void disableRFID() {
   digitalWrite(RFID_ENABLE, HIGH);  
}
 
/**
 * Blocking function, waits for and gets the RFID tag.
 */
void getRFIDTag() {
  byte next_byte; 
  while(Serial.available() <= 0) {}
  if((next_byte = Serial.read()) == START_BYTE) {      
    byte bytesread = 0; 
    while(bytesread < CODE_LEN) {
      if(Serial.available() > 0) { //wait for the next byte
          if((next_byte = Serial.read()) == STOP_BYTE) break;       
          tag[bytesread++] = next_byte;                   
      }
    }                
  }    
}
 
/**
 * Waits for the next incoming tag to see if it matches
 * the current tag.
 */
boolean isCodeValid() {
  byte next_byte; 
  int count = 0;
  while (Serial.available() < 2) {  //there is already a STOP_BYTE in buffer
    delay(1); //probably not a very pure millisecond
    if(count++ > VALIDATE_LENGTH) return false;
  }
  Serial.read(); //throw away extra STOP_BYTE
  if ((next_byte = Serial.read()) == START_BYTE) {  
    byte bytes_read = 0; 
    while (bytes_read < CODE_LEN) {
      if (Serial.available() > 0) { //wait for the next byte      
          if ((next_byte = Serial.read()) == STOP_BYTE) break;
          if (tag[bytes_read++] != next_byte) return false;                     
      }
    }                
  }
  return true;   
}

Very briefly, you have two options: Interrupt/event driven programs, or non-blocking polling. For the Arduino I prefer the latter.

Write a couple of routines, checking the state of the switch and RFID, repectivly, and opening the door. No routine may use delay() (or only very short intervals) nor wait for something. This is non-blocking.

Where you need a time-interval in these routines, note the millis() when you "start" your timer, and next time you enter the same routine you see you have a "running timer", and just check if millis() now is less than your interval where you exit, or you do what needed doing.

Your loop() now simply calls all these routines without any timing or logic. The routines can modify global variables, f.ex. "switch is down", which the indvidual routines use to implement the right logic.

I wrote a sketch that did temperature control, two DC motors feed active feedback, and both Serial output and input parsing commands - at the "same time" - using this techinque. The main loop executed 100us at the most, as the routines only do a little each time and never block.