How to make dg012-atv turn left-right code

Hi, I have code that makes the car detect the distance of 10cm and when it gets 10cm to object it moves backward and forward 10cm to object using a ultrasonic sensor. My question is how do i make it turn left or right

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include “utility/Adafruit_MS_PWMServoDriver.h”

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
int sound = 250;
// Select which ‘port’ M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *motor1 = AFMS.getMotor(1);
Adafruit_DCMotor *motor2 = AFMS.getMotor(2);
Adafruit_DCMotor *motor3 = AFMS.getMotor(3);
Adafruit_DCMotor *motor4 = AFMS.getMotor(4);
const int trigPin = 9;
const int echoPin = 10;

void setup() {
Serial.begin (9600);
AFMS.begin();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(115200);
motor1->setSpeed(100);
motor2->setSpeed(100);
motor3->setSpeed(100);
motor4->setSpeed(100);

motor1->run(RELEASE);
motor2->run(RELEASE);
motor3->run(RELEASE);
motor4->run(RELEASE);

}

void loop() {
uint8_t i;
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;

if (distance <= 10) {

motor1->run(FORWARD);
motor2->run(FORWARD);
motor3->run(FORWARD);
motor4->run(FORWARD);
delayMicroseconds(10);

}
else {
motor1->run(BACKWARD);
motor2->run(BACKWARD);
motor3->run(BACKWARD);
motor4->run(BACKWARD);
delayMicroseconds(10);

}

}

  1. What kind of a car? It looks like it may have 4 motors but I have no idea how those motors are arranged.

  2. What would make it decide to turn? Once you know which way to turn it should not be difficult to drive the motors appropriately.

  3. Please use code tags next time. Read the how-to-use-this-forum post at the top of every forum.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Did you write this code.
Before you post your code, while in the IDE, press CTRL-T, this will format your code and indent your if and for statements, it make your code easier to read.

Thanks.. Tom.. :slight_smile:

My question is how do i make it turn left or right

What does the car do if you run the motors on one side forward while running the motors on the other side backward?