4WD Obstacle Avoidance bot

So I built a robot car to drive around and avoid objects.
I wrote a program to test the sensor and motors and it all is functional.
My completed code, however, is giving me a headache. only 3 wheels will turn; 1,3, and 4.
I found out that if I do not try the ping, It works just fine (Besides avoiding obstacles).

#include <AFMotor.h>
#define trigPin 3
#define echoPin 2
AF_DCMotor motor(1, MOTOR12_8KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
AF_DCMotor motor3(3, MOTOR12_8KHZ);
AF_DCMotor motor4(4, MOTOR12_8KHZ);

void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
motor.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
Serial.begin (9600);
}

int CheckDistance()
{
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

return distance;
}

void vroom(int delaytime)
{
motor.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(delaytime);
motor.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
delay(delaytime);

}
void MotorBackward(int delaytime)
{
motor.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
delay(delaytime);
motor.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
delay(delaytime);
}

void MotorLeft()
{
motor.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(600);
}

void MotorRight()
{
motor.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
delay(600);
}
void MotorRelease()
{
motor.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
delay(500);
}
void loop()
{
long testDistance = CheckDistance();
if (testDistance >= 50|| testDistance == 0)
{
vroom(1000);
}
else
{
Serial.print(testDistance);
Serial.println(" cm");
MotorBackward(600);
MotorRelease();
MotorRight();
MotorRelease();
vroom(600);
MotorRelease();
MotorLeft();
MotorRelease();

}
delay(500);
}

If the void loop() only contains the vroom function; 4 wheels will turn.
the way it is; motor 2 doesn’t turn

anybody?

Disconnect everything but motor 2. Does it run by itself? If not, try it in another position. Does it work in another position?

Yes Motor 2 will run by itself and it will also run If I do not do a ping with the sensor. It is only when I include the sensor ping that motor 2 stops working.

What pins does the motor shield use?

CODE TAGS
Do you have the V1 motor shield? If you do, you will need to use the analog pins for the sensor. The shield uses pin 3.

im using the Arduino Motor Shield L293D. Sensor is connected to pin 2,3,5v, and ground.

You are not answering the questions being asked. Good luck with solving your problem yourself.

If you are using the Adafruit motor shield, you MUST use the analog pins. The shield uses all pins except the analog pins and pins 2 and 13.

Sorry im new and still trying to learn. How would using analog pins change the code?

How would using analog pins change the code?

It would get the sensor off the pin that is causing motor 2 to not work.

Change the 2nd and 3rd lines to:

#define trigPin A0
#define echoPin A1

Then move the sensor to A0 and A1.