Button relay controller in RFID project

Hey,
I'm setting up an electronic door lock using the mfrc522 board and a relay.
What I want is to be able to scan a tag to open the door and turn the lights on, and the door locks itself after a few seconds, then on the inside of the door have a button that will turn off the lights and unlock the door for a few seconds.
So far I am able to turn the lights on and unlock the door but the button does nothing at all. Opening the door at the moment requires a reset.

I have a button connected to pin 2 and 5v, with a 10k resistor between 2 and gnd.
I have the code for the button in the setup, and I'm stuck for ideas so need help.

  pinMode(lock, INPUT);
  if (digitalRead(lockUp) == LOW) {
    Serial.println("Locking now");
    digitalWrite(relay1, LOW);
    delay(5000);
    digitalWrite(relay2,HIGH);
    digitalWrite(relay1,HIGH);
    normalModeOn();
  }

That's what I have so far for the button, so I would appreciate some pointers.

Hi, as for solving the problem of button, you can have a look of the crowtail button that it can output logic HIHG when pressed and logic Low when released. The panel mount design makes it really suitable for installing on actual projects.

To find out the problem, you can have a look:
http://www.elecrow.com/wiki/index.php?title=Crowtail-_Button

for the code and other details you need to deal with this problem.

You should post the whole sketch... but if the button code is in setup() then that only runs once, after which the code in loop() runs over and over.

Full code is here;

#include <EEPROM.h>
#include <SPI.h>
#include <MFRC522.h>

#define COMMON_ANODE

#ifdef COMMON_ANODE
#else
#endif

#define relay1 8 //Lock controller
#define relay2 7 //Light controller
#define lockUp 3

boolean match = false;
boolean programMode = false;

int successRead;

byte storedCard[4];
byte readCard[4];
byte masterCard[4];

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup() {
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(lockUp, INPUT);
  digitalWrite(relay2, HIGH);
  digitalWrite(relay1,LOW);
  delay(5000);
  digitalWrite(relay1,HIGH);
  
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);

  pinMode(lockUp, INPUT);
  if (digitalRead(lockUp) == LOW) {
    Serial.println("Locking now");
    digitalWrite(relay1, LOW);
    delay(5000);
    digitalWrite(relay2,HIGH);
    digitalWrite(relay1,HIGH);
    normalModeOn();
    
  }
  if (EEPROM.read(1) != 143) {
    Serial.println("Scan tag define as Master.");
    Serial.println("...");
    do {
      successRead = getID();
    }
    while (!successRead);
    for ( int j = 0; j < 4; j++ ) {
      EEPROM.write( 2 +j, readCard[j] );
    }
    EEPROM.write(1,143);
    Serial.println("Master defined.");
    Serial.println("...");
  }
  Serial.println("RC522 Door Control");
  Serial.println("Master UID;");
  for ( int i = 0; i < 4; i++ ) {
    masterCard[i] = EEPROM.read(2+i);
    Serial.print(masterCard[i], HEX);
  }
  Serial.println("");
  Serial.println("Ready to scan.");
  Serial.println("...");
}


void loop () {
  do {
    successRead = getID();
    if (programMode) {
    }
    else {
      normalModeOn();
    }
  }
  while (!successRead);
  if (programMode) {
    if ( isMaster(readCard) ) {
      Serial.println("Master scanned."); 
      Serial.println("Leaving Program Mode.");
      Serial.println("...");
      programMode = false;
      return;
    }
    else {	
      if ( findID(readCard) ) {
        Serial.println("Known ID. Removing record.");
        Serial.println("...");
        deleteID(readCard);
      }
      else {
        Serial.println("Uknown ID. Creating record.");
        Serial.println("...");

        writeID(readCard);
      }
    }
  }
  else {
    if ( isMaster(readCard) ) {
      programMode = true;
      Serial.println("Master scanned.");
      Serial.println("Starting Program Mode.");
      Serial.println("...");
      int count = EEPROM.read(0);
      Serial.print(count);
      Serial.print(" record(s) on EEPROM.");
      Serial.println("");
      Serial.println("Scan tags to create or delete records.");
      Serial.println("...");

    }
    else {
      if ( findID(readCard) ) {
        Serial.println("Access granted.");
        Serial.println("...");
        openDoor(300);
      }
      else {
        Serial.println("Access denied.");
        Serial.println("...");
        failed(); 
      }
      
    }      
  }
}

