Obstacle avoiding robot car - code problem

Hello everyone. I am trying to build an obstacle avoiding robot car for many days now but have not prevailed yet. I have managed to find what the problem really is in the project, and I believe it is the code as well as the sensor I am using. Everything is good except the robot does not turn and go forward when the sensor senses an object that is less than 15 CM. It just keep on rotating with some slight pauses in-between.

For my car, I am using a HC-SR04 ultrasonic sensor, an Arduino REV3 motor shield, an Arduino Uno, and 2 DG01D DC motors.

My code is as follows:

#include "ArduinoMotorShieldR3.h"
#define trigPin 11
#define echoPin 10


void setup() {
 Serial.begin(9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(12, OUTPUT); //Low = backwards, High = forwards, 12 is for Channel A
 pinMode(13, OUTPUT); // 13 is for Channel B
 pinMode(9, OUTPUT); //9 is brake (stop) for Channel A
 pinMode(8, OUTPUT); //8 is also brake but for Channel B
}


void turn() {

  digitalWrite(9, HIGH);
  digitalWrite(8, HIGH);
  delay(500);
  
  digitalWrite(12, LOW);
  digitalWrite(9, LOW);
  analogWrite(3, 255);
  digitalWrite(13, HIGH);
  digitalWrite(8, LOW);
  analogWrite(11, 255);
  //delay(500);
  
  digitalWrite(9, HIGH);
  digitalWrite(8, HIGH);
  delay(500);
}


void forward() {
  digitalWrite(12, LOW);
  digitalWrite(9, LOW);
  analogWrite(3, 160);
  digitalWrite(13, LOW);
  digitalWrite(8, LOW); 
  analogWrite(11, 160);
  
}


void loop() {

  long duration, distance;
  digitalWrite(trigPin,LOW);
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);    
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.4; //convert to cm
 
  if (distance < 15){
    Serial.print(distance);
    Serial.println(" CM");
    Serial.println("Turning");
    turn();
    //delay(500);
  }

  else {
    Serial.println("Going forward");
    delay(15);
    forward();
  }



}

The sensor is working fine, I have already tried its calibration. The motors are fine too, I have also calibrated them with a different code. But for some reason, when I put the sensor and the motors together, it just doesn’t work as it is supposed to. I am sure the code is the culprit, but I maybe wrong, I am only a beginner :slight_smile:

Your help is greatly appreciated, thank you!

When you say it keeps on rotating, do you mean the robot’s sort of spinning on its axis?

What do your Serial prints of the distance say? Is it still showing <15 even when it has turned away from the obstacle?

(I would be inclined to move that print of distance out of the if, into the body of loop() right after the line where distance is calculated; seems more sensible to me)

the robot does not turn and go forward when the sensor senses an object that is less than 15 CM.

The robot is doing what the program tells it to do.

The if statement specifies only that the robot should turn when the distance is less than 15 cm.

Otherwise, it is to go forward.

Hi,
Welcome to the Forum.

void forward() {
  digitalWrite(12, LOW);
  digitalWrite(9, LOW);
  analogWrite(3, 160);
  digitalWrite(13, LOW);
  digitalWrite(8, LOW); 
  analogWrite(11, 160);

In this sample of your code you are using pins 3 and 11 as outputs but you do not pinMode them.
Also pin 11 is the trig pin of the ultrasonic unit.
Can I suggest you use names for your pins to avoid this duplication.

byte motAdirPin = 12;
byte motBdirPin = 13;
byte motAbrakePin = 9;
byte motBbrakePin = 8;
void setup() {
 Serial.begin(9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(motAdirPin, OUTPUT); //Low = backwards, High = forwards, 12 is for Channel A
 pinMode(motBdirPin, OUTPUT); // 13 is for Channel B
 pinMode(motAbrakePin, OUTPUT); //9 is brake (stop) for Channel A
 pinMode(motBbrakePin, OUTPUT); //8 is also brake but for Channel B
}

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Thanks.. Tom.. :slight_smile:

Thank you for the replies. I will use names for the pins to avoid future complication, so thank you for the suggestion Tom! Also, by circuit, do you mean the all the connections of the motor shield with the sensor and motors? Sorry I am new to this, so please bare with me!

Replying to JuggerHorn, the serial monitor shows "Forward" when distance is greater than 15 CM, and inversely shows the distance and "turning" when distance is less than 15 CM. And yes, by rotating I mean the robot spins on its axis.

Thanks for the help once again!

Update: I got the robot to spin 90 degrees when an object is within its 15 CM distance, however, now it does not move forward. The serial monitor shows the else statement working by showing "Going forward", but the motors stay stationary. So frustrating!

Hi,

Post you new code.
What shield are you using?
A simple diagram showing the shield/uno as a square and then show wires to the motors and power supplies, labeling the pins/terminals will do.

Tom... :slight_smile:

SINGH:
Update: I got the robot to spin 90 degrees when an object is within its 15 CM distance, however, now it does not move forward. The serial monitor shows the else statement working by showing "Going forward", but the motors stay stationary. So frustrating!

You may be sure that the program is doing EXACTLY what you have told it to do. Think about the problem with that in mind.

And remember that a computer is incredibly stupid - you need to tell it every little thing that it must do.

It can be very useful to work through the program line by line using your brain instead of the Arduino and writing down the values of variables on a piece of paper and following all the logic branches.

...R

Hello Tom. I am using the Arduino REV3 motor shield. I also can't seem to complete my CAD circuit as Tinkercad does not have the HC-SRO4 sensor and the REV3 motor shield.

For my new code, I did not change anything but the pins for Trig and Echo. I have gotten the robot to spin but have not figured out why it does not go forward after spinning. i tried adding the FORWARD(); function but no success. I noticed that the FORWARD() and well as the TURN() function work properly as I did a test run without the sensor. After this, I managed to get the robot moving forward after spinning by changing the TRIG to 12, and the echo to 13. However, now the sensor does not sense properly, it just shows 0 CM!

Also, as the REV3 motor shield uses pins to control the motors such as 12 and 13 for motor direction or 9 and 8 for motor brakes, is it possible that the sensor pins are interfering with the motor shield pins?

Thank you so much for your help!

SINGH:
Hello Tom. I am using the Arduino REV3 motor shield. I also can't seem to complete my CAD circuit as Tinkercad does not have the HC-SRO4 sensor and the REV3 motor shield.

But your hands and a pen or pencil and a sheet of paper have ALL the symbols you need.
Then take a picture and post it.
Tom... :slight_smile:

Sure! Here is your badly drawn circuit you wished :slight_smile: Please note that the code is same but the TRIG and ECHO pins are different.

Just to add, I finally got the robot to work. It goes forward when there is no obstacle, and spins when there is an object within 15 CM. But for some reason that my brain cannot figure out, it only works when the robot is plugged in from the laptop. When I try to use external battery source, it just keeps on spinning!

Thanks for your help!

Here is your badly drawn circuit you wished

Where?

It sounds like your battery pack is too small to handle the motor current. Surely it is not a 9V block battery, intended for smoke alarms.

Sorry for the image delay, I got a post-limit error. The battery pack I am using is 5V.

There are no 5V battery packs. What are you using instead?

I am using 4 AA batteries for my external power source. They give a current of 5V altogether.

SINGH:
current of 5V

You may find it worthwhile to read this comparison of voltage and current.

Image from Reply #10 so we don’t have to download it. See this Simple Image Guide

36c34bd1e4075e03fec9feb281caefb25c3bcd8d.jpg

…R

As well as being badly lit your picture is upside down. Have another go, please.

Help us to help you!

…R

Four AA batteries should give 6 V. If they give only 5 V, they are probably empty and the voltage drops even more, when the motors are supposed to run, leading to only one motor running or something similar.

Johan_Ha:
Four AA batteries should give 6 V. If they give only 5 V, they are probably empty

Rechargeables are nominally 1.2V, which is why servo specs (for example) list voltage requirements of 4.8 to 6, namely 4x rechrageables (@1.2) or 4x non-recheargeables (@1.5).

So 4x rechargebles giving 5V together does not mean they're empty, at all.

(That said, of course OP didn't say what they are, but @Johan_Ha you shouldn't assume one or the other.)