Help with RFID sketch

I have a sketch with two problems and I cant figure out y.
I have two push buttons on pin 11 and 12 they are suppose to control a relay on pin 8 and 9 (the other relays and bush button work ok) the relays go on and off alternately
The other thing is my RFID scanner reads a correct tag and is suppose to turn on a relay on pin 6. for a second. That works but it turns the relay on pause 3 seconds turns on again pause 3 then on again. When this happens relays on pin 8 and 9 go off.
If anyone can take a look and tell me what I did wrong
Rick

//  Push Buttons for Relay Number 1 -----> Pin 10
//  Push Buttons for Relay Number 3 -----> Pin 11
//  Push Buttons for Relay Number 4 -----> Pin 12

//  Photo Cell ----> Pin A0

//Relay Number 1 -----> Pin 6
//Relay Number 2 -----> Pin 7
//Relay Number 3 -----> Pin 8
//Relay Number 4 -----> Pin 9

//RFID Connection:
//ENABLE -----> Pin 2
//RFID TX ---- Pin RX
//VCC ----> 5v
//GND -----> Arduino GND



//Constants
#define NumberOfIDs  20  //This defines the amount of IDs in the access list
#define ON_TIME 1000         //time in milliseconds the relay #1 stays energized
#define ON_TIME_2 1000         //time in milliseconds the relay #2 stays energized
#define ON_TIME_3 1000         //time in milliseconds the relay #3 stays energized
#define ON_TIME_4 1000         //time in milliseconds the relay #4 stays energized
#define Relay2_operateValue 265

//External Equipment
#define ENABLE 2               //Pin connected to the Enable pin of RFID
#define RELAY1 6                  //Pin that is connected to the relay #1
#define RELAY2 7                 //Pin that is connected to the relay #2
#define RELAY3 8                 //Pin that is connected to the relay #3
#define RELAY4 9                  //Pin that is connected to the relay #4
#define RELAY1_PUSH_BUTTON 10
#define RELAY3_PUSH_BUTTON 11
#define RELAY4_PUSH_BUTTON 12

//Analog Input
#define PHOTO_CELL A0


int     val = 0; 
int     bytesread = 0;
String  Master = "120075A418";  //Stores the master ID used for programming the other IDs
String  IDCode;
String  accessList[NumberOfIDs]={"120075A418","12007389DA","690008BB23","6900076031","11003163FE","430051E429","0F032BBBD8","0F032BBA53"};  // !!!!  HERE YOU WILL ADD THE IDs !!!!! 
char    code[10];
boolean IDFound,photoCellstate= false;
int button1State,button3State,button4State =0;         // variable for reading the pushbutton status
int photoCellValue=1023;

void setup() {
  
pinMode(ENABLE,OUTPUT);
pinMode(RELAY1,OUTPUT);
pinMode(RELAY2,OUTPUT);
pinMode(RELAY3,OUTPUT);
pinMode(RELAY4,OUTPUT);
pinMode(RELAY1_PUSH_BUTTON, INPUT);
pinMode(RELAY3_PUSH_BUTTON, INPUT);
pinMode(RELAY4_PUSH_BUTTON, INPUT);

Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps 

digitalWrite(ENABLE, LOW);                  // LOW Activates the RFID reader, HIGH deactivates it
digitalWrite(RELAY1,LOW);
digitalWrite(RELAY2,LOW);
digitalWrite(RELAY3,LOW);
digitalWrite(RELAY4,LOW);

}  


 void loop() { 
    // read the state of the pushbutton value:
  button1State = digitalRead(RELAY1_PUSH_BUTTON);
  button3State = digitalRead(RELAY3_PUSH_BUTTON);
  button4State = digitalRead(RELAY4_PUSH_BUTTON);
  photoCellValue = analogRead(PHOTO_CELL);
  
   /**** EXTRACTION OF DATA FROM CARD ****/
  if(Serial.available() > 0) {            // if data available from reader 
    if((val = Serial.read()) == 10) {   // check for header [Header of data = 10]
      bytesread = 0; 
      
      while(bytesread<10) {              // read 10 digit code 
        if( Serial.available() > 0) { 
          val = Serial.read(); 
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading 
            break;                             // stop reading 
          } 
          code[bytesread] = val;      // add the digit
          bytesread++;                   // ready to read next digit
        } 
      } 
      if(bytesread == 10) {                  // if 10 digit read is complete
       IDCode = code;
       IDCode = IDCode.substring(0,10);
      }
      bytesread = 0; 
      digitalWrite(ENABLE, HIGH);                  // deactivate the RFID reader for a moment so it will not flood
      delay(1500);                       // wait for a bit 
      digitalWrite(ENABLE, LOW);                  // Activate the RFID reader
       
       
       /*** INTERPRETATION OF DATA ***/
       //At this point, data was read, and there is a IDcode in the IDCode variable     
       
          for(int i = 0;i<NumberOfIDs;i++)  //check entered ID against saved IDs
          {
             if(IDCode.equals(accessList[i])){
                IDFound = true;}                  //if the ID is in the access list, IDFound = true;
          }
          if(IDFound)
          {
          // energize Relay 1:    
              digitalWrite(RELAY1,HIGH);    //energize the relay 1
              delay(ON_TIME);            //Relay stays energized for ON_TIME milliseconds
              digitalWrite(RELAY1,LOW); //Immediately after delay, de-energize relay.
              IDFound = false;
          }
          else{
            digitalWrite(RELAY1,LOW);
          }
 
    }    //end of data reading

  }


 
   // check if the push buttons are pressed.
  // if it is, the button State is HIGH:
  
   /***Relay Number 1 control ***/
  if (button1State == HIGH) {     
    // energize Relay 1:    
      digitalWrite(RELAY1,HIGH);//energize the relay 1
  delay(ON_TIME);            //Relay stays energized for ON_TIME milliseconds
  digitalWrite(RELAY1,LOW); //Immediately after delay, de-energize relay.
  } 
  else {
    // de-energize Relay 1:
    digitalWrite(RELAY1, LOW); 
  }

   /***Relay Number 2 control ***/
  if ((photoCellValue <= Relay2_operateValue) && (photoCellstate==false)) {     
    // energize Relay 2:    
  digitalWrite(RELAY2,HIGH);//energize the relay 2
  delay(ON_TIME_2);            //Relay 2 stays energized for ON_TIME_2 milliseconds
  digitalWrite(RELAY2,LOW); //Immediately after delay, de-energize relay. 
  photoCellstate=true; 
  } 
    if (photoCellValue > Relay2_operateValue) {     
    // de-energize Relay 2:    
  digitalWrite(RELAY2,LOW); //Immediately after delay, de-energize relay. 
  photoCellstate=false; 
  } 

  
   /***Relay Number 3 control ***/
  if (button3State == HIGH) {     
    // energize Relay 3:    
  digitalWrite(RELAY3,HIGH);//energize the relay 3
  delay(ON_TIME_3);            //Relay 3 stays energized for ON_TIME_3 milliseconds
  digitalWrite(RELAY3,LOW); //Immediately after delay, de-energize relay.  
  } 
  else {
    // de-energize Relay 3:
    digitalWrite(RELAY3, LOW); 
  }
  
  /***Relay Number 4 control ***/
  if (button4State == HIGH) {     
    // energize Relay 4:    
  digitalWrite(RELAY4,HIGH);//energize the relay 4
  delay(ON_TIME_4);            //Relay 4 stays energized for ON_TIME_4 milliseconds
  digitalWrite(RELAY4,LOW); //Immediately after delay, de-energize relay. 
  } 
  else {
    // de-energize Relay 4:
    digitalWrite(RELAY4, LOW); 
  }
 
}

/*** CLEARS DATA FROM BUFFERS TO AVOID DOUBLE READS***/
void flushBuffers(){
   while(Serial.available() > 0)          //if there is still data remaining in the RFID reader buffer
       Serial.read();                         //read it into arduino buffer
   Serial.flush();                             //then flush it
}

You have probably by accident got your question included inside the code tags which makes it very difficult to read. You should modify your post to correct that.

You have an enormous account of code in loop() so I'm not suprised that you can't figure it out.

May I suggest that you reorganize your code into a collection of short functions each of which just has a single task. That will make the interactions easier to separate from each other. The demo several things at a time is an example of what I have in mind,

...R

My fault for the relays going on and off. Put pull down resistors on the switches and its ok now. Only thing is that when I use the RFID reader to read a valid tag it operates relay #1 2 or 3 times. I found out it depends on how long I leave the tag in front of the reader. Any way to stop that

I found out it depends on how long I leave the tag in front of the reader. Any way to stop that

Write the code correctly. Read the state change detection example for a clue.

Still having trouble when I read a tag the relay #1 its suppose to control operates twice instead of just once
I’ve tried to make some changes in the code with no luck. The code I have is mostly from other sketches I found on the net brought together and some of mine. This is my first attempt.
If you think the problem is still with the read state change detection.
can you give me an example of what you mean
Thanks
Rick

This is updated code

//  Push Buttons for Relay Number 1 -----> Pin 10
//  Push Buttons for Relay Number 3 -----> Pin 11
//  Push Buttons for Relay Number 4 -----> Pin 12

//  Photo Cell ----> Pin A0

//Relay Number 1 -----> Pin 6
//Relay Number 2 -----> Pin 7
//Relay Number 3 -----> Pin 8
//Relay Number 4 -----> Pin 9

//RFID Connection:
//ENABLE -----> Pin 2
//RFID TX ---- Pin RX
//VCC ----> 5v
//GND -----> Arduino GND



//Constants
#define NumberOfIDs  20  //This defines the amount of IDs in the access list
#define ON_TIME 1000         //time in milliseconds the relay #1 stays energized
#define ON_TIME_2 1000         //time in milliseconds the relay #2 stays energized
#define ON_TIME_3 1000         //time in milliseconds the relay #3 stays energized
#define ON_TIME_4 1000         //time in milliseconds the relay #4 stays energized
#define Relay2_operateValue 265

//External Equipment
#define ENABLE 2               //Pin connected to the Enable pin of RFID
#define RELAY1 6                  //Pin that is connected to the relay #1
#define RELAY2 7                 //Pin that is connected to the relay #2
#define RELAY3 8                 //Pin that is connected to the relay #3
#define RELAY4 9                  //Pin that is connected to the relay #4
#define RELAY1_PUSH_BUTTON 10
#define RELAY3_PUSH_BUTTON 11
#define RELAY4_PUSH_BUTTON 12

//Analog Input
#define PHOTO_CELL A0


int     val = 0; 
int     bytesread = 0;
String  IDCode;
String  accessList[NumberOfIDs]={"120075A418","12007389DA","690008BB23","6900076031","11003163FE","430051E429","0F032BBBD8","0F032BBA53"};  // !!!!  HERE YOU WILL ADD THE IDs !!!!! 
char    code[10];
boolean IDFound,photoCellstate= false;
int button1State,button3State,button4State =0;         // variable for reading the pushbutton status
int photoCellValue=1023;

