Plzz help me edditing this code

I want to edit this code so that the robot stops when obstacles comes. In front of it…plzz help me

Obstacle_Avoiding_Robot_Code.ino (1.98 KB)

It is much easier for everyone if you post the code for short programs. Like this

/* Code Written by Roy Pe'er. I've explained all the code in the grey comments.
I recommend you to go over the code, examine it, play with it, improve it and modify it according to your needs. 
For more awesome videos, subsrice to my channel:
http://www.youtube.com/subscription_center?add_user=Roypeer1
*/





#include <AFMotor.h> //import your motor shield library
#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  
  Serial.println("Motor test!");
   pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
  pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
  motor1.setSpeed(105); //set the speed of the motors, between 0-255
motor2.setSpeed (105);  
}
 
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 (" Turning !");
    motor1.run(FORWARD);  // Turn 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);  
  }  
  
  

  
  
  
 
}

…R

You need to tell us what the code actually does and what you want it to do differently.

...R

I think u should use:

motor1.stop();
motor2.stop();

Hope this’ll work :slight_smile:

so note this part in the code

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

when you have two wheels and one is turning FORWARD and the other BACKWARD, your robot is turning on itself.

I suppose that instead of turning you want to stop, right?

motors are declared as instance of the class AF_DCMotor, and if you look at the documentation for that class you’ll see an interesting note

run(cmd)
Sets the run-mode of the motor.
Parameters:
cmd - the desired run mode for the motor
Valid values for cmd are:
FORWARD - run forward (actual direction of rotation will depend on motor wiring)
BACKWARD - run backwards (rotation will be in the opposite direction from FORWARD)
RELEASE - Stop the motor. This removes power from the motor and is equivalent to setSpeed(0). The motor shield does not implement dynamic breaking, so the motor may take some time to spin down

so instead of going FORWARD and BACKWARD on the motors, have them do what you want at that stage.