Go Down

Topic: Obstacle avoiding and line tracking robot (Read 2213 times) previous topic - next topic


Jan 09, 2019, 06:32 pm Last Edit: Jan 09, 2019, 07:48 pm by priyamna
The following is a code built using an arduino software. This code is written to make the robot avoid obstacles and follow a specified path(ie. avoid black line and follow white line). The code compiles successfully but when uploaded to the robot does not work. Please help in fixing if this code. I  wanted it to avoid black line and obstacles but when the code is uploaded is not moving at all.

int trigPin = 9;
int echoPin = 10;
int revright = 4;      //REVerse motion of Right motor
int fwdleft = 7;     
int revleft= 6;       
int fwdright= 5;       //ForWarD motion of Right motor
int c = 0;
#define LS  2    // left sensor
#define RS  8    // right sensor

void setup() {
   pinMode(5, OUTPUT);
   pinMode(6, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(7, OUTPUT);
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
   pinMode(LS, INPUT);
   pinMode(RS, INPUT); 
  // put your setup code here, to run once:

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  duration=pulseIn(echoPin, HIGH);
  distance =(duration/2)/29.1;
  digitalWrite(5,HIGH);                               //       If you dont get proper movements of your robot,
   digitalWrite(4,LOW);                               //        then alter the pin numbers
   digitalWrite(6,LOW);                               //
   digitalWrite(7,HIGH);                              //
  else if(distance<20) 
   digitalWrite(6,HIGH);                                  //HIGH
if((digitalRead(LS)==HIGH) && (digitalRead(RS)==HIGH))    // Condition_1 stop
  if((digitalRead(LS)==LOW) && (digitalRead(RS)==LOW))  //CONDITION-2 FORWRD
  if((digitalRead(LS)==LOW) && (digitalRead(RS)==HIGH))  // RIGHT
  if((digitalRead(LS)==HIGH) && (digitalRead(RS)==LOW))
void MoveForward()
    analogWrite(fwdleft, 50);
    analogWrite(revleft, 0);
    analogWrite(fwdright, 50);
    analogWrite(revright, 0);

void TurnRight()
    analogWrite(fwdleft, 0);
    analogWrite(revleft, 60);
    analogWrite(fwdright, 60);
    analogWrite(revright, 0);

void TurnLeft()
    analogWrite(fwdleft, 60);
    analogWrite(revleft, 0);
    analogWrite(fwdright, 0); 
    analogWrite(revright, 60);

void Stop()
    analogWrite(fwdleft, 0);
    analogWrite(revleft, 0);
    analogWrite(fwdright, 0);
    analogWrite(revright, 0);


Jan 09, 2019, 06:39 pm Last Edit: Jan 09, 2019, 06:41 pm by vinceherman
when uploaded to the robot does not work
This is entirely unhelpful.

A much better way to describe the results of your program is to state:
What you expected to happen
What actually happened
How those 2 differ

Edit:  Also, go read the sticky post titled "How to use this forum - please read" at the top of this and every forum.  It will help you understand how to do things like post code using code tags.


Jan 11, 2019, 05:37 am Last Edit: Jan 11, 2019, 06:46 am by Willpatel_Kendmirez
Avoiding obstacles and following paths are pretty much exclusive activities, at least the way I'm picturing this.

Is it supposed to follow a line until it gets an obstacle, then go round it and pick up the line on the other side, or what?

But that aside, my advice here would be to:

  • Strip the code down to exclude the obstacle and line sensors and just get it to call the function MoveForward() just to make sure it actually works, ie all the power to the motor etc etc is all operational
  • Then edit: add call some of the other functions like TurnLeft() etc and get it test all of those functions, moving in say a square of sorts to check that all works
  • Then add only the obstacle avoiding stuff and get that to work
  • Then take the obstacle avoiding stuff out and put the line following stuff in and get that to work
  • Then and only then, try to combine both sets of activities.

Or have you already done that?

Go Up