Motors rotate after I give some external force

I’m trying to program a Walking robot using a Arduino UNO R3, L298N driver module, and a HC-05 bluetooth
module.

This is my code:

const byte enA = 9;//defining the pins for usage
const byte in1 = 4;
const byte in2 = 5;
const byte enB = 10;
const byte in3 = 6;
const byte in4 = 7;

int val = 0;//The Values recieved by the Bluetooth is assigned to this variable
int starttime = 0;//For running a command for desired secs
int endtime = 0;//        ""
int loopcount = 0;//      ""

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  //Read input from bluetooth module:
  if ( Serial.available())
  {
    val = Serial.read();
  }


  //Input key switch
  switch (val) {
    case '0':
      analogWrite(enA, 0);//controls speed
      analogWrite(enB, 0);
      //Code when no key is pressed
      break;
    case '1':
      Serial.println("<Command recieved>");
      // Set Motor A forward
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      // Set Motor B forward
      digitalWrite(in3, LOW);
      digitalWrite(in4, HIGH);//Code when UP key is pressed

      starttime = millis();//This while allows us to run loop for required secs
      endtime = starttime;
      while ((endtime - starttime) <= 3000) // do this loop for up to 2secs(1000mS=1 secs)
      {
        // code here
          analogWrite(enA, 250);
          analogWrite(enB, 250);
          Serial.println("<Forward>");
        loopcount = loopcount + 1;
        endtime = millis();
      }
      analogWrite(enA, 0);//code to execute when loop is complete
      analogWrite(enB, 0);


      Serial.println("<Setting Motor Forward>");
      break;
    case '2':
      // Set Motor A backward
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);
      // Set Motor B backward
      digitalWrite(in3, HIGH);
      digitalWrite(in4, LOW);
      starttime = millis();
      endtime = starttime;
      while ((endtime - starttime) <= 3000) // do this loop for up to 1000mS
      {
        // code here
        analogWrite(enA, 250);
        analogWrite(enB, 250);
        Serial.println("<Backward>");


        loopcount = loopcount + 1;
        endtime = millis();
      }
      analogWrite(enA, 0);
      analogWrite(enB, 0);

      //Code when DOWN key is pressed
      Serial.println("<Backward>");
      break;
    case '3':
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      digitalWrite(in3, HIGH);
      digitalWrite(in4, LOW);
       starttime = millis();
      endtime = starttime;
      while ((endtime - starttime) <= 3000) // do this loop for up to 1000mS
      {
        // code here
          analogWrite(enA, 200);
          analogWrite(enB, 200);
          Serial.println("<Left>");
        loopcount = loopcount + 1;
        endtime = millis();
      }
      analogWrite(enA, 0);
      analogWrite(enB, 0);
      Serial.println("<Left>");
      //Code when LEFT key is pressed
      break;
    case '4':
      //Code when RIGHT key is pressed
      digitalWrite(in1, HIGH);
      digitalWrite(in2, LOW);
      digitalWrite(in3, LOW);
      digitalWrite(in4, HIGH);
       starttime = millis();
      endtime = starttime;
      while ((endtime - starttime) <= 3000) // do this loop for up to 1000mS
      {
        // code here
          analogWrite(enA, 200);
          analogWrite(enB, 200);
          Serial.println("<Right>");
        loopcount = loopcount + 1;
        endtime = millis();
      }
      analogWrite(enA, 0);
      analogWrite(enB, 0);
      Serial.println("<right>");
      break;
    case '5':
      //Code when X key is pressed
       starttime = millis();
      endtime = starttime;
      while ((endtime - starttime) <= 1000) // do this loop for up to 1000mS
      {
        // code here
          analogWrite(enA, 0);
          analogWrite(enB, 200);
          Serial.println("<Drift left>");
        loopcount = loopcount + 1;
        endtime = millis();
      }
      analogWrite(enA, 0);
      analogWrite(enB, 0);
      break;
    case '6':
    starttime = millis();
      endtime = starttime;
      while ((endtime - starttime) <= 1000) // do this loop for up to 1000mS
      {
        // code here
          analogWrite(enA, 200);
          analogWrite(enB, 0);
          Serial.println("<Drift right>");
        loopcount = loopcount + 1;
        endtime = millis();
      }
      analogWrite(enA, 0);
      analogWrite(enB, 0);
      //Code when O key is pressed
      
      break;
    default:
    starttime = millis();
      endtime = starttime;
      while ((endtime - starttime) <= 1000) // do this loop for up to 1000mS
      {
        // code here
          analogWrite(enA, 0);
          analogWrite(enB, 0);
        loopcount = loopcount + 1;
        endtime = millis();
      }
      analogWrite(enA, 0);
      analogWrite(enB, 0);
      
      // default code (should never run)
  }
}

My problem is that my motor spin only when I give some external force to it.

Can this be because of supplying a low voltage.

My circuit is attached below.

Also suggest me a way to give separate power supply to the motors.

The millis command is to run my code for desired seconds…

Yes, it's probably low voltage.

Are the motors rated at 12V? What's the current rating on the motor?

Do you have a multimeter to measure battery voltage? The battery probably can't supply the necessary current so the voltage is probably dropping when you try to start the motors.

What happens when you connect the motors directly to the battery?

...And the maximum PWM is 255 so you could give it a little more "juice".