rfid doorlock and button switch merged but wont work as it should

Hi I am new to the forum and also pretty new to Arduino and Programming.
Since I started I learned a lot but as a newbie I have my problems.

I recreated a RFID doorlock with my own addons like a display which opens the door to my flat.
I am using a relay to open the door and as I used a dual relay module I also thought it would be a good idea to be able to switch on and of the venting and light in my toilet. Reasons are that I am living in an 250 year old house and power outlets are short, I want to change the lightswitch with an outlet and add a push button on the case I mounted next to the door which houses the arduino and the relays.

I merged the codes and it compiled just fine, but for some reason Iam only able to turn the relay on and off when the RFID is active ( holding the tag to the reader so the electric doorlock opens)

I am not sure whats the problem so I hope you guys can help me, sorry for my bad english.

The whole RFID stuff works like a charm, only the added button switch stuff isnt working as it should.

  //Arduino RFID Access Control

  

// adding a Pushbutton for the toilett light and ventilation

int pinButton = 8;
int Relay = 2;
int stateRelay = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 500;


#include <Wire.h>
#include <EEPROM.h>     // We are going to read and write PICC's UIDs from/to EEPROM
#include <SPI.h>        // RC522 Module uses SPI protocol
#include <MFRC522.h>  // Library for Mifare RC522 Devices
#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // added a lcd and works just fine




#define COMMON_ANODE

#ifdef COMMON_ANODE
#define LED_ON LOW
#define LED_OFF HIGH
#else
#define LED_ON HIGH
#define LED_OFF LOW
#endif

#define redLed 7    // Set Led Pins
#define greenLed 6
#define blueLed 5

#define relay 4     // Set Relay Pin
#define wipeB 3     // Button pin for WipeMode

boolean match = false;          // initialize card match to false
boolean programMode = false;  // initialize programming mode to false

int successRead;    // Variable integer to keep if we have Successful Read from Reader

byte storedCard[4];   // Stores an ID read from EEPROM
byte readCard[4];   // Stores scanned ID read from RFID Module
byte masterCard[4];   // Stores master card's ID read from EEPROM

/*
  We need to define MFRC522's pins and create instance
  Pin layout should be as follows (on Arduino Uno):
  MOSI: Pin 11 / ICSP-4
  MISO: Pin 12 / ICSP-1
  SCK : Pin 13 / ICSP-3
  SS : Pin 10 (Configurable)
  RST : Pin 9 (Configurable)
  look MFRC522 Library for
  other Arduinos' pin configuration
*/

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

///////////////////////////////////////// Setup ///////////////////////////////////
void setup() {
  

  lcd.begin(20, 4);   // LCD stuff again, works fine
  lcd.noBacklight();
  delay(250);

  pinMode(pinButton, INPUT); //Buttonswitch stuff that wont work as it should
  pinMode(Relay, OUTPUT);
  
  //Arduino Pin Configuration
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(wipeB, INPUT_PULLUP);   // Enable pin's pull up resistor
  pinMode(relay, OUTPUT);
  //Be careful how relay circuit behave on while resetting or power-cycling your Arduino
  digitalWrite(relay, HIGH);    // Make sure door is locked
  digitalWrite(redLed, LED_OFF);  // Make sure led is off
  digitalWrite(greenLed, LED_OFF);  // Make sure led is off
  digitalWrite(blueLed, LED_OFF); // Make sure led is off

  //Protocol Configuration
  Serial.begin(9600);  // Initialize serial communications with PC
  SPI.begin();           // MFRC522 Hardware uses SPI protocol
  mfrc522.PCD_Init();    // Initialize MFRC522 Hardware

  //If you set Antenna Gain to Max it will increase reading distance
  //mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);

  Serial.println(F("Access Control v3.3"));   // For debugging purposes
  ShowReaderDetails();  // Show details of PCD - MFRC522 Card Reader details

  //Wipe Code if Button Pressed while setup run (powered on) it wipes EEPROM
  if (digitalRead(wipeB) == LOW) {  // when button pressed pin should get low, button connected to ground
    digitalWrite(redLed, LED_ON); // Red Led stays on to inform user we are going to wipe
    Serial.println(F("Wipe Button Pressed"));
    Serial.println(F("You have 5 seconds to Cancel"));
    Serial.println(F("This will be remove all records and cannot be undone"));
    delay(5000);                        // Give user enough time to cancel operation
    if (digitalRead(wipeB) == LOW) {    // If button still be pressed, wipe EEPROM
      Serial.println(F("Starting Wiping EEPROM"));
      for (int x = 0; x < EEPROM.length(); x = x + 1) {    //Loop end of EEPROM address
        if (EEPROM.read(x) == 0) {              //If EEPROM address 0
          // do nothing, already clear, go to the next address in order to save time and reduce writes to EEPROM
        }
        else {
          EEPROM.write(x, 0);       // if not write 0 to clear, it takes 3.3mS
        }
      }
      Serial.println(F("EEPROM Successfully Wiped"));
      digitalWrite(redLed, LED_OFF);  // visualize successful wipe
      delay(200);
      digitalWrite(redLed, LED_ON);
      delay(200);
      digitalWrite(redLed, LED_OFF);
      delay(200);
      digitalWrite(redLed, LED_ON);
      delay(200);
      digitalWrite(redLed, LED_OFF);
    }
    else {
      Serial.println(F("Wiping Cancelled"));
      digitalWrite(redLed, LED_OFF);
    }
  }
  // Check if master card defined, if not let user choose a master card
  // This also useful to just redefine Master Card
  // You can keep other EEPROM records just write other than 143 to EEPROM address 1
  // EEPROM address 1 should hold magical number which is '143'
  if (EEPROM.read(1) != 143) {
    Serial.println(F("No Master Card Defined"));
    Serial.println(F("Scan A PICC to Define as Master Card"));
    do {
      successRead = getID();            // sets successRead to 1 when we get read from reader otherwise 0
      digitalWrite(blueLed, LED_ON);    // Visualize Master Card need to be defined
      delay(200);
      digitalWrite(blueLed, LED_OFF);
      delay(200);
    }
    while (!successRead);                  // Program will not go further while you not get a successful read
    for ( int j = 0; j < 4; j++ ) {        // Loop 4 times
      EEPROM.write( 2 + j, readCard[j] );  // Write scanned PICC's UID to EEPROM, start from address 3
    }
    EEPROM.write(1, 143);                  // Write to EEPROM we defined Master Card.
    Serial.println(F("Master Card Defined"));
  }
  Serial.println(F("-------------------"));
  Serial.println(F("Master Card's UID"));
  for ( int i = 0; i < 4; i++ ) {          // Read Master Card's UID from EEPROM
    masterCard[i] = EEPROM.read(2 + i);    // Write it to masterCard
    Serial.print(masterCard[i], HEX);
  }
  Serial.println("");
  Serial.println(F("-------------------"));
  Serial.println(F("Everything Ready"));
  Serial.println(F("Waiting PICCs to be scanned"));
  cycleLeds();    // Everything ready lets give user some feedback by cycling leds
}


