Weird behaviour with potentiometer

Hello,

I have Arduino Uno Rev3, and two clones. I also have a motomonster with potentiometer soldered to it. I am trying to make a 2DOF racing sim, but Arduinos act weird. For example, I have a motor hooked up, and I wrote simple program to control it. The code says to motor: if pot value is higher than x stop. It does not stop it just slows down (I even atached a inbuilt led to test where is the problem, but it only dims not turning off). When pot value is lower than about 500 it just does not listen to any commands, and stops the motor, even though it is not supposed to stop.

Please can someone help me? Thank you in advance

Hard to see how this is an installation issue.

You might get more help if you post your code, more details, and a schematic

This is my code…

#define BRAKE 0
#define CW    1
#define CCW   2
#define CS_THRESHOLD 15   // Definition of safety current (Check: "1.3 Monster Shield Example").

//MOTOR 1
#define MOTOR_A1_PIN 7
#define MOTOR_B1_PIN 8

//MOTOR 2
#define MOTOR_A2_PIN 4
#define MOTOR_B2_PIN 9

#define PWM_MOTOR_1 5
#define PWM_MOTOR_2 6

#define CURRENT_SEN_1 A2
#define CURRENT_SEN_2 A3

#define EN_PIN_1 A0
#define EN_PIN_2 A1

#define MOTOR_1 0
#define MOTOR_2 1



void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  

}

// the loop routine runs over and over again forever:
void loop() {


  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability

  if (sensorValue > 721 && sensorValue < 779){
    Serial.println("stred");
    digitalWrite(LED_BUILTIN, LOW);
    motorGo(MOTOR_1, BRAKE, 0);
  }
  if (sensorValue < 720){
    Serial.println("doleva");
    digitalWrite(LED_BUILTIN, HIGH);
    motorGo(MOTOR_1, CW, 150);
  }
  

  if (sensorValue > 780){
    Serial.println("doprava");
    digitalWrite(LED_BUILTIN, HIGH);
    motorGo(MOTOR_1, CCW, 150);
  }

  if (sensorValue > 950){
    Serial.println("stop");
    digitalWrite(LED_BUILTIN, LOW);
    motorGo(MOTOR_1, BRAKE, 0);
  }

  if (sensorValue < 150){
    Serial.println("stop");
    digitalWrite(LED_BUILTIN, LOW);
    motorGo(MOTOR_1, BRAKE, 0);
  }

  
  
  
}

void motorGo(uint8_t motor, uint8_t direct, uint8_t pwm)         //Function that controls the variables: motor(0 ou 1), direction (cw ou ccw) e pwm (entra 0 e 255);
{
  if(motor == MOTOR_1)
  {
    if(direct == CW)
    {
      digitalWrite(MOTOR_A1_PIN, LOW); 
      digitalWrite(MOTOR_B1_PIN, HIGH);
    }
    else if(direct == CCW)
    {
      digitalWrite(MOTOR_A1_PIN, HIGH);
      digitalWrite(MOTOR_B1_PIN, LOW);      
    }
    else
    {
      digitalWrite(MOTOR_A1_PIN, LOW);
      digitalWrite(MOTOR_B1_PIN, LOW);            
    }
    
    analogWrite(PWM_MOTOR_1, pwm); 
  }
  else if(motor == MOTOR_2)
  {
    if(direct == CW)
    {
      digitalWrite(MOTOR_A2_PIN, LOW);
      digitalWrite(MOTOR_B2_PIN, HIGH);
    }
    else if(direct == CCW)
    {
      digitalWrite(MOTOR_A2_PIN, HIGH);
      digitalWrite(MOTOR_B2_PIN, LOW);      
    }
    else
    {
      digitalWrite(MOTOR_A2_PIN, LOW);
      digitalWrite(MOTOR_B2_PIN, LOW);            
    }
    
    analogWrite(PWM_MOTOR_2, pwm);
  }
}