void setup() {
  
pinMode(ENABLE,OUTPUT);
pinMode(RELAY1,OUTPUT);
pinMode(RELAY2,OUTPUT);
pinMode(RELAY3,OUTPUT);
pinMode(RELAY4,OUTPUT);
pinMode(RELAY1_PUSH_BUTTON, INPUT);
pinMode(RELAY3_PUSH_BUTTON, INPUT);
pinMode(RELAY4_PUSH_BUTTON, INPUT);

Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps 

digitalWrite(ENABLE, LOW);                  // LOW Activates the RFID reader, HIGH deactivates it
digitalWrite(RELAY1,LOW);
digitalWrite(RELAY2,LOW);
digitalWrite(RELAY3,LOW);
digitalWrite(RELAY4,LOW);

}  


 void loop() { 
  digitalWrite(ENABLE, LOW);                  // LOW Activates the RFID reader, HIGH deactivates it

   // read the state of the pushbutton value:
  button1State = digitalRead(RELAY1_PUSH_BUTTON);
  button3State = digitalRead(RELAY3_PUSH_BUTTON);
  button4State = digitalRead(RELAY4_PUSH_BUTTON);
  photoCellValue = analogRead(PHOTO_CELL);
  
   /**** EXTRACTION OF DATA FROM CARD ****/
  if(Serial.available() > 0) {            // if data available from reader 
    if((val = Serial.read()) == 10) {   // check for header [Header of data = 10]
      bytesread = 0; 
      
      while(bytesread<10) {              // read 10 digit code 
        if( Serial.available() > 0) { 
          val = Serial.read(); 
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading 
            break;                             // stop reading 
          } 
          code[bytesread] = val;      // add the digit
          bytesread++;                   // ready to read next digit
        } 
      } 
      if(bytesread == 10) {                  // if 10 digit read is complete
       IDCode = code;
       IDCode = IDCode.substring(0,10);
      }
      
      bytesread = 0; 
      digitalWrite(ENABLE, HIGH);                  // deactivate the RFID reader for a moment so it will not flood

             
       /*** INTERPRETATION OF DATA ***/
       //At this point, data was read, and there is a IDcode in the IDCode variable     
       
          for(int i = 0;i<NumberOfIDs;i++)  //check entered ID against saved IDs
          {
             if(IDCode.equals(accessList[i])){
                IDFound = true;}                  //if the ID is in the access list, IDFound = true;
          }
          if(IDFound)
          {
          // energize Relay 1:    
              digitalWrite(RELAY1,HIGH);    //energize the relay 1
              delay(ON_TIME);            //Relay stays energized for ON_TIME milliseconds
              digitalWrite(RELAY1,LOW); //Immediately after delay, de-energize relay.
              IDFound = false;
              
          }
          else{
            digitalWrite(RELAY1,LOW);
          }
 
    }    //end of data reading
    
      delay(4000);                                 // wait for a bit 

  }


 
   // check if the push buttons are pressed.
  // if it is, the button State is HIGH:
  
   /***Relay Number 1 control ***/
  if (button1State == HIGH) {     
    // energize Relay 1:    
      digitalWrite(RELAY1,HIGH);//energize the relay 1
  delay(ON_TIME);            //Relay stays energized for ON_TIME milliseconds
  digitalWrite(RELAY1,LOW); //Immediately after delay, de-energize relay.
  } 
  else {
    // de-energize Relay 1:
    digitalWrite(RELAY1, LOW); 
  }

   /***Relay Number 2 control ***/
  if ((photoCellValue < Relay2_operateValue) && (photoCellstate==false)) {     
    // energize Relay 2:    
  digitalWrite(RELAY2,HIGH);//energize the relay 2
  delay(ON_TIME_2);            //Relay 2 stays energized for ON_TIME_2 milliseconds
  digitalWrite(RELAY2,LOW); //Immediately after delay, de-energize relay. 
  photoCellstate=true; 
  } 
    if (photoCellValue > 400) {     
    // de-energize Relay 2:    
  digitalWrite(RELAY2,LOW); //Immediately after delay, de-energize relay. 
  photoCellstate=false; 
  } 

  
   /***Relay Number 3 control ***/
  if (button3State == HIGH) {     
    // energize Relay 3:    
  digitalWrite(RELAY3,HIGH);//energize the relay 3
  delay(ON_TIME_3);            //Relay 3 stays energized for ON_TIME_3 milliseconds
  digitalWrite(RELAY3,LOW); //Immediately after delay, de-energize relay.  
  } 
  else {
    // de-energize Relay 3:
    digitalWrite(RELAY3, LOW); 
  }
  
  /***Relay Number 4 control ***/
  if (button4State == HIGH) {     
    // energize Relay 4:    
  digitalWrite(RELAY4,HIGH);//energize the relay 4
  delay(ON_TIME_4);            //Relay 4 stays energized for ON_TIME_4 milliseconds
  digitalWrite(RELAY4,LOW); //Immediately after delay, de-energize relay. 
  } 
  else {
    // de-energize Relay 4:
    digitalWrite(RELAY4, LOW); 
  }
 
}

/*** CLEARS DATA FROM BUFFERS TO AVOID DOUBLE READS***/
void flushBuffers(){
   while(Serial.available() > 0)          //if there is still data remaining in the RFID reader buffer
       Serial.read();                         //read it into arduino buffer
   Serial.flush();                             //then flush it
}

I have updated the code in the original post

Well, put the original code back. Post your modified code in a reply. Do NOT change the first code you post.

      if(bytesread == 10) {                  // if 10 digit read is complete
       IDCode = code;
       IDCode = IDCode.substring(0,10);
      }

If there are 10 elements in the array, copy them into a String. Then, make sure that the String contains no more than, and no less than, 10 characters. Why is that necessary?

Why is it necessary to use Strings at all?

  button1State = digitalRead(RELAY1_PUSH_BUTTON);
  button3State = digitalRead(RELAY3_PUSH_BUTTON);
  button4State = digitalRead(RELAY4_PUSH_BUTTON);

How ARE these switches wired? Why are you not using the internal pullup resistors and simple wiring? You do have external pullup or pulldown resistors, right?

          for(int i = 0;i<NumberOfIDs;i++)  //check entered ID against saved IDs
          {
             if(IDCode.equals(accessList[i])){
                IDFound = true;}                  //if the ID is in the access list, IDFound = true;
          }

If the tag read matches the first one in the list, check the other 19 anyway. Why?

/*** CLEARS DATA FROM BUFFERS TO AVOID DOUBLE READS***/
void flushBuffers(){
   while(Serial.available() > 0)          //if there is still data remaining in the RFID reader buffer
       Serial.read();                         //read it into arduino buffer
   Serial.flush();                             //then flush it
}

Throw away random amounts of unread data, then block until all data sent TO the RFID reader has been sent. None of this crap is advisable.