Object Avoidance Robot (Help Needed)

// I will need help introducing the DC motors since I have no idea how they work

int trigPin = 13
const int echoPin = 12;
int led = 11;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);


void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 10) {

else {


// I will need help introducing the DC motors since I have no idea how they work

You don’t say what kind they are. You don’t say what will be between them and the Arduino.

int trigPin = 13
const int echoPin = 12;

The echo pin will always be 12, but, while the program is running, you might change the trigger pin? That seems pretty unlikely.

What size variable is needed to hold 12? 13?

you don’t need to declare the echopin as as an constant variable (int)

int trigPin = 13;
int echoPin = 12;

I will assume you’re using an H-bridge and running two dc motors.

int motorLeftA = 2;
int motorLeftB = 3;
int motorRightA = 4;
int motorRightB = 5;

void main()

pinMode(motorLeftA, OUTPUT);
pinMode(motorLeftB, OUTPUT);
pinMode(motorRightA, OUTPUT);


void loop()

digitalWrite(motorLeftA, HIGH);
digitalWrite(motorLeftB, LOW);
digitalWrite(motorRightA, HIGH);
digitalWrite(motorRightB, LOW);

delay (2000);

digitalWrite(motorLeftA, LOW);
digitalWrite(motorLeftB, HIGH);
digitalWrite(motorRightA, LOW);
digitalWrite(motorRightB, HIGH);

delay (4000);


Try and attach your motors to the declared pins and see if it will drive forward and backwards.

if it works, then we can make functions afterwards which you just call for your robot to drive forward, backward, turn left and turn right.