it is really simple and for examle it is supposed to stop if pot value is less than 150 (motor stops at 400-300 , led dims), if it is in between 721 and 779 (works as supposed to, and if it is above 950 (motor slows down but never stops)

The pot is connected to A0

Also i used codes from different finished projects with no success. Also acts on its own

I see that i have to give ranges to each command as two commands might be sent at the same time, but that doesnt solve the problems with already completed codes from other projects. It just acts as it is not supposed to and i dont know why

Here is the updated code where is works with pots maximum value but minimum is still not working. As pot value falls to 320 only LED listens to commands. And then when i raise the value motor wont listen until it reaches value 500. But Serial print and LED work correctly.

#define BRAKE 0
#define CW    1
#define CCW   2
#define CS_THRESHOLD 15   // Definition of safety current (Check: "1.3 Monster Shield Example").

//MOTOR 1
#define MOTOR_A1_PIN 7
#define MOTOR_B1_PIN 8

//MOTOR 2
#define MOTOR_A2_PIN 4
#define MOTOR_B2_PIN 9

#define PWM_MOTOR_1 5
#define PWM_MOTOR_2 6

#define CURRENT_SEN_1 A2
#define CURRENT_SEN_2 A3

#define EN_PIN_1 A0
#define EN_PIN_2 A1

#define MOTOR_1 0
#define MOTOR_2 1



void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  

}

// the loop routine runs over and over again forever:
void loop() {


  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability

  if (sensorValue > 720 && sensorValue < 750){
    Serial.println("stred");
    digitalWrite(LED_BUILTIN, LOW);
    motorGo(MOTOR_1, BRAKE, 0);
  }
  if (sensorValue > 201 && sensorValue < 720){
    Serial.println("doleva");
    digitalWrite(LED_BUILTIN, HIGH);
    motorGo(MOTOR_1, CW, 150);
  }
  

  if (sensorValue > 750 && sensorValue < 900 ){
    Serial.println("doprava");
    digitalWrite(LED_BUILTIN, HIGH);
    motorGo(MOTOR_1, CCW, 150);
  }

  if (sensorValue > 901){
    Serial.println("stop");
    digitalWrite(LED_BUILTIN, LOW);
    digitalWrite(MOTOR_A1_PIN, LOW);
    motorGo(MOTOR_1, BRAKE, 0);
  }

  if (sensorValue < 200){
    Serial.println("stop");
    digitalWrite(LED_BUILTIN, LOW);
    digitalWrite(MOTOR_A1_PIN, LOW);
    motorGo(MOTOR_1, BRAKE, 0);
  }

  
  
  
}

void motorGo(uint8_t motor, uint8_t direct, uint8_t pwm)         //Function that controls the variables: motor(0 ou 1), direction (cw ou ccw) e pwm (entra 0 e 255);
{
  if(motor == MOTOR_1)
  {
    if(direct == CW)
    {
      digitalWrite(MOTOR_A1_PIN, LOW); 
      digitalWrite(MOTOR_B1_PIN, HIGH);
    }
    else if(direct == CCW)
    {
      digitalWrite(MOTOR_A1_PIN, HIGH);
      digitalWrite(MOTOR_B1_PIN, LOW);      
    }
    else
    {
      digitalWrite(MOTOR_A1_PIN, LOW);
      digitalWrite(MOTOR_B1_PIN, LOW);            
    }
    
    analogWrite(PWM_MOTOR_1, pwm); 
  }
  else if(motor == MOTOR_2)
  {
    if(direct == CW)
    {
      digitalWrite(MOTOR_A2_PIN, LOW);
      digitalWrite(MOTOR_B2_PIN, HIGH);
    }
    else if(direct == CCW)
    {
      digitalWrite(MOTOR_A2_PIN, HIGH);
      digitalWrite(MOTOR_B2_PIN, LOW);      
    }
    else
    {
      digitalWrite(MOTOR_A2_PIN, LOW);
      digitalWrite(MOTOR_B2_PIN, LOW);            
    }
    
    analogWrite(PWM_MOTOR_2, pwm);
  }
}