Encoder motors coding

Hi, I am counting waves from motor and I am saving this information when motor is specific position.Lastly I want to move motors from random position to specific position.I wrote code and first motor working good but second motor making error I did not find it. Please help me.At least I waste one week and I did not succeed.

#include <EEPROM.h>

volatile int IRQcount1 = 0;
volatile int IRQcount2 = 0 ;

boolean motor1_direction = false;
boolean motor1_effect = true ;
boolean motor2_direction = false ;
boolean motor2_effect = true ;

boolean STOP_FORWARD_M1 =true;
boolean STOP_FORWARD_M2 =true; 
boolean STOP_BACKWARD_M1 =true;
boolean STOP_BACKWARD_M2 =true;

boolean  komok = true ;
boolean  enter = true ;



int pin2 = 3;    //interrupt pins
int pin1 = 2;    //interrupt pins
int motor_position[4]; //in here each array part represent each motor
int user1_m1= 0;
int user1_m2= 0;            // motor1--> motor_position[0]  ex
                             // motor1--> motor_position[0]  ex
int user2_m1= 0;            // motor1--> motor_position[0]  ex
int user2_m2= 0;            // motor1--> motor_position[0]  ex

const int buttonPin1 = A0;
const int buttonPin2 = A1;
const int buttonPin5 = A4;
const int buttonPin6 = A5;

const int buttonPin7 = 12;  //save button
const int buttonPin8 = 13;  //user1
const int buttonPin9 = 10;  //user2

int state1 = 0;       //bu stateler buttonlar için
int state2 = 0;
int state5 = 0;             
int state6 = 0;

int state7 = 0;
int state8 = 0;
int state9 = 0;

int M1_relayPin1 = 11;   //
int M1_relayPin2 = 4;
int M2_relayPin5 = 7;
int M2_relayPin6 = 8;

void setup() {

  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);

  pinMode(buttonPin7, INPUT);
  pinMode(buttonPin8, INPUT);
  pinMode(buttonPin9, INPUT);


  pinMode(M1_relayPin1, OUTPUT);
  pinMode(M1_relayPin2, OUTPUT);
  pinMode(M2_relayPin5, OUTPUT);
  pinMode(M2_relayPin6, OUTPUT);


  Serial.begin(9600);

  attachInterrupt(digitalPinToInterrupt(pin1), IRQcounter2, CHANGE);

  attachInterrupt(digitalPinToInterrupt(pin2), IRQcounter, CHANGE);

  //bu attachinterrotun anlamı herhanggi bir voltage değişikliği anında IRQcounter a git ve ordaki işlemi yap

}