int getID() {
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return 0;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return 0;
  }
  Serial.println("Tag UID;");
  for (int i = 0; i < 4; i++) {
    readCard[i] = mfrc522.uid.uidByte[i];
    Serial.print(readCard[i], HEX);
    delay(50);
  }
  Serial.println("");
  mfrc522.PICC_HaltA();
  return 1;
}


void normalModeOn () {
  digitalWrite(relay1, HIGH);
}

void readID( int number ) {
  int start = (number * 4 ) + 2;
  for ( int i = 0; i < 4; i++ ) {
    storedCard[i] = EEPROM.read(start+i);
  }
}

void writeID( byte a[] ) {
  if ( !findID( a ) ) {
    int num = EEPROM.read(0);
    int start = ( num * 4 ) + 6;
    num++;
    EEPROM.write( 0, num );
    for ( int j = 0; j < 4; j++ ) {
      EEPROM.write( start+j, a[j] );
    }
    successWrite();
  }
  else {
    failedWrite();
  }
}

void deleteID( byte a[] ) {
  if ( !findID( a ) ) {
    failedWrite();
  }
  else {
    int num = EEPROM.read(0);
    int slot;
    int start;
    int looping;
    int j;
    int count = EEPROM.read(0);
    slot = findIDSLOT( a );
    start = (slot * 4) + 2;
    looping = ((num - slot) * 4);
    num--;
    EEPROM.write( 0, num );
    for ( j = 0; j < looping; j++ ) {
      EEPROM.write( start+j, EEPROM.read(start+4+j));
    }
    for ( int k = 0; k < 4; k++ ) {
      EEPROM.write( start+j+k, 0);
    }
    successDelete();
  }
}

boolean checkTwo ( byte a[], byte b[] ) {
  if ( a[0] != NULL )
    match = true;
  for ( int k = 0; k < 4; k++ ) {
    if ( a[k] != b[k] )
      match = false;
  }
  if ( match ) {
    return true;
  }
  else  {
    return false;
  }
}

int findIDSLOT( byte find[] ) {
  int count = EEPROM.read(0);
  for ( int i = 1; i <= count; i++ ) {
    readID(i);
    if( checkTwo( find, storedCard ) ) {
      return i;
      break;
    }
  }
}

boolean findID( byte find[] ) {
  int count = EEPROM.read(0);
  for ( int i = 1; i <= count; i++ ) {
    readID(i);
    if( checkTwo( find, storedCard ) ) {
      return true;
      break;
    }
    else {
    }
  }
  return false;
}

void successWrite() {
  Serial.println("ID record created.");
  Serial.println("...");
}

void failedWrite() {
  Serial.println("Failed.");
  Serial.println("...");
}

void successDelete() {
  Serial.println("ID record deleted.");
  Serial.println("...");
}

boolean isMaster( byte test[] ) {
  if ( checkTwo( test, masterCard ) )
    return true;
  else
    return false;
}

void openDoor( int setDelay ) {
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
  delay(5000);
  digitalWrite(relay1, HIGH);
  delay(5);
}
  
void failed() {
  digitalWrite(relay1, HIGH);
  delay(1200);
}

I want the lockUp function to set relay1 to low for 10 seconds and then both relays to high. When I press the button nothing happens at all. Doesn't even show on the serial monitor.

I tried putting the button code in the loop function, but wherever it goes I get an error about too many functions.

How could I expand the openDoor code to include the instruction for the button, or would this not work.

Getting rather frustating that it won't work for me :confused: