Making an arduino line follower with obstacle avoidance

I have had an arduino for a wile now but i just cannot find out how to make a robot with line following and obstacle avoidance.

How can I write a sketch that will alow one sensor to have more power over another sensor? So if my robot is moving along a line and i put something infrunt of it it will sotp useing the line sensors and use the code for the obstacle avoidance.

 

111swords

Try something like this:if

Try something like this:

if (obstacleDetected?)

{

avoidObstacle();

}

else

{

lineFollow();

}

I’ve tryed that but what
I’ve tryed that but what should I do if I have 2 objected detectors and 2 line sensors?

GeneralGeek’s response
Still holds true. You just need to check however many sensors you have.

I have a question

Have you managed to build an obstacle avoiding robot yet? I see you have joined this month so we have no idea what your capabilities are. Beginners we usually direct to the start here page (link at top) which is a basic object avoiding robot project.

You need to break down the problem into parts. After you have built the object avoidance or start here robot then you can pull it apart and attempt a line follower. There are many examples of line followers here if you search.

After you have built the start here robot and a line follower it may become clear what the General and Bird have suggested to you. And begin to understand how to combine the two behaviours.

Yes I have made a line

Yes I have made a line following robot,obstacle avoidance robot, light seeking robot,IR tip wire,windows gaming controler andother things. I just cannot add 2 different abilitys together like navigating to a bright light sorse wile moving around things.

this is my code so far

int m11 = 11; //motor inable, for motor 1

int m10 = 10; //motor right, for motor 1

int m9  = 9; //motor left, for motor 1

int m7  = 7; //motor inable, for motor 2

int m6  = 6; //motor left, for motor 2

int m5  = 5; //motor left, for motor 2

int s3  = 3; //sensor1

int s2  = 2; //sensor 2

 

 

void setup() {

 

  pinMode(m11, OUTPUT);

  pinMode(m10, OUTPUT);

  pinMode(m9, OUTPUT);

  pinMode(m7, OUTPUT);

  pinMode(m6, OUTPUT);

  pinMode(m5, OUTPUT);

  pinMode(s3, INPUT);

  pinMode(s2, INPUT);

 

boolean running = true;

 

 

}

 

void loop() 

{

 

if (digitalRead(s2) == LOW && digitalRead(s3) == LOW)

  {

  digitalWrite(m11, HIGH);

  digitalWrite(m10, HIGH);

  digitalWrite(m9, LOW);

  digitalWrite(m7, HIGH);

  digitalWrite(m6,LOW);

  digitalWrite(m5, HIGH);

   delay(500);

}

  else if (digitalRead(s2) == LOW)

  {

  digitalWrite(m11, HIGH);

  digitalWrite(m10, HIGH);

  digitalWrite(m9, LOW);

  digitalWrite(m7, LOW);

  digitalWrite(m6, LOW);

  digitalWrite(m5, LOW);

   delay(500);

}

 

 

else if (digitalRead(s3) == LOW)

  {

  digitalWrite(m11, LOW);

  digitalWrite(m10, LOW);

  digitalWrite(m9, LOW);

  digitalWrite(m7, HIGH);

  digitalWrite(m6, HIGH);

  digitalWrite(m5, LOW);

   delay(500);

  }

 

 

    else

{

  digitalWrite(m11,HIGH);

  digitalWrite(m10, LOW);

  digitalWrite(m9, HIGH);

  digitalWrite(m7, HIGH);

  digitalWrite(m6,  LOW);

  digitalWrite(m5, HIGH);

 

 

 

}

 

}

Given I don’t know what each

Given I don’t know what each sensor is, and how your motor driver works I can’t really understand that code. Could you try posting pseudocode instead? (English words that are easy to understand in the same format as your code)

e.g. if (lightsensor sees dark) etc…

Also you can set it up to do

Also you can set it up to do it manually with a SPDT switch with say analog pin A2 going to switch (center) then connect the GND one side and V+ the other side of switch.

