need help with obstacle avoiding robot code.

I have an elegoo 4 wheeled robot. I can get programme it to turn left, right forward and back all correctly. but when I try to get it to obstacle avoid it stops correctly at the wall, looks around and then just sits there. Any help would be appreciate.

#include <Servo.h> //servo library
Servo myservo; // create servo object to control servo
int Echo = A4;
int Trig = A5;
int in1 = 6;
int in2 = 7;
int in3 = 8;
int in4 = 9;
int ENA = 5;
int ENB = 11;
int ABS = 110;
int rightDistance = 0,leftDistance = 0,middleDistance = 0 ;
void _mForward()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.println("go forward!");
}

void _mBack()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.println("go back!");
}

void _mleft()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.println("go left!");
}

void _mright()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.println("go right!");
}
void _mStop()
{
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);
Serial.println("Stop!");
}
/Ultrasonic distance measurement Sub function/
int Distance_test()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(20);
digitalWrite(Trig, LOW);
float Fdistance = pulseIn(Echo, HIGH);
Fdistance= Fdistance/58;
return (int)Fdistance;
}

void setup()
{
myservo.attach(3);// attach servo on pin 3 to servo object
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
_mStop();
}

void loop()
{
myservo.write(90);//setservo position according to scaled value
delay(500);
middleDistance = Distance_test();
#ifdef send
Serial.print("middleDistance=");
Serial.println(middleDistance);
#endif

if(middleDistance<=20)
{
_mStop();
delay(500);
myservo.write(5);
delay(1000);
rightDistance = Distance_test();

#ifdef send
Serial.print("rightDistance=");
Serial.println(rightDistance);
#endif

delay(500);
myservo.write(90);
delay(1000);
myservo.write(180);
delay(1000);
leftDistance = Distance_test();

#ifdef send
Serial.print("leftDistance=");
Serial.println(leftDistance);
#endif

delay(500);
myservo.write(90);
delay(1000);
if(rightDistance>leftDistance)
{
_mright();
delay(180);
}
else if(rightDistance<leftDistance)
{
_mleft();
delay(180);
}
else if((rightDistance<=20)||(leftDistance<=20))
{
_mBack();
delay(180);
}
else
{
_mForward();
}
}
else
_mForward();
}

I don't see anything in the code that would cause it to stop. What is the serial output saying when the robot stops?

Hello,
I have the same issue.
I would also like to know the meaning of "#ifdef send .........#endif".
Thank you

johncc33:
I would also like to know the meaning of "#ifdef send .........#endif".

It means:
If there is a "#define send" in the program, include the lines between the #ifdef and #endif.
This is debug output you can display on Serial Monitor to watch the progress of your program. By having the debug output be 'conditional' you can make the final program smaller and faster when you are done with debugging.

Scott6428:
I have an elegoo 4 wheeled robot. I can get programme it to turn left, right forward and back all correctly. but when I try to get it to obstacle avoid it stops correctly at the wall, looks around and then just sits there. Any help would be appreciate.

Hi, had the same problem. I putted the distance_test() and some tests with mright(), mleft(), mForward() into the loop and there was no problem, but the right wheels didn't move.

I was wondering about the difference of ENB (corrected!) compared to the former setups. And here comes the hint: have a look to "Obstacle Avoidance Car.pdf" on page 2. They describe, that you cannot use Pin 10 and therefore use Pin 11. The drawback is, that you have to remove one line tracking module. And then it works.

Hope that helps...

Thank you Xentity, that worked !

Appreciate all that replied.