void loop() {

  //EEPROM.write(motor_position[0]);
  //EEPROM.write(motor_position[1]);


  motor_position[0] = IRQcount1;
  motor_position[1] = IRQcount2;

  state1 = digitalRead(buttonPin1);
  state2 = digitalRead(buttonPin2);
  state5 = digitalRead(buttonPin5);
  state6 = digitalRead(buttonPin6);


  state7 = digitalRead(buttonPin7);
  state8 = digitalRead(buttonPin8);
  state9 = digitalRead(buttonPin9);



  if (state1 == HIGH) {
    
    STOP_FORWARD_M1=true;
    STOP_BACKWARD_M1=true;
    
     motor1_direction = false;
     motor1_effect = false ;
     digitalWrite(M1_relayPin1, LOW);
     digitalWrite(M1_relayPin2, HIGH);

    
  }

  else if (state7 == HIGH && state8 == HIGH) {

    user1_m1 = motor_position[0];
    user1_m2 = motor_position[1];

  }

  else if (state7 == HIGH && state9 == HIGH) {

    
    user2_m1 = motor_position[0];
    user2_m2 = motor_position[1];

  }

  else if (state2 == HIGH) {
    
    STOP_FORWARD_M1=true;
    STOP_BACKWARD_M1=true;
    
     motor1_direction = true;
     motor1_effect = true ;
     digitalWrite(M1_relayPin2, LOW);     
     digitalWrite(M1_relayPin1, HIGH);

 
  }
  
  else if (state5 == HIGH) {

    
    STOP_FORWARD_M2=true;
    STOP_BACKWARD_M2=true;
    
     motor2_direction = false;
     motor2_effect = false ;
     digitalWrite(M2_relayPin5, LOW);
     digitalWrite(M2_relayPin6, HIGH);


  }
  
  else if (state6 == HIGH) {
    
     motor2_direction = true;
     motor2_effect = true ;
     digitalWrite(M2_relayPin6, LOW);
     digitalWrite(M2_relayPin5, HIGH);

     
    STOP_FORWARD_M2=true;
    STOP_BACKWARD_M2=true;

  }
  else if (state8 == HIGH) {

   komok=false;
   enter=true;
   

    while(komok==false&& enter==true){
      
     komok=true;
     
  if((IRQcount1 < user1_m1)&&STOP_BACKWARD_M1){
    if((user1_m1-IRQcount1)<30) STOP_BACKWARD_M1=false;
         komok=false;
        Serial.println(" if 1111 ");   
        digitalWrite(M1_relayPin1, LOW);   
        digitalWrite(M1_relayPin2, HIGH);   

        motor1_direction =false;
        motor1_effect = false; 
      
  Serial.print("      IR1:");
  Serial.print(IRQcount1);

  Serial.print("        motor1:");
  Serial.print(motor_position[0]);

  Serial.print("    user1-m1:");
  Serial.print(user1_m1);
  
  Serial.print("      IR2:");
  Serial.print(IRQcount2);

  Serial.print("        motor2:");
  Serial.print(motor_position[1]);

  Serial.print("    user1-m2:");
  Serial.println(user1_m2);
                           
  }
  
  else if (STOP_FORWARD_M1==false)         digitalWrite(M1_relayPin2, HIGH);             
  else if (STOP_BACKWARD_M1==false)        digitalWrite(M1_relayPin1, HIGH);
  
 
  else if((IRQcount1 > user1_m1)&&STOP_FORWARD_M1){
    if((IRQcount1-user1_m1)<30) STOP_FORWARD_M1=false;
             komok=false;
       Serial.println(" if    2222 ");   
        digitalWrite(M1_relayPin2, LOW);
        digitalWrite(M1_relayPin1, HIGH);
        motor1_direction =true;
        motor1_effect = true;

  Serial.print("      IR1:");
  Serial.print(IRQcount1);

  Serial.print("        motor1:");
  Serial.print(motor_position[0]);

  Serial.print("    user1-m1:");
  Serial.print(user1_m1);
  
  Serial.print("      IR2:");
  Serial.print(IRQcount2);

  Serial.print("        motor2:");
  Serial.print(motor_position[1]);

  Serial.print("    user1-m2:");
  Serial.println(user1_m2);

  }  
   else if((IRQcount2 < user1_m2)&&STOP_BACKWARD_M2){
    if((IRQcount2-user1_m2)<30) STOP_BACKWARD_M2=false;
             komok=false;

        Serial.println(" if 3333 ");   
        digitalWrite(M2_relayPin5, LOW);
        digitalWrite(M2_relayPin6, HIGH);

        motor2_direction =true;
        motor2_effect = true; 
 

  Serial.print("      IR1:");
  Serial.print(IRQcount1);

  Serial.print("        motor1:");
  Serial.print(motor_position[0]);

  Serial.print("    user1-m1:");
  Serial.print(user1_m1);
  
  Serial.print("      IR2:");
  Serial.print(IRQcount2);

  Serial.print("        motor2:");
  Serial.print(motor_position[1]);

  Serial.print("    user1-m2:");
  Serial.println(user1_m2);
                     
  }
  
  else if (STOP_FORWARD_M2==false)         digitalWrite(M2_relayPin5, HIGH);             
  else if (STOP_BACKWARD_M2==false)        digitalWrite(M2_relayPin6, HIGH);

   else if((IRQcount2 > user1_m2)&&STOP_FORWARD_M2){
    if((IRQcount2-user1_m2)<30) STOP_FORWARD_M2=false;
             komok=false;

        Serial.println(" if 4444 ");   
        digitalWrite(M2_relayPin5, HIGH);
        digitalWrite(M2_relayPin6, LOW);

        motor2_direction =true;
        motor2_effect = true; 
 

  Serial.print("      IR1:");
  Serial.print(IRQcount1);

  Serial.print("        motor1:");
  Serial.print(motor_position[0]);

  Serial.print("    user1-m1:");
  Serial.print(user1_m1);
  
  Serial.print("      IR2:");
  Serial.print(IRQcount2);

  Serial.print("        motor2:");
  Serial.print(motor_position[1]);

  Serial.print("    user1-m2:");
  Serial.println(user1_m2);
                     
  }
  
//Serial.println("WHILEDAYIM");
  }

  }

  else if (state9 == HIGH) {

  }
  
  else {

    digitalWrite(M1_relayPin1, HIGH );
    digitalWrite(M1_relayPin2, HIGH);
    digitalWrite(M2_relayPin5, HIGH );
    digitalWrite(M2_relayPin6, HIGH);

       motor1_direction = false ;
       motor1_effect = true ;

       motor2_direction = false ;
       motor2_effect = true ;

        
  }



  //burada serial printleri düzenle


  Serial.print("      IR1:");
  Serial.print(IRQcount1);

  Serial.print("        motor1:");
  Serial.print(motor_position[0]);

  Serial.print("    user1-m1:");
  Serial.print(user1_m1);
  
  Serial.print("      IR2:");
  Serial.print(IRQcount2);

  Serial.print("        motor2:");
  Serial.print(motor_position[1]);

  Serial.print("    user1-m2:");
  Serial.println(user1_m2);

}

void IRQcounter() {

  if (motor1_direction ==false &&  motor1_effect == false)
 {
      //Serial.println("voiddeyim");

    IRQcount1++;

  }

// evet
  else if (motor1_direction== true && motor1_effect ==true)
  {

    IRQcount1--;

  }
  //Serial.println("void 1 deyim");
}
void IRQcounter2(){
  
  
  if (motor2_direction ==false && motor2_effect ==false)
  {

    IRQcount2++;

  }
  else if (motor2_direction == true && motor2_effect ==true)
  {

    IRQcount2--;

  }
  
  
  }

Moderator edit: code tags. Always code tags

Have you tried a simple code that only reads the encoder to make sure that the encoder, by itself, is working properly?

Yes I tried only on a motor its working truly but when I applied for two motor its not working.

Huge monolithic loop() function. Unreadable, to say the least.

Separate functions for separate things please, well-named, then loop can be a simple list of
calls to those functions.

state1, state2, state3 - meaningless names to me, and to you in 6 months time I reckon...

You are reading volatile variables without a critical section from you main code, which isn't reliable unless the
variable in question is a single byte (on ATmega processors). What if the interrupt happened right
between you reading each byte of the IRQcount1 variable? Nothing stops that currently.

"Won't work" tells us nothing. What does your code actually do? How is what it is doing different from what you want? Please read the "how to ask a programming question" sticky for hints on asking an effective question.

The aim is to move the motors forward and backward with the buttons(state1,state2,state5,state6) and count the squared wave from them with IRQcounter. Then, the motors register their steps in any position to a button(user1_m1 and user1_m2 this two for two motor position for user1 preference) in the form of user_1 option. To move from a position to the registered position when the registered key is pressed(user1 button is button8

Also,When I press the eighth key, it must go to position user1, but when doing this, the first motor does not stop at its own position, its running until second motor set up and stop itself.
generall problem is inside of this//////// else if (state8 == HIGH) {}//////
Thank you;

If the states form a natural sequence, use arrays. The code will become much smaller and simpler and
there will be fewer places for bugs to hide.