Arduino line follow code help! (photo-resistors)

Hello, i am a newbie…and im building a line following robot. so far, i have 3 photo-resistors. and i want to make it follow a line that is distinctively different to the background. black tape on white tile…red tape on black carpet ext. i have code set up completely for moving servos and everything…i just need to know what to put in for photo-resistors to detect and follow line.

#include <Servo.h> 

Servo motorLeft;
Servo motorRight;

void setup() {


void loop()


help me fill in the blank?

I'm not going to write your code for you. I'll give you some hints, though. You have some sensors that return analog data that you need to read. Maybe the analogRead() function would be useful.

Once you have data from the analog sensors, you need to make some decisions. If the left value is larger than or smaller than the center value, do something. If the right value is larger than or smaller than the center value, do something. The if statement would be most useful, here.

Doing something is a matter of running the left side motor, the right side motor, or both motors, by writing a value to the servo. Perhaps Servo::write() would be useful.

If you aren't sure that the data you are getting is resulting in the correct decisions, it's time to write some stuff to the Serail Monitor window. Serial.begin(), Serial.print(), and Serial.println() might prove useful.

You are not the first person to build a line-following roobot. Google can provide an example or 10000.

i you helped me before, i built that ping obsticle bot that looked left with a servo mounted to the front and avoided all obstacles 10 inches or less. this is far less in level, but i need to build one, at least once. i understyand all the logic and code....for i am a java programer. its just, i was wondering about the photo resistors. what kind of value does it return and what not.

what im asking is, what would compare to what? my middle photo cell will be hovering over the tape, and i would want the outside 2 to be on ether side of the tape? or would i want all 3 to be on the tape and if left or right is not = to middle the i turn in on the appropriate direction? witch would be more effective?

also, one last question....about how far should the photo resistors b\be from the ground? and depending on witch methods above would return the most accurate line following, about how far apart would they be?

ps. you have replied to alot of my topis and i want to clareafy....all my questions are mostly hardware setup and design help....if i get over that hurdle, the programing is easy for me 8-)

As I mentioned, the first step is to learn how to read one sensor, and print (to the serial monitor) it's value.

int leftVal = analogRead(leftPin);
Serial.print("Left: ");

Next step is to replicate this code twice, and change left to center and right.

When you get all three values being printed, try moving the sensors over different color materials.

You should be able to see differences between the three values as you move the sensors back and forth over a line.

The differences in the values, not the absolute values, are what tell you when to turn left or right.