So when you flick the switch one way to V+ in your circuit the arduino will detect it as HIGH and switch to line follow mode.

If you flick swith the other way to GND in your circuit the arduino will detect as LOW and switch to explore mode.

Here is a rough example for code it may help…

 

int mode = LOW;                                           //add a variable such as LOW = explore

 

void setup()

if (digitalRead(A2) == HIGH)                           // Read mode jumper

{    
    mode = HIGH;                                           // Line follow mode

}

 

void loop() {                                                   //add SPDT s/w to switch between modes
  if (mode)
    mode_line_follow();
  else
    mode_explore();
}

I have a couple of suggestions

give your variables more useful names. m11 doesn’t tell me what the variable does, while rightMotorEnable would tell you without reading anymore of the program that you are looking at the right motor enable. Also, functions/procedures are a good thing and very useful in C and C type languages.

For example:

  digitalWrite(m11, HIGH);
  digitalWrite(m10, HIGH);
  digitalWrite(m9, LOW);
  digitalWrite(m7, HIGH);
  digitalWrite(m6,LOW);
  digitalWrite(m5, HIGH);
  delay(500);

Could be put in a function.

forward() {
  digitalWrite(m11, HIGH);
  digitalWrite(m10, HIGH);
  digitalWrite(m9, LOW);
  digitalWrite(m7, HIGH);
  digitalWrite(m6,LOW);
  digitalWrite(m5, HIGH);
  delay(500);

}

And then you could use your if statement checking if the sensors are low { forward(); }

It will make your code more readable and allow you to change small parts of code and not affect anything else.

So this is my code int

So this is my code 

int motor right = 11;

int motor left = 10;

int light sensor 2  = 3;

int light sensor 1  = 2;

 

 

void setup() {

 

  pinMode(motor right, OUTPUT);

  pinMode(motor left, OUTPUT);

  pinMode(light sensor 1, INPUT);

  pinMode(light sensor 2, INPUT);

 

boolean running = true;

 

 

}

 

void loop() 

{

 

 

if (digitalRead(light sensor 1) == LOW && digitalRead(light sensor 2) == LOW)

  {

  digitalWrite(motor right, HIGH);

  digitalWrite(motor left, HIGH);

 

   delay(500);

}

  else if (digitalRead(light sensor 2) == LOW)

  {

  digitalWrite(mottor right, LOW);

  digitalWrite(motor left, HIGH);

 

   delay(500);

}

 

 

else if (digitalRead(light sensor 1) == LOW)

  {

  digitalWrite(motor right, HIGH);

  digitalWrite(motor left, LOW);

 

   delay(500);

  }

 

 

    else

{

  digitalWrite(motor right, HIGH);

  digitalWrite(motor left, HIGH);

 

 

}

 

}

Now say I have 2 of the same sketches but one is for obstacle avoidence and the other for light following how can I put them in to a function?

I think the function is what I need can some pleas show me how to put one in my sketch, so I can make some thing like the first reply thanks.

111swords

variable names

can not have spaces in them. motor_right or motorRight but not motor right 
It won’t work. 

Wouldn’t this be a good place for and interrupt?

I’m still a nubee in the Arduino arena, but this certainly sounds like a good place to use an interrupt.  Couldn’t 111swords use a potentiometer in series with the analog output of the sonar?  Since the output of the sonar is simply a variable voltage, it should trip a hardware interrupt at a certain point (>3V?).  He could vary the range to trip with the pot.  This would do what he is looking for - follow the line until the interrupt is tripped.  

**interrupts are useful, but, **

I don’t have a good grasp on using them. Also, they aren’t needed in this case. His issue is a simple condition; follow line until I see an obstacle, then avoid obstacle, and finally, follow line again. The problem will come after avoiding the object. He will have to find the line again. That will require having positional awareness.

*Edit - fixed the capitalization

help

could some one give me code of ligne follower avoidance obstacles .?