Programming a button to lock the door?

Hello,

I have a code set up and is currently working, except for the button. I wanted the button to be programmed to lock the door when pressed, and be unlocked when released. The idea is to have a rfid card, and when scanned the right one, it unlocks the door. The button will sense when the door is closed, and lock it again, and when open, print a door opened message on the lcd. Take a look at my code:

#include <Button.h>
#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>
#include <Relay.h>


#define RST_PIN   9
#define SS_PIN    10
byte readCard[4];
char* myTags[100] = {};
int tagsCount = 0;
String tagID = "";
boolean successRead = false;
boolean correctTag = false;
boolean doorOpened = false;
// Create instances
MFRC522 mfrc522(SS_PIN, RST_PIN);
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7); //Parameters: (rs, enable, d4, d5, d6, d7)
Relay solenoid(8, false); // Solenoid
Button lockac (1);
void setup() {
  // Initiating
  SPI.begin();        // SPI bus
  mfrc522.PCD_Init(); //  MFRC522
  lcd.begin(16,2);   // LCD screen
  solenoid.begin();  // Solenoid
  lcd.setBacklightPin(3, POSITIVE);
  lcd.setBacklight(HIGH);
  pinMode(8, OUTPUT);
  lockac.begin();
  // Prints the initial message
  lcd.print("-No Master Tag-");
  lcd.setCursor(0, 1);
  lcd.print("Scan new card");
  // Waits until a master card is scanned
  while (!successRead) {
    successRead = getID();
    if ( successRead == true) {
      myTags[tagsCount] = strdup(tagID.c_str()); // Sets the master tag into position 0 in the array
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("-Master Tag Set-");
      tagsCount++;
    }
  }
  successRead = false;
  printNormalModeMessage();
}
void loop() {
  int (lockac.read()== Button::PRESSED);
  // If door is closed...
  if (lockac.pressed()) {
    if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
      return;
    }
    if ( ! mfrc522.PICC_ReadCardSerial()) {   //Since a PICC placed get Serial and continue
      return;
    }
    tagID = "";
    // The MIFARE PICCs that we use have 4 byte UID
    for ( uint8_t i = 0; i < 4; i++) {  //
      readCard[i] = mfrc522.uid.uidByte[i];
      tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
    }
    tagID.toUpperCase();
    mfrc522.PICC_HaltA(); // Stop reading
    correctTag = false;
    // Checks whether the scanned tag is the master tag
    if (tagID == myTags[0]) {
      lcd.clear();
      lcd.print("-Program mode-:");
      lcd.setCursor(0, 1);
      lcd.print("Add/Remove Tag");
      while (!successRead) {
        successRead = getID();
        if ( successRead == true) {
          for (int i = 0; i < 100; i++) {
            if (tagID == myTags[i]) {
              myTags[i] = "";
              lcd.clear();
              lcd.setCursor(0, 0);
              lcd.print("-Tag Removed-");
              printNormalModeMessage();
              return;
            }
          }
          myTags[tagsCount] = strdup(tagID.c_str());
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("-Tag Added-");
          printNormalModeMessage();
          tagsCount++;
          return;
        }
      }
    }
    successRead = false;
    // Checks whether the scanned tag is authorized
    for (int i = 0; i < 100; i++) {
      if (tagID == myTags[i]) {
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("-Access Granted-");
        solenoid.turnOn(); // Unlocks the door
        printNormalModeMessage();
        correctTag = true;
      }
    }
    if (correctTag == false) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("-Access Denied-");
      printNormalModeMessage();
    }
  }
  // If door is open...
  else {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("-Door Opened-");
    lcd.setCursor(0, 1);
    lcd.print("Please Close");
    while (!doorOpened) { //<---- added a curly bracket here
      lockac.read()== Button::RELEASED;
      if (lockac.released()) {
        doorOpened = true;
      }//} <---- removed a curly bracket here
    }
    if (doorOpened = false);
    (lockac.read()== Button::PRESSED);
    delay(500);
    solenoid.turnOff(); // Locks the door
    printNormalModeMessage();
   }
  }
uint8_t getID() {
  // Getting ready for Reading PICCs
  if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
    return 0;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {   //Since a PICC placed get Serial and continue
    return 0;
  }
  tagID = "";
  for ( uint8_t i = 0; i < 4; i++) {  // The MIFARE PICCs that we use have 4 byte UID
    readCard[i] = mfrc522.uid.uidByte[i];
    tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
  }
  tagID.toUpperCase();
  mfrc522.PICC_HaltA(); // Stop reading
  return 1;
}
void printNormalModeMessage() {
  delay(1500);
  lcd.clear();
  lcd.print("-System Ready-");
  lcd.setCursor(0, 1);
  lcd.print("Scan Card");
}

I have tried multiple things, but yet it won’t work. The lcd reads that it is released, meaning that it is opened. However, when pressed, to simulate the door closing, it won’t lock, and still reads “door opened”.

  int (lockac.read()== Button::PRESSED);

What is this rubbish? You are comparing the value returned by the read() method to the PRESSED value, and creating an int from the true or false that results, but not storing it anywhere. So, this was a complete waste of the effort to hunt and peck it out.

      lockac.read()== Button::RELEASED;

Another useless comparison.

    if (doorOpened = false);

You assigned false to doorOpened, resulting in false. Then you do nothing if the result is true and nothing if the result is false. Therefore, more useless hunting and pecking.

PaulS-

Ok, so I got rid of what you call "rubbish." (or just a beginner trying to find a way to solve the problem with whats included in the library) However, the doorOpened statement IS doing something, at the end, if it is false, meaning the door is closed, it delays 500, locks the door, and prints the original message. If it is true, it has a while statement, where it displays "open door." Maybe I am misunderstanding you, and correct me if I am. Sorry if I inconvenienced you with a "waste of effort to hunt and peck it out," because I sure didn't realize this. So, in a way, it did help me, therefore, not a waste of effort. I am just a beginner, trying to feel my way around. If you can't help, that is fine. Thank you for your time.

-Sam

Welcome to the forum. Sometimes frustration levels jaundice the more experienced eye,

if (doorOpened = false);

is not a test, it's an assignment (== vs =).

Often a careful review, coupled with good code formatting (judicious use of white space and tabs) will reveal problems even prior to posting.

if (doorOpened = false);

And what does the **; **do for you?

Thank you DKWatson and larryd for cooparating.

I deleted the "if" statement, and before I did this I tried the "if" statement without the ;. However, either way, it still won't work. Is there something big that I am missing here? Before I messed with the code to add some products to it, it did not have a "if" statement, so I thought I would return it to that state. Thank you all for helping me, as I know it probably is frustrating.

-Sam

If you've changed your code, re-post it. I don't think anybody is interested in doing their own version control. It is also helpful to see the error messages. If it's a logic problem, tell us where you think it is and what it should be doing that it isn't. Quite often the path to problem resolution is found in clearly defining what the problem is.

Thank you DKWatson-

I have changed the code so that it will rule out the button. I have given up on it, and have found another way around it. Thank you to all who contributed to this forum. I deeply appreciate the fact that people are willing to help. Thanks again!

-Sam