0

I have made a line following robot using 2 IR sensor. My line following robot was working successfully with this code.

#define LS 2 // left sensor
#define RS 3 // right sensor
/*-------definning Outputs------*/
int LM1= 5; // left motor
int LM2= 6; // left motor
int RM1= 9; // right motor
int RM2= 10;
void setup()
{
 pinMode(LS, INPUT);
 pinMode(RS, INPUT);
 pinMode(LM1, OUTPUT);
 pinMode(LM2, OUTPUT);
 pinMode(RM1, OUTPUT);
 pinMode(RM2, OUTPUT);
}
void loop()
{
 if(digitalRead(LS) && digitalRead(RS)) // Move Forward
 {
 digitalWrite(LM1, HIGH);
 digitalWrite(LM2, LOW);
 digitalWrite(RM1, HIGH);
 digitalWrite(RM2, LOW);
 }
 else if(!(digitalRead(LS)) && digitalRead(RS)) // Turn right
 {
 digitalWrite(LM1,HIGH);
 digitalWrite(LM2, LOW);
 analogWrite(RM1,70);
 digitalWrite(RM2,LOW);
 }
 else if(digitalRead(LS) && !(digitalRead(RS))) // turn left
 {
 analogWrite(LM1,70);
 digitalWrite(LM2, LOW);
 digitalWrite(RM1,HIGH);
 digitalWrite(RM2,LOW);
 }else{
 digitalWrite(LM1, LOW);
 digitalWrite(LM2, LOW);
 digitalWrite(RM1,LOW);
 digitalWrite(RM2, LOW);
 }
}

As I used only two sensor,sometimes it goes out of the line. So I have tried to make an advanced line following robot which could get back to track automatically when it goes out of the line.But the code is taking only the 1st value of tmp.(tmp=1) even when left sensor touches the line after white sensor and the robot goes out of the line.The value of tmp should be 2 when the robot goes out of the line and the left sensor touches the line after the white sensor. Here is the code:

/*-------definning Inputs------*/
#define LS 2 // left sensor
#define RS 3 // right sensor
/*-------definning Outputs------*/
int LM1= 5; // left motor
int LM2= 6; // left motor
int RM1= 9; // right motor
int RM2= 10; //right motor
int tmp;
void setup()
{
 pinMode(LS, INPUT);
 pinMode(RS, INPUT);
 pinMode(LM1, OUTPUT);
 pinMode(LM2, OUTPUT);
 pinMode(RM1, OUTPUT);
 pinMode(RM2, OUTPUT);
}
void loop()
{
 if(digitalRead(LS) && digitalRead(RS)) // Move Forward
 {
 digitalWrite(LM1, HIGH);
 digitalWrite(LM2, LOW);
 digitalWrite(RM1, HIGH);
 digitalWrite(RM2, LOW);
 }
 else if(!(digitalRead(LS)) && digitalRead(RS)) // Turn right
 {
 digitalWrite(LM1,HIGH);
 digitalWrite(LM2, LOW);
 analogWrite(RM1,70);
 digitalWrite(RM2,LOW);
 tmp=1;
 }
 else if(digitalRead(LS) && !(digitalRead(RS))) // turn left
 {
 analogWrite(LM1,70);
 digitalWrite(LM2, LOW);
 digitalWrite(RM1,HIGH);
 digitalWrite(RM2,LOW);
 tmp=2;
 }
 else//out of the line
 {
 if(tmp=1){
 analogWrite(LM1,70);
 digitalWrite(LM2, LOW);
 digitalWrite(RM1,LOW);
 analogWrite(RM2,70);
 }else if(tmp=2){
 digitalWrite(LM1,LOW);
 analogWrite(LM2, 70);
 analogWrite(RM1,70);
 digitalWrite(RM2,LOW);
 }
 }
}
Edgar Bonet
45.1k4 gold badges42 silver badges81 bronze badges
asked Nov 7, 2016 at 9:19

1 Answer 1

1

This test:

if (tmp = 1)

is always true. You are assigning 1 to tmp and that value (1), when interpreted in a boolean context (i.e. as a truth value), is true.

You probably mean

if (tmp == 1)

and similarly for the next test.

answered Nov 7, 2016 at 10:56
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.