Help needed with my IR sensor for line follower...

Hey im using 2 infra red sensors for my line follower robot. I placed both the sensors on white ground, but i got a low value for my left sensor and high value for the right sensor(checked through serial monitor).. It should have been equal. when i interchanged the ports , the left sensor gave high and right sensor gave low. what is happening? please help..

Post a schematic & code, we're in the dark here without data to start from.

heres the code -

int leftmotor1=1;
int leftmotor2=2;
int rightmotor1=3;
int rightmotor2=4;
int leftsensor=A0;
int rightsensor=A2;

void setup()
{
pinMode(A0, INPUT);
pinMode(A2, INPUT);
pinMode(1,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);

}

void right()
{
digitalWrite(leftmotor1, HIGH);
digitalWrite(leftmotor2, LOW);
}

void left()
{
digitalWrite(rightmotor1, LOW);
digitalWrite(rightmotor2, HIGH);
}

void straight()
{
digitalWrite(leftmotor1, HIGH);
digitalWrite(leftmotor2, LOW);
digitalWrite(rightmotor1, LOW);
digitalWrite(rightmotor2, HIGH);
}

void loop()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
if( analogRead(leftsensor<400 && rightsensor<400))
{
straight();
}
else if( analogRead(leftsensor>400 && rightsensor<400))
{
left();
}
else if( analogRead(leftsensor>400 && rightsensor>400))
{
right();

}
}

IR_proximity_sensor.jpg

I think you have too much going on in these:
if( analogRead(leftsensor<400 && rightsensor<400)) << I don’t this is actually reading the input

Try it as
vlaue_leftsensor = analogRead(leftsensor); // capture the input level
value_rightsensor = analogRead(rightsensor); // capture the input level
if (value_leftsensor<400 && value_rightsensor<400){ … // now do the comparison

Cross-posting the same question is not going to win friends - it wastes time.
Don’t do it.

if( analogRead(leftsensor<400 && rightsensor<400)) << I don’t this is actually reading the input

Yes of course it does - it reads input one. Always. And unless it is actually grounded, it will almost always be true.

Groove, if you would please read the query once again! its not the same question i posted. last one i posted was regarding the compiler error , and this time the sensors were giving wrong data, and i wanted to know why..

when both the sensors were above a white surface, they both should have been giving a low analog input, but i checked serial monitor (NOT WITH THE ABOVE PROGRAM), but one gave around 385 and other gave around 50.. hope i made my point..

also i didnt get you " they give 1 always " ? how? if the sensor is above a black surface it gave reading close to 900, for white below 400..

also i didnt get you " they give 1 always "

 int leftsensor=A0;
 int rightsensor=A2;
if( analogRead(leftsensor<400 && rightsensor<400))  << I don't this is actually reading the input

OK, expanding your “analogRead”,

if( analogRead(14 < 400 && 16 < 400))

We get if( analogRead(1)) because both clauses are true.
And unless analogue input 1 is actually grounded, it is very unlikely to read zero, so your “if” will almost always be true.

Groove pointed this out in you other question about your missing “if”, but it seems you ignored it.

Don’t cross-post - it makes people really angry - and make sure you read and understand all responses.

Don't cross-post - it makes people really angry - and make sure you read and understand all responses.

It appears you havent really got what my query here , was. I connected the outputs of my 2 infrared sensors to analog ports 0 and 2 and checked their values.. When both the sensors are above a white surface both should give a low value say in the hundreds range, but i got 50 for left sensor and 400 for right sensor.. why is this happening? but above a black surface both are giving high values in 900s range , JUST AS EXPECTED..

void setup() { Serial.begin(9600); }

void loop() { int sensorValue = analogRead(A0);//left sensor reading int sensorValue2 = analogRead(A2);//right sensor reading serial.printIn(sensorValue2, DEC); Serial.println(sensorValue, DEC); }

This isnt reposting, this is a totally different query. and im glad you replied...

It appears you havent really got what my query here , was.

From the code you posted first in reply 2, it is impossible to see how you arrived at your conclusions about the sensor readings.

Hi guys, just notice, that "white" and "black" only related to visual range, 0.4 - 0.7 mcmeters. They could be the same reflective surface in IR , >0.8+ mcmeters. Put aluminum foil on your desk, it white as hell in IR. And second, author of the post expect similar signals from two devices. It's not industrial calibrated sensors, for "toys" accessories variation in output I-on min - 1 mA, max - 6 mA. Even if company make a selection to make a couple to minimize difference, it never the same. Just put in your code left threshold-level and right one, like you "calibrate" them individually.

jayakrishnan,

Did you fix your print statement? The first print statement will not work.

serial.printIn(sensorValue2, DEC); << this one is not correct - paste this in instead “Serial.println”
Serial.println(sensorValue, DEC);

use of capitalization is important. If you look at post #8, you have a undercase S and an uppercase I instead of a lowercase L.
You should have seen a compiler error with the code you posted.

ohh! i just typed in that code for the forum, did that correctly in the software... im still not getting the right values from the analog port.

Try putting a short delay between the 2 reads. The analog signal goes thru an internal mux and must charge up a cap that is part of the A/D circuit.

Thanks but i tried it a couple more times and it was working just fine... :D also i redid the program and now the robot works awesome :D

thanks guys :)

hi grimreaper I am also using 2 infra red sensors for my line follower robot. please send me the correct code. my mail id is embedded.kannan@gmail.com