Help controlling 12vDC Motor's triggered by ultrasonic sensor (code)

Hi there guys, this is my first project with arduino, so I apologize in advance for any beginner mistakes or seemingly obvious doubts.

I'm doing a project that consists in triggering 2 12vDC motor's trought an ultrasonic sensor when distance < 40cm. I want the motors to be still, and only start rotating when an object enters de 0-40cm zone captated by the sensor.

The components i'm using are:

Arduino Uno
Shield Motor (L293Dx2)
Ultrasonic Sensor (HC-SR04)
2 12vDCmotors

Trought tutorials of 'how to do an obstancle avoiting car' I've already done the hardware and some software parts and it's working great. I'm running this code that i've founded online and modifidet a bit, to test it:

#include <AFMotor.h> 
#define trigPin 12 // define the pins of your sensor
#define echoPin 13 
AF_DCMotor motor1(1,MOTOR12_64KHZ); // set up motors.
AF_DCMotor motor2(2, MOTOR12_8KHZ);
 
void setup() {
  Serial.begin(9600); // begin serial communitication  
   pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
  pinMode(echoPin, INPUT);
  motor1.setSpeed(200);
motor2.setSpeed(200);
}


void loop() {
   long duration, distance; // start the scan
  digitalWrite(trigPin, LOW);  
  delayMicroseconds(2); // delays are required for a succesful sensor operation.
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); //this delay is required as well!
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;// convert the distance to centimeters.
  if (distance < 25)/*if there's an obstacle 25 centimers, ahead, do the following: */ {   
   Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.

Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Starting");
    motor1.run(BACKWARD);  // keep as long as there's an obstacle ahead.
    motor2.run (BACKWARD);

}
  else {
   Serial.println ("No obstacle detected. going forward");
   delay (15);
   motor1.run(FORWARD); //if there's no obstacle ahead, Go Forward! 
    motor2.run(FORWARD);  
  }  
  
  

  
  
  
 
}

Now, to try and do the thing that i want, i've humble modifided it to :

#include <AFMotor.h> 
#define trigPin 12 // define the pins of your sensor
#define echoPin 13 
AF_DCMotor motor1(1,MOTOR12_64KHZ); // set up motors.
AF_DCMotor motor2(2, MOTOR12_8KHZ);
 
void setup() {
  Serial.begin(9600); // begin serial communitication  
   pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
  pinMode(echoPin, INPUT);
  motor1.setSpeed(200);
motor2.setSpeed(200);
}


void loop() {
   long duration, distance; // start the scan
  digitalWrite(trigPin, LOW);  
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;// convert the distance to centimeters.
  if (distance > 40)/*if there's an obstacle 40 centimers, ahead, do the following: */ {   
   Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.

Serial.println (" The obstacle is declared. ");
Serial.println (" Starting");
    motor1.run(0);  // keep as long as there's an obstacle ahead.
    motor2.run (0);

}
  else {
   Serial.println ("No obstacle detected. going forward");
   delay (15);
   motor1.run(FORWARD); //if there's no obstacle ahead, Go Forward! 
    motor2.run(FORWARD);
  }  
  
}

I've tried to make it as if the speed of the motor is alaways 0, and if there's an obstacle tectected at distance<40 (cm) it starts running, but the only thing that i keep having is the motors always running.

Can somebody help this beginner, but motivated enthusiast?

Thank you,
RT

If there's no obstacle you want not to be moving but if an obstacle gets close you want to drive into it? Are you sure?

Sorry but as your code is doing the precise opposite of what the comments say it's doing I lost interest.

Steve

Thank you for your answer Steve.

I do know that, but due to my lack of knowlege that was the code that i've found trought youtube tutorials.
Can you give me some insights to turn into the right direction?

For instance, how do i write it so:

  1. the motors are still
  2. the sensor reads distance
  3. when distance <40 motors start running
  4. when distance >40 motors stop running

I think i kinda understand the logic of it, i just dont know how to translate it into code.

Time to do some debugging. You have all those Serial.prints in your existing code so time to check what they tell you, first when there is an obstacle close and second when there is no obstacle. They should at least show you where in the code it gets to.

It may be confusing to read because your code says when there's no obstacle (distance > 40) print "Close object detected!" but you should still be able to work out if the sensor is reading correctly and which bits of code it's getting to.

You might want to add an extra print immediately after distance is calculated to show the value before any ifs e.g.

  distance = (duration/2) / 29.1;// convert the distance to centimeters.
  Serial.print("Distance : ");
  Serial.println(distance);

Steve