0

I built a small robotic car with an ultrasonic sensor on it. (I followed this tutorial : http://educ8s.tv/arduino-robot-easy-diy-project/ ) and the full source code is underneath it (you will have to download)

I modified the code as I want the robot to stop when there is a object placed in front of it, but if the object quickly moves away, for the robot to keep on going forward.

Here is my attempt inside of the void loop :

void loop() {
 int distanceR = 0;
 int distanceL =  0;
 delay(40);
 distance = readPing();
 if (distance >= 15){
  while(distance>= 15){
    moveForward();
    distance = readPing();
  }
 }

 if(distance<=15)
 {
  moveStop();
  delay(100);
  moveBackward();
  delay(300);
  moveStop();
  delay(200);
  distance = readPing();

  distanceR = lookRight();
  delay(200);
  distanceL = lookLeft();
  delay(200);
  distance = readPing();

  if (distance >= 15){
    while(distance >= 15){
      moveForward();
      distance = readPing();
    }
  }


  if(distanceR>=distanceL)
  {
    turnRight();

  }else
  {
    turnLeft();

  }
 }else
 {
  moveForward();
 }
 distance = readPing();``
}

This code works initially to stop the robot and then it keeps going, but the second time you put your hand in front of it and keeping it there, it turns but then suddenly stops. (I am trying to test that the first time an object quickly moves away and the second time it stays there.)

To me it seems that the code stops looping after I have the robot stop for the first time and runs the rest of the void loop but then stops because in the beginning I have the robot test the distance of any objects in front of it but it still doesn't move.

Any help will be appreciated.

3
  • Does it turn fully right (90degrees) before stopping? Commented Jul 1, 2018 at 6:00
  • Also how fast does your robot move? I am guessing it is very slow. If my assumption is right, then I know what the problem is. Commented Jul 1, 2018 at 6:01
  • It moves relatively fast but no it turns about 45 degrees before stopping Commented Jul 1, 2018 at 15:27

1 Answer 1

0

You have some dead code and several redundancies. Here is what it looks like when that is eliminated:

void loop() {
 int distance;
 int distanceR = 0;
 int distanceL = 0;
 delay(40);
 distance = readPing();

 while(distance>= 15){
    moveForward();
    distance = readPing();
 }

  moveStop();
  delay(100);
  moveBackward();
  delay(300);
  moveStop();
  delay(200);

  distanceR = lookRight();
  delay(200);
  distanceL = lookLeft();
  delay(200);

  if(distanceR>=distanceL)
  {
    turnRight();

  }else
  {
    turnLeft();

  }
}

This should help you see your logic more clearly.

Sign up to request clarification or add additional context in comments.

1 Comment

I added the readPing() and while loop() again in between the lookRight and lookLeft and the if statement at the end. That way, it works how I want it to now. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.