///////////////////////////////////////// Main Loop ///////////////////////////////////

 // I did put the loop on first place so I thought it would not get in the way, but it looks like it somehow does :( 

void loop () {
{
stateButton = digitalRead(pinButton);  
  if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
    if(stateRelay == HIGH){
      stateRelay = LOW; 
    } else {
       stateRelay = HIGH; 
    }
    time = millis();
  }
  digitalWrite(Relay, stateRelay);
  previous == stateButton;
 
  }
  do {
    successRead = getID();  // sets successRead to 1 when we get read from reader otherwise 0
    if (programMode) {
      cycleLeds();              // Program Mode cycles through RGB waiting to read a new card

...................................
    
There is plenty more code but I am not allowed to post more than 9000characters :(

There is plenty more code but I am not allowed to post more than 9000characters :frowning:

But you could attach it.

#define relay 4     // Set Relay Pin

Of course, your code would be a lot smaller if you missed-out the pointless comments.

Sorry, missed the attach option below.

I am sorry but I am a new to all of this and comments are pretty important to understand the whole thing.

rfidklolichtalternative.ino (21.2 KB)

LupusWorax:
comments are pretty important to understand the whole thing.

Good comments are. Stating the bleeding obvious is a waste of space.

See down towards the bottom where he gets to comments.

So after we cleared what I have done wrong in " how to perfectly write a code as a newbie" and how to " just leave away the pointless comments " nobody is still able to help me with my problem?
This Forum sector should be renamed from " project Guidance" to " how to write, so pro programmers feel comfortable"
pretty sad to see, but a friend of mine was right, programmers are mostly unfriendly as hell.

Thanks for no help at all, gonna lock for a better place where maybe someone really wants to help and not only state how childish or dumb someones code looks like.

This is Arduino and I guess its meant for newbies like me, thats the whole idea behind it, isnt it.
Well not with help from you obviously.

Well, it goes like this; mostly I post from my phone or my tablet, neither of which has a decent code editor, so, if I can't see the code embedded in the post, I leave replying to someone who can read the code, or until I get to my laptop or desktop.

Newbie code that reached 21k is going to take some picking through.

Meanwhile, I'll just pick up all the toys you threw out of your pram.

Hallo, I'm using same code to make a RFID doorlock from omnesiar, And I'm tried to add strike push button to, if you still need the code, here the code :

(Just for add button)

==============================================

#define button 3
#define relay 2

Void Setup() {
pinMode(button, INPUT_PULLUP);
pinMode(relay, OUTPUT);

Void Loop() {

// add on first and inside "do" fuction

If (digitalRead(button) == HIGH) {
digitalWrite(relay, HIGH);
}
else {
digitalWrite(relay, LOW);
}

}

=============================================

The code and wire config is the same with wipeB

If you have any question, you can send email to me moroisaanes@gmail.com