Pages: [1]   Go Down
Author Topic: Trying to programm a line follower robot  (Read 1368 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello all,
i'm trying to programm my line follower robot.i use 2 DC motors controlled by a L293D chip,3 IR sensors (left,right,middle) and a photoresistor sensor as a 'Start Button'.Every sensor and the motors work properly as i test them by simple programms.
I have a problem trying to programm this.Here is my code:

Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

int motorLEFTpin1 = 8;              //define digital output pin no.
int motorLEFTpin2 = 9;              //define digital output pin no.
int motorRIGHTpin1 = 10;
int motorRIGHTpin2 = 11;

void setup () {
  Serial.begin(9600);
 
  pinMode(motorLEFTpin1,OUTPUT);        //set pin 8 as output
  pinMode(motorLEFTpin2,OUTPUT);        // set pin 9 as output
  pinMode(motorRIGHTpin1,OUTPUT);       // set pin 10 as output
  pinMode(motorRIGHTpin2,OUTPUT);        // set pin 11 as output
}

int analogPin0 = 0;        //pin0 =  Start
int analogPin1 = 1;        // pin1 = middle IR
int analogPin2 = 2;        //pin2 = right IR
int analogPin3 = 3;        // pin3 = left IR

int val0 = 0;           // variable to store the value read
int val1 = 0;
int val2 = 0;
int val3 = 0;

void loop()
{
  val0 = analogRead(analogPin0); 
  val1 = analogRead(analogPin1);
  val2 = analogRead(analogPin2);
  val3 = analogRead(analogPin3);       // read the input pin

 
  Serial.println(val0); 
  Serial.println("\t");
  Serial.println(val1);
  Serial.println("\t");
  Serial.println(val2);
  Serial.println("\t");
  Serial.println(val3);
  Serial.println("\t");
  Serial.println("\t");
  Serial.println("\t");
  Serial.println("\t");
 
 
  if ( val0 > 900 && val2 > 900 && val3 > 900 && val1 > 900) {                  //START
   
    digitalWrite(motorLEFTpin1,LOW);
    digitalWrite(motorLEFTpin2,HIGH);
    digitalWrite(motorRIGHTpin1,HIGH);
    digitalWrite(motorRIGHTpin2,LOW);
  }
Until there everything is OK.I light the photoresistance and the robot starts moving forward.The rest of the programm is this:

Code:
while ( val2 < 900 && val0 < 900) {                           //Black line at right IR,turn left
     digitalWrite(motorLEFTpin1,LOW);
     digitalWrite(motorLEFTpin2,LOW);
     digitalWrite(motorRIGHTpin1,HIGH);
     digitalWrite(motorRIGHTpin2,LOW);
   }
}

I chose to use 'while' command to make the robot turn to the left when the right IR sensor 'sees' a black line.
My problem is that the robot turns left but it remains at this situation.While command means that turning left is happening while val2<900.After that it has to go forward again.Isn't it?Where is my fault?
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You don't change val0 or val2 (Horrible choice of names BTW) in the loop, so once you're in there, you're never coming out.
Logged

Germany
Offline Offline
Faraday Member
**
Karma: 56
Posts: 2983
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Where is my fault?

loop() is supposed to define what has to be done at one moment in time.
After reading the input, deciding, eventually doing a different output than before, it should return, just to be called again.

So replace your while by an  else if ( ) ... and eventually add more decision cases
   
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I replace 'while' with 'else if' and i don't see any change.Do I have to change something at the loop ()?
Sorry if i make fool questions but i have very little experience on programming.
Logged

Germany
Offline Offline
Faraday Member
**
Karma: 56
Posts: 2983
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

perhaps  Serial.println("LEFT");
in the corresponding block(s) will give you hints about what's happening ...

I bet the robot does not know what to do ( neither START nor LEFT, but what else ??? )
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Serial.println ("LEFT").you mean to change the labels so i can see the sensor values cleraly at serial monitor?i did that but i have no problem seeing the values.I renamed my values and i did the changes at serial print.

you probably win your bet but would you like to be more specific and tell me what exactly have to do?
the new code is this:
Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

int motorLEFTpin1 = 8;              //define digital output pin no.
int motorLEFTpin2 = 9;              //define digital output pin no.
int motorRIGHTpin1 = 10;
int motorRIGHTpin2 = 11;

void setup () {
  Serial.begin(9600);
 
  pinMode(motorLEFTpin1,OUTPUT);        //set pin 8 as output
  pinMode(motorLEFTpin2,OUTPUT);        // set pin 9 as output
  pinMode(motorRIGHTpin1,OUTPUT);       // set pin 10 as output
  pinMode(motorRIGHTpin2,OUTPUT);        // set pin 11 as output
}

int analogPin0 = 0;        //pin0 =  Start
int analogPin1 = 1;        // pin1 = middle IR
int analogPin2 = 2;        //pin2 = right IR
int analogPin3 = 3;        // pin3 = left IR

int Start = 0;           // variable to store the value read
int middleIR = 0;
int rightIR = 0;
int leftIR = 0;

void loop()
{
  Start = analogRead(analogPin0); 
  middleIR = analogRead(analogPin1);
  rightIR = analogRead(analogPin2);
  leftIR = analogRead(analogPin3);       // read the input pin

 
  Serial.println(Start); 
  Serial.println("START");
  Serial.println(middleIR);
  Serial.println("MIDDLE");
  Serial.println(rightIR);
  Serial.println("RIGHT");
  Serial.println(leftIR);
  Serial.println("LEFT");
  Serial.println("\t");
  Serial.println("\t");
  Serial.println("\t");

 
 
  if ( Start > 900 && rightIR > 900 && leftIR > 900 && middleIR > 900) {                  //START
   
    digitalWrite(motorLEFTpin1,LOW);
    digitalWrite(motorLEFTpin2,HIGH);
    digitalWrite(motorRIGHTpin1,HIGH);
    digitalWrite(motorRIGHTpin2,LOW);
  }
 
   else if ( rightIR < 900 && Start < 900) {                           //Black line at right IR,turn left
     digitalWrite(motorLEFTpin1,LOW);
     digitalWrite(motorLEFTpin2,LOW);
     digitalWrite(motorRIGHTpin1,HIGH);
     digitalWrite(motorRIGHTpin2,LOW);
   }
}
Logged

Germany
Offline Offline
Faraday Member
**
Karma: 56
Posts: 2983
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Add just before the last curly brace
Code:
  else  {   
     //lost: What to do now ???
     digitalWrite(motorLEFTpin1,LOW);
     digitalWrite(motorLEFTpin2,LOW);
     digitalWrite(motorRIGHTpin1,LOW);
     digitalWrite(motorRIGHTpin2,LOW);
   }
... and you robot will stop ...
the rest is up to you smiley-wink
 
Logged

Pages: [1]   Go Up
Jump to: