Controlling a movement of the cart using a motor

Hello,

I am new using Arduino. My project is to build a cart moved by a motor controlled by an inverter. The program will control the inputs for the inverter and is receiving feedback from potentiometer and end switches. Acording to the program selection he will move in different direction. The problem that I have is controlling the function "miscarestdrststop" (the cart is moving from left to right stop and after that from right to left and stop; program ends) and the function "miscarestdrcontinua" ( the cart is moving continuous from left to right untill stop is off push button is activated).
The cart after executing the movement from left to right he is not moving by itself from right to left. He stays there.
If you can help me with this, it will be very much apreciated. Thanks.

First part of the program :

/*
 *  Program for controlling the cart movement 
 */



int switchParcare = 3;        // switch parking connected to pin 3
int switchCapStanga = 4;      // switch left connected to pin 4
int switchCapDreapta = 5;     // switch right connected to pin 5
int progrP1 = 6;              // selection program P1 parking position connected to pin 6
int progrP2 = 7;              // selection program P2 midlle fixed position connected to pin 7
int progrP3 = 8;              // selection program P3 left to right and stop connected to pin 8
int progrP4 = 9;              // selection program P4 left to right, right to left and stop connected to pin 9
int progrP5 = 10;             // selection program P5 continuous movement left to right and reverse connected to pin 10
int progrP6 = 19;             // selection program P6 departure position connected to pin 19
int switchPornit = 11;        // switch start of the movement connected to pin 11
int switchOprit = 12;         // switch stop of the movement connected to pin 12

// Commands for electromagnetic brake of the motor and the inverter that controls the motor direction, speed and on/off

int comandaFrana = 13;        // command for electromagnetic brake connected to pin 13
int comandaPOinv = 15;        // command for on/off motor connected to pin 15 
int comandaSens = 16;         // command for direction of the motor connected to pin 16 
int comandaSelV1 = 17;        // command for speed selection V1 connected to pin 17 
int comandaSelV2 = 18;        // command for speed selection V2 connected to pin 18 


int potMotor = 0;             // potentiometer position of the cart controlled by the motor to analog pin 0





int valP1;                        // variable for storing the value of the selection program P1 (parking)
int valP2;                        // variable for storing the value of the selection program P2 (fixed position midlle)
int valP3;                        // variable for storing the value of the selection program P3 (movement left to right and stop)
int valP4;                        // variable for storing the value of the selection program P4 (movement left to right, right to left and stop)
int valP5;                        // variable for storing the value of the selection program P5 (continuous movement)
int valP6;                        // variable for storing the value of the selection program P6 (start position)
int swParc;                       // variable for storing the value of the parking switch
int swCapSt;                      // variable for storing the value of the left switch
int swCapDr;                      // variable for storing the value of the right switch
int potmotValue;                  // variable for storing the value of the potentiometer connected to the cart

int valp1Po1;                      // variable for storing the value 1 of the ON push-button program P1
int valp1Po2;                      // variable for storing the value 2 of the ON push-button program P1
int valp1Op1;                      // variable for storing the value 1 of the OFF push-button program P1
int valp1Op2;                      // variable for storing the value 2 of the OFF push-button program P1
int p1Mode = 0;                    // state of the push-button for program P1

int valp2Po1;                      // variable for storing the value 1 of the ON push-button program P2
int valp2Po2;                      // variable for storing the value 2 of the ON push-button program P2
int valp2Op1;                      // variable for storing the value 1 of the OFF push-button program P2
int valp2Op2;                      // variable for storing the value 2 of the OFF push-button program P2
int p2Mode = 0;                    // state of the push-button for program P2

int valp3Po1;                      //      
int valp3Po2;                      // 
int valp3Op1;                      // same as previous
int valp3Op2;                      // 
int p3Mode = 0;                    // 

int valp4Po1;                      // 
int valp4Po2;                      // 
int valp4Op1;                      // same as previous
int valp4Op2;                      // 
int p4Mode = 0;                    // 

int valp5Po1;                      // 
int valp5Po2;                      // 
int valp5Op1;                      // same as previous
int valp5Op2;                      // 
int p5Mode = 0;                    //  

int valp6Po1;                      // 
int valp6Po2;                      // 
int valp6Op1;                      // same as previous
int valp6Op2;                      // 
int p6Mode = 0;                    // 

int x;                            // global variable for controlling the function left to right and stop and function right to left and stop

