Odd problem with motor not turning in one direction

Hi all. I have been having a strange issue with a motor not reversing direction properly.

I am driving the motors using an Arduino UNO with a 9V battery power supply, using an L298N dual bridge board. I am also using a VL6180x distance sensor to control the turning action of the 2 9V DC motors (https://www.robotshop.com/uk/makeblock-9v-dc-motor-185-rpm.html).

Here is the code using the VL6180x libraries:

#include <Wire.h>
#include <Adafruit_VL6180X.h>

Adafruit_VL6180X vl = Adafruit_VL6180X();

void setup() {

  Serial.begin(115200);
  Serial.println("Adafruit VL6180x test!");
  if (! vl.begin()) {
    Serial.println("Failed to find sensor");
    while (1);
  }
  Serial.println("Sensor found!");

  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

  randomSeed(analogRead(A0));
}

void loop() {

  analogWrite(0, 255);
  digitalWrite(1, HIGH);
  digitalWrite(2, LOW);
  analogWrite(5, 255);
  digitalWrite(4, LOW);
  digitalWrite(3, HIGH);

  uint8_t range = vl.readRange();
  uint8_t status = vl.readRangeStatus();

  if (status == VL6180X_ERROR_NONE) {
    Serial.print("Range: "); Serial.println(range);
  }

  int rando = random(1, 11);
  //Serial.println(rando);

  if (range < 80)
  {
    if (rando <= 5)
    {
      do {
        Serial.println("Turning Left...");
        //analogWrite(5, 255);
        //analogWrite(0, 255);
        digitalWrite(1, HIGH);
        digitalWrite(2, LOW);
        digitalWrite(4, HIGH);
        digitalWrite(3, LOW);
        range = vl.readRange();
      }
      while (range < 120);
    }
    else
    {
      do {
        Serial.println("Turning Right...");
        //analogWrite(5, 255);
        //analogWrite(0, 255);
        digitalWrite(1, LOW);                         //ERROR
        digitalWrite(2, HIGH);
        digitalWrite(4, LOW);
        digitalWrite(3, HIGH);
        range = vl.readRange();
      }
      while (range < 120);
    }
  }
}

The code will generate a random number and if the sensor detects a distance less than 80, the motors will turn left or right, depending on the random number value, until the distance is greater than 120. The problem occurs where i have marked error.

When the program attempts to turn left, it works perfectly but on trying to turn right, the motor getting the PWM input from Pin 0 with the LOW Pin as 1 and the HIGH Pin as 2, does not reverse direction. The motor is still attempting to turn as i can hear and feel it vibrating. Very puzzled as to what could be causing this as the code for turning in both directions is identical, just the motors directions are reversed. Any help is greatly appreciated.

Pins 0 and 1 are the hardware serial pins on an Uno.

And even if you weren't using pin 0 for Serial, it is not capable of PWM output on most Arduinos, including the Uno.

9V battery? I forsee disappointment if you are trying to power a motor from a small 9V battery, they cannot source enough current.

MorganS: And even if you weren't using pin 0 for Serial, it is not capable of PWM output on most Arduinos, including the Uno.

Sorry i still do not understand. If the serial pins are incapable of PWM output, why can the motor turn one way but not the other? Is it not possible at all to use these pins for this purpose?

Cyanners:
Is it not possible at all to use these pins for this purpose?

It certainly isn’t possible to use them to drive a motor AND for Serial output at the same time. You have to pick one.

Best bet, stay off of pins 0 and 1.

Delta_G: It certainly isn't possible to use them to drive a motor AND for Serial output at the same time. You have to pick one.

Best bet, stay off of pins 0 and 1.

Ok i see. Thanks for the help.

Here is a useful L298 driver/motor tutorial, showing typical pin connections: https://howtomechatronics.com/tutorials/arduino/arduino-dc-motor-control-tutorial-l298n-pwm-h-bridge/