Go Down

Topic: Line following and Obstacle Avoiding robot not working (Read 331 times) previous topic - next topic

TEPmontyYT

Hi there,

I am currently making a line following and obstacle avoiding robot which is supposed to follow a line and in case if there is an obstacle on the line, the robot will be able to direct away from it and then follow the line again, this is my code:

#include <NewPing.h>        //Ultrasonic sensor function library. You must install this library
 int vSpeed = 110;                      
 int turn_speed = 230;       // 0 - 255  max
 int t_p_speed = 125;
 int stop_distance = 12;
 int turn_delay = 10;


//HC-SR04 Sensor connection
 const int trigPin = 11;
 const int echoPin = 12;

//L293 Connection  
 const int motorA1      = 3;  
 const int motorA2      = 4;
 const int motorAspeed  = 5;
 const int motorB1      = 7;
 const int motorB2      = 8;
 const int motorBspeed  = 6;

//Sensor Connection
 const int left_sensor_pin =9;
 const int right_sensor_pin =10;

 
 int turnspeed;
 int left_sensor_state;
 int right_sensor_state;

 long duration;
 int distance;

void setup() {
 pinMode(motorA1, OUTPUT);
 pinMode(motorA2, OUTPUT);
 pinMode(motorB1, OUTPUT);
 pinMode(motorB2, OUTPUT);

 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
   
 Serial.begin(9600);

 delay(3000);                              
 



 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance= duration*0.034/2;
 Serial.print("Distance: ");
 Serial.println(distance);


left_sensor_state = digitalRead(left_sensor_pin);
right_sensor_state = digitalRead(right_sensor_pin);



if(right_sensor_state == HIGH && left_sensor_state == LOW)
{
 Serial.println("turning right");

 digitalWrite (motorA1,LOW);
 digitalWrite(motorA2,HIGH);                      
 digitalWrite (motorB1,LOW);
 digitalWrite(motorB2,HIGH);

 analogWrite (motorAspeed, vSpeed);
 analogWrite (motorBspeed, turn_speed);
 
 }
if(right_sensor_state == LOW && left_sensor_state == HIGH)
{
 Serial.println("turning left");
 
 digitalWrite (motorA1,HIGH);
 digitalWrite(motorA2,LOW);                      
 digitalWrite (motorB1,HIGH);
 digitalWrite(motorB2,LOW);

 analogWrite (motorAspeed, turn_speed);
 analogWrite (motorBspeed, vSpeed);

 delay(turn_delay);
 }

if(right_sensor_state == LOW && left_sensor_state == LOW)
{
 Serial.println("going forward");

 digitalWrite (motorA1,LOW);
 digitalWrite(motorA2,HIGH);                      
 digitalWrite (motorB1,HIGH);
 digitalWrite(motorB2,LOW);

 analogWrite (motorAspeed, vSpeed);
 analogWrite (motorBspeed, vSpeed);

 delay(turn_delay);
 
 }

if(right_sensor_state == HIGH && left_sensor_state == HIGH)
{
 Serial.println("stop");
 
 analogWrite (motorAspeed, 0);
 analogWrite (motorBspeed, 0);
 while(true){
 
}
 }

if(distance < stop_distance)
{

 digitalWrite (motorA1,HIGH);
 digitalWrite(motorA2,LOW);                      
 digitalWrite (motorB1,LOW);
 digitalWrite(motorB2,HIGH);
 delay(250);
 analogWrite (motorAspeed, 0);
 analogWrite (motorBspeed, 0);
 delay(500);
 digitalWrite (motorA1,HIGH);
 digitalWrite(motorA2,LOW);                      
 digitalWrite (motorB1,HIGH);
 digitalWrite(motorB2,LOW);

 analogWrite (motorAspeed, t_p_speed);
 analogWrite (motorBspeed, t_p_speed);
 delay(900);


 digitalWrite (motorA1,LOW);
 digitalWrite(motorA2,HIGH);                      
 digitalWrite (motorB1,HIGH);
 digitalWrite(motorB2,LOW);

 analogWrite (motorAspeed, t_p_speed);
 analogWrite (motorBspeed, t_p_speed);
 delay(800);

 digitalWrite (motorA1,LOW);
 digitalWrite(motorA2,HIGH);                      
 digitalWrite (motorB1,LOW);
 digitalWrite(motorB2,HIGH);
 delay(900);

 digitalWrite (motorA1,LOW);
 digitalWrite(motorA2,HIGH);                      
 digitalWrite (motorB1,HIGH);
 digitalWrite(motorB2,LOW);

 delay(700);

  digitalWrite (motorA1,LOW);
 digitalWrite(motorA2,HIGH);                      
 digitalWrite (motorB1,LOW);
 digitalWrite(motorB2,HIGH);
 delay(650);

 digitalWrite (motorA1,LOW);
 digitalWrite(motorA2,HIGH);                      
 digitalWrite (motorB1,HIGH);
 digitalWrite(motorB2,LOW);

 left_sensor_state == HIGH;

 while(left_sensor_state == LOW){

 left_sensor_state = digitalRead(left_sensor_pin);
 right_sensor_state = digitalRead(right_sensor_pin);
 Serial.println("in the first while");
 

}

 digitalWrite (motorA1,HIGH);
 digitalWrite(motorA2,LOW);                      
 digitalWrite (motorB1,LOW);
 digitalWrite(motorB2,HIGH);
 delay(100);
 
 digitalWrite (motorA1,HIGH);
 digitalWrite(motorA2,LOW);                      
 digitalWrite (motorB1,HIGH);
 digitalWrite(motorB2,LOW);
 delay (500);

 }
}

void loop() {
}


The void loop has nothing in it because whenever I put the code in it, the whole entire robot doesn't even move, now the problem that I am facing is that the motors either move backwards, or one moves forwards and the other one doesn't, the robot doesn't stop if there is an obstacle in front of it or when there is no line at all, neither does it stop if the IR sensors are on top of a black strip even though the infrared sensors are sensing the black indicated by the leds of the ir sensors.

Here is a link to a video of what is going on:
https://www.youtube.com/watch?v=WNIiwZjY6hY&feature=youtu.be

Please help, thank you and have a nice day!

slipstick

With the code in setup() it does what it does once only and then does nothing. In particular it only ever reads the sensors once and never again. That's completely useless.

Put most of the code back in loop() where it belongs, post that version (using code tags as in"How to use this forum" at the top of the forum) and give details of what works and what doesn't. Then we can try to solve whatever the problems are when the program at least looks like it could possibly work.

Steve

Delta_G

You'll also need to learn about handling the timing without using delay.  You need non-blocking code.  Otherwise your bot won't be responsive.  It will only see obstacles if they happen to pop up at exactly the right moment to be seen.  Otherwise most of the time it isn't looking. 

There should be plenty of examples of line followers and obstacle avoiders for you to look at online.  Why don't you do a little research here.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

TEPmontyYT

Dear slipstick and Delta_G, I have edited the code so that there is more stuff in the loop function,here is the code that I have now made:


Code: [Select]
#include <NewPing.h> 
  int vSpeed = 110;                     
  int turn_speed = 230;       // 0 - 255  max
  int t_p_speed = 125;
  int stop_distance = 12;
  int turn_delay = 10;


//HC-SR04 Sensor connection
  const int trigPin = 11;
  const int echoPin = 12;

//L293 Connection   
  const int motorA1      = 3; 
  const int motorA2      = 4;
  const int motorAspeed  = 5;
  const int motorB1      = 7;
  const int motorB2      = 8;
  const int motorBspeed  =6;

//Sensor Connection
  const int left_sensor_pin =9;
  const int right_sensor_pin =10;

 
  int turnspeed;
  int left_sensor_state;
  int right_sensor_state;

  long duration;
  int distance;

void setup() {
  pinMode(motorA1, OUTPUT);
  pinMode(motorA2, OUTPUT);
  pinMode(motorB1, OUTPUT);
  pinMode(motorB2, OUTPUT);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
   
  Serial.begin(9600);

  delay(3000);                             
 
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance= duration*0.034/2;
  Serial.print("Distance: ");
  Serial.println(distance);


left_sensor_state = digitalRead(left_sensor_pin);
right_sensor_state = digitalRead(right_sensor_pin);



if(right_sensor_state == HIGH && left_sensor_state == LOW)
{
  Serial.println("turning right");

  digitalWrite (motorA1,LOW);
  digitalWrite(motorA2,HIGH);                       
  digitalWrite (motorB1,LOW);
  digitalWrite(motorB2,HIGH);

  analogWrite (motorAspeed, vSpeed);
  analogWrite (motorBspeed, turn_speed);
 
  }
if(right_sensor_state == LOW && left_sensor_state == HIGH)
{
  Serial.println("turning left");
 
  digitalWrite (motorA1,HIGH);
  digitalWrite(motorA2,LOW);                       
  digitalWrite (motorB1,HIGH);
  digitalWrite(motorB2,LOW);

  analogWrite (motorAspeed, turn_speed);
  analogWrite (motorBspeed, vSpeed);

  delay(turn_delay);
  }

if(right_sensor_state == LOW && left_sensor_state == LOW)
{
  Serial.println("going forward");

  digitalWrite (motorA1,LOW);
  digitalWrite(motorA2,HIGH);                       
  digitalWrite (motorB1,HIGH);
  digitalWrite(motorB2,LOW);

  analogWrite (motorAspeed, vSpeed);
  analogWrite (motorBspeed, vSpeed);

  delay(turn_delay);
 
  }

if(right_sensor_state == HIGH && left_sensor_state == HIGH)
{
  Serial.println("stop");
 
  analogWrite (motorAspeed, 0);
  analogWrite (motorBspeed, 0);
  while(true){
 
 }
  }

 if(distance < stop_distance)
 {

  digitalWrite (motorA1,HIGH);
  digitalWrite(motorA2,LOW);                       
  digitalWrite (motorB1,LOW);
  digitalWrite(motorB2,HIGH);
  delay(250);
  analogWrite (motorAspeed, 0);
  analogWrite (motorBspeed, 0);
  delay(500);
  digitalWrite (motorA1,HIGH);
  digitalWrite(motorA2,LOW);                       
  digitalWrite (motorB1,HIGH);
  digitalWrite(motorB2,LOW);

  analogWrite (motorAspeed, t_p_speed);
  analogWrite (motorBspeed, t_p_speed);
  delay(900);


  digitalWrite (motorA1,LOW);
  digitalWrite(motorA2,HIGH);                       
  digitalWrite (motorB1,HIGH);
  digitalWrite(motorB2,LOW);

  analogWrite (motorAspeed, t_p_speed);
  analogWrite (motorBspeed, t_p_speed);
  delay(800);

  digitalWrite (motorA1,LOW);
  digitalWrite(motorA2,HIGH);                       
  digitalWrite (motorB1,LOW);
  digitalWrite(motorB2,HIGH);
  delay(900);

  digitalWrite (motorA1,LOW);
  digitalWrite(motorA2,HIGH);                       
  digitalWrite (motorB1,HIGH);
  digitalWrite(motorB2,LOW);

  delay(700);

   digitalWrite (motorA1,LOW);
  digitalWrite(motorA2,HIGH);                       
  digitalWrite (motorB1,LOW);
  digitalWrite(motorB2,HIGH);
  delay(650);

  digitalWrite (motorA1,LOW);
  digitalWrite(motorA2,HIGH);                       
  digitalWrite (motorB1,HIGH);
  digitalWrite(motorB2,LOW);

  left_sensor_state == HIGH;

  while(left_sensor_state == LOW){

  left_sensor_state = digitalRead(left_sensor_pin);
  right_sensor_state = digitalRead(right_sensor_pin);
  Serial.println("in the first while");
 
 
}

  digitalWrite (motorA1,HIGH);
  digitalWrite(motorA2,LOW);                       
  digitalWrite (motorB1,LOW);
  digitalWrite(motorB2,HIGH);
  delay(100);
 
  digitalWrite (motorA1,HIGH);
  digitalWrite(motorA2,LOW);                       
  digitalWrite (motorB1,HIGH);
  digitalWrite(motorB2,LOW);
  delay (500);

  }
}[code]
[/code]


What's happening now is that the robot moves backwards for about five seconds and stops, it is not following the line, it is unable to avoid obstacles, and moves for a much, much shorter amount of time.

PaulS

Code: [Select]
left_sensor_state = digitalRead(left_sensor_pin);
right_sensor_state = digitalRead(right_sensor_pin);

You have some undefined sensor connected to two pins. for which you have not set the mode. Are we supposed to guess what kind of sensors they are, and what you read from them?
The art of getting good answers lies in asking good questions.

TEPmontyYT

Dear PaulS

They are two infrared sensors, for the mode, I do not know what you mean by setting it, if it is possible could you please send me an example of this, my infrared sensor on the left has an output to digital 10 on the arduino and my right hand side infrared sensor has an output to digital 9 on the arduino

PaulS

When the IR sensor is not outputting data, what state is the pin in? HIGH? LOW? You don't know?

You should be using a pinMode() of INPUT_PULLUP so you CAN answer the question.

You should be printing the values you read from the pins, so you KNOW what is happening.
The art of getting good answers lies in asking good questions.

TEPmontyYT

Sorry PaulS I am not at all skilled with arduino code,  but if it is possible could you please edit my code and send it to me in your next reply, that would be really helpful and nice, thank you.

Go Up