void setup() {
  
  pinMode(switchParcare, INPUT);          // setting as input for parking switch
  pinMode(switchCapStanga, INPUT);        // setting as input for left switch
  pinMode(switchCapDreapta, INPUT);       // setting as input for right switch
  pinMode(progrP1, INPUT);                // setting as input for program P1
  pinMode(progrP2, INPUT);                // setting as input for program P2
  pinMode(progrP3, INPUT);                // setting as input for program P3
  pinMode(progrP4, INPUT);                // setting as input for program P4
  pinMode(progrP5, INPUT);                // setting as input for program P5
  pinMode(progrP6, INPUT);                // setting as input for program P6
  pinMode(switchPornit, INPUT);           // setting as input for push-button ON
  pinMode(switchOprit, INPUT);            // setting as input for push-button OFF
  
  
  pinMode(comandaFrana, OUTPUT);          // setting as output for command of the electromagnetic brake
  pinMode(comandaPOinv, OUTPUT);          // setting as output for command ON/OFF motor
  pinMode(comandaSens, OUTPUT);           // setting as output for command direction of the motor
  pinMode(comandaSelV1, OUTPUT);          // setting as output for command speed selection V1 of the motor
  pinMode(comandaSelV2, OUTPUT);          // setting as output for command speed selection V2 of the motor
 
  
 
  
  
  Serial.begin(9600);                // Set up serial communication at 9600bps
  
}

Part with problemm :

void miscarestdrstop() {                                            // left to right and stop program
  int starestdrstop = 0;                                            // state  of the program
  
  swCapSt = digitalRead(switchCapStanga);
  swCapDr = digitalRead(switchCapDreapta);
  potmotValue = analogRead(potMotor);
  
    if ((potmotValue >= 90) && (potmotValue < 200) || (swCapSt == LOW)) {    // if value of the potentiometer is between the values execute the following code
      potmotValue = analogRead(potMotor);
      delay(50);
      swCapSt = digitalRead(switchCapStanga);
      swCapDr = digitalRead(switchCapDreapta);
      digitalWrite(comandaFrana, HIGH);
      digitalWrite(comandaSens, LOW);
      digitalWrite(comandaSelV2, HIGH);
      digitalWrite(comandaSelV1, LOW);
      digitalWrite(comandaPOinv, HIGH);
    } 
     
      else if ((potmotValue >= 200) && (potmotValue < 800) && (swCapSt == HIGH))  {    // if value of the potentiometer is between the values execute the following code
      potmotValue = analogRead(potMotor);
      delay(50);
      swCapSt = digitalRead(switchCapStanga);
      swCapDr = digitalRead(switchCapDreapta);
      digitalWrite(comandaFrana, HIGH);
      digitalWrite(comandaSens, LOW);
      digitalWrite(comandaSelV1, LOW);
      digitalWrite(comandaSelV2, LOW);
      delay(10);
      digitalWrite(comandaPOinv, HIGH);
      }
 
      else if ((potmotValue >= 800) && (potmotValue < 925) && (swCapSt == HIGH)) {    // if value of the potentiometer is between the values execute the following code
      potmotValue = analogRead(potMotor);
      delay(50);
      swCapSt = digitalRead(switchCapStanga);
      swCapDr = digitalRead(switchCapDreapta);
      digitalWrite(comandaPOinv, HIGH);
      digitalWrite(comandaFrana, HIGH);
      digitalWrite(comandaSens, LOW);
      digitalWrite(comandaSelV1, HIGH);
      digitalWrite(comandaSelV2, LOW);
    }
    
      else if ((potmotValue >= 925) && (potmotValue < 950) || (swCapDr == LOW)) {      // if value of the potentiometer is between the values execute the following code
      potmotValue = analogRead(potMotor);
      delay(50);
      swCapSt = digitalRead(switchCapStanga);
      swCapDr = digitalRead(switchCapDreapta);
      digitalWrite(comandaPOinv, LOW); 
      digitalWrite(comandaSens, LOW);
      digitalWrite(comandaSelV1, LOW);
      digitalWrite(comandaSelV2, LOW);
      delay(50);
      digitalWrite(comandaFrana, LOW);
      starestdrstop = 2;      // state of the program becomes 2
    }
    
    
    else  {
    digitalWrite(comandaPOinv, LOW);
    digitalWrite(comandaSens, LOW);
    digitalWrite(comandaSelV1, LOW);
    digitalWrite(comandaSelV2, LOW);
    digitalWrite(comandaFrana, LOW);
  }
  
  x = starestdrstop;            // transfer the state of the program to global variable
}

void miscaredrststop() {                                                    // right to left and stop program
  int staredrststop = 2;                                                    // state  of the program
  
  swCapSt = digitalRead(switchCapStanga);
  swCapDr = digitalRead(switchCapDreapta);
  potmotValue = analogRead(potMotor);
  
    if (((potmotValue >= 925) && (potmotValue < 950)) || (swCapDr == LOW)) {
      potmotValue = analogRead(potMotor);
      delay(10);
      swCapSt = digitalRead(switchCapStanga);
      swCapDr = digitalRead(switchCapDreapta);
      digitalWrite(comandaFrana, HIGH);
      digitalWrite(comandaSens, HIGH);
      digitalWrite(comandaSelV2, HIGH);
      digitalWrite(comandaSelV1, LOW);
      delay(20);
      digitalWrite(comandaPOinv, HIGH);
    } 
     
      else if ((potmotValue >= 150) && (potmotValue < 925) && (swCapDr == HIGH) && (swCapSt == HIGH))  {
      potmotValue = analogRead(potMotor);
      delay(10);
      swCapSt = digitalRead(switchCapStanga);
      swCapDr = digitalRead(switchCapDreapta);
      digitalWrite(comandaFrana, HIGH);
      digitalWrite(comandaSens, LOW);
      digitalWrite(comandaSelV1, LOW);
      digitalWrite(comandaSelV2, LOW);
      digitalWrite(comandaPOinv, HIGH);
      }
 
      else if ((potmotValue > 100) && (potmotValue < 150) && (swCapSt == HIGH))  {
      potmotValue = analogRead(potMotor);
      delay(10);
      swCapSt = digitalRead(switchCapStanga);
      digitalWrite(comandaFrana, HIGH);
      digitalWrite(comandaSens, LOW);
      digitalWrite(comandaSelV1, HIGH);
      digitalWrite(comandaSelV2, LOW);
      digitalWrite(comandaPOinv, HIGH);
      }
 
   
      else if (((potmotValue > 75) && (potmotValue <= 100)) || (swCapSt == LOW)) {
      potmotValue = analogRead(potMotor);
      delay(10);
      swCapSt = digitalRead(switchCapStanga);
      digitalWrite(comandaPOinv, LOW);
      digitalWrite(comandaSens, LOW);
      digitalWrite(comandaSelV1, LOW);
      digitalWrite(comandaSelV2, LOW);
      delay(20);
      digitalWrite(comandaFrana, LOW);
     staredrststop = 1;                // state of the program becomes 1
    }
    
    
    else  {
    digitalWrite(comandaPOinv, LOW);
    digitalWrite(comandaSens, LOW);
    digitalWrite(comandaSelV1, LOW);
    digitalWrite(comandaSelV2, LOW);
    digitalWrite(comandaFrana, LOW);
  }
  x = staredrststop;        // transfer the state of the program to global variable
}

void miscarestdrststop() {                        // left to right, right to left and stop program only one cycle 
int a = 0;                                        // state of the program
 
 if (a != 1) {                                    // if state not equal with 1
   if( x != 2) {                                  // if global state of the left to right function not equal with 2
  
  miscarestdrstop();}                              // call movement left to right and stop function
  a = 1;                                            // go out after execution of the program
 }
 
   else if (a != 2) {                              // if state not equal with 2
     if ( x != 1) {                                // if global state of the right to left function not equal with 1
     miscaredrststop(); }                          // call movement right to left and stop function
  a = 2;                                            // go out after execution of the program
 }
 
  
 
  
 /* 
  else  {
    digitalWrite(comandaPOinv, LOW);
    digitalWrite(comandaSens, LOW);
    digitalWrite(comandaSelV1, LOW);
    digitalWrite(comandaSelV2, LOW);
    digitalWrite(comandaFrana, LOW);
  } */
 
Serial.println(a, DEC);          // debug
Serial.println(x, DEC);           // debug
}


void miscarestdrcontinua() {                          // left to right, right to left  continuous program ; 
  miscarestdrstop();
  delay(500);
  miscaredrststop();
  delay(500);
}

Your if tests are very hard to follow. If you'd be much better if the code looked like this:

swCapSt = digitalRead(switchCapStanga);
if(swCapSt == HIGH)
{
// Do something, depending on pot values
}
else
{
// Do whatever
}

I don't see that potmotValue is ever used as an output.

I'm also confused by your re-reading the potMotor pin after making a decision about what to do.

Finally, I don't see that the potMotor pin was ever declared as an INPUT pin.

Thanks for your information. I tried to put the entire program but I was limited by the number of characters.
I will try to rebulid the program .
Thanks