Line+Follower

Detailed information on the line tracker sensors can be found here
Arduino Library for the Pololu QTR Reflectance Sensors

July 23, 2013 tested code: LineTracker4

Code (from the Internet):
/* Line follower robot code

This code will make your robot follow a black line over a white background. Use a Pololu Infra Red array and two continuous servos to make your robot. For schematics and more info refer to here :

This code is written by Farshid Jafari Harandi, (C) Spekel, Spekel.se - No commercial use allowed. For more info, email : harandi@spekel.se




 * 1) include  //use the servo library to control the servo motors//

//const int ledPin = 13;// LED connected to digital pin 13 const int sensorPin1 = 2; //line sensor number 1// //const int sensorPin2 = 3;// line sensor number 2 const int sensorPin3 = 4; //line sensor number 3// //const int sensorPin4 = 5;// line sensor number 4 const int sensorPin5 = 6; //line sensor number 5// //const int sensorPin6 = 7;// line sensor number 6 const int sensorPin7 = 8; //line sensor number 7// //const int sensorPin8 = 9;// line sensor number 8 const int Lmotor = 10; //Left servo motor// //const int Rmotor = 11;// Right servo motor const int cLED1 = 12; //Custom LED number 1// //const int cLED2 = 13;// Custom LED number 2 const int idle = 90; //servo motor will stop at this speed// //int i;// //int sensor1,sensor2,sensor3,sensor4,sensor5,sensor6,sensor7,sensor8;// variables to hold the status of sensors Servo Lservo; //Left servo motor// //Servo Rservo;// Right servo motor int linePosition;

//The setup method runs once, when the sketch starts//

//void setup// //{// //pinMode(sensor1, INPUT);// initialize the digital pin as an input; pinMode(sensor2, INPUT); //initialize the digital pin as an input;// //pinMode(sensor3, INPUT);// initialize the digital pin as an input; pinMode(sensor4, INPUT); //initialize the digital pin as an input;// //pinMode(sensor5, INPUT);// initialize the digital pin as an input; pinMode(sensor6, INPUT); //initialize the digital pin as an input;// //pinMode(sensor7, INPUT);// initialize the digital pin as an input; pinMode(sensor8, INPUT); //initialize the digital pin as an input;//

//pinMode(Lmotor, OUTPUT);// initialize the digital pin as an output; pinMode(Rmotor, OUTPUT); //initialize the digital pin as an output;// //pinMode(cLED1, OUTPUT);// initialize the digital pin as an output; pinMode(cLED2, OUTPUT); //initialize the digital pin as an output;// //Serial.begin(115200);// //readSensors;// //Lservo.attach(Lmotor);// //Rservo.attach(Rmotor);// //Lservo.write(idle);// order motor to stop Rservo.write(idle); //order motor to stop// //}//

the loop method runs over and over again, //as long as the Arduino has power//

//void loop// //{// read the sensors readSensors; //find where the line is// //linePosition = findLinePosition;// //Serial.println(linePosition);// update motors updateMotors(linePosition); delay(100); }

void readSensors { sensor1 = digitalRead(sensorPin1); sensor2 = digitalRead(sensorPin2); sensor3 = digitalRead(sensorPin3); sensor4 = digitalRead(sensorPin4); sensor5 = digitalRead(sensorPin5); sensor6 = digitalRead(sensorPin6); sensor7 = digitalRead(sensorPin7); sensor8 = digitalRead(sensorPin8); Serial.print(sensor1,BIN); Serial.print(sensor2,BIN); Serial.print(sensor3,BIN); Serial.print(sensor4,BIN); Serial.print(sensor5,BIN); Serial.print(sensor6,BIN); Serial.print(sensor7,BIN); Serial.println(sensor8,BIN); }

int findLinePosition { //assign a meaningful value based on where the line is located under the sensor board//

//if (sensor1==0 && sensor2==0 && sensor3==0 && sensor4==0// //&& sensor5==0 && sensor6==0 && sensor7==0 && sensor8==0) return -255;// //if (sensor1 == 1 && sensor2 == 1) return 6;// //if (sensor1 == 1) return 7;// //if (sensor2 == 1 && sensor3 == 1) return 4;// //if (sensor2 == 1) return 5;// //if (sensor3 == 1 && sensor4 == 1) return 2;// //if (sensor3 == 1) return 3;// //if (sensor4 == 1 && sensor5 == 1) return 0;// //if (sensor4 == 1) return 1;// //if (sensor5 == 1 && sensor6 == 1) return -2;// //if (sensor5 == 1) return -1;// //if (sensor6 == 1 && sensor7 == 1) return -4;// //if (sensor6 == 1) return -3;// //if (sensor7 == 1 && sensor8 == 1) return -6;// //if (sensor7 == 1) return -5;// //if (sensor8 == 1) return -7;// //}//

//void updateMotors(int linePosition)// //{//

//int Lspeed,Rspeed;// //if (linePosition == -255)// //{// //Rspeed = idle;// //Lspeed = idle;// //}// //else// //{// //Lspeed = map(linePosition,-7,7,90,180);// Possible errors try: Lspeed = map(linePosition,-7,7,0,90) Rspeed = map(linePosition,-7,7,0,90); // Possible errors try: Rspeed = map(linePosition,-7,7,90,180) } Lservo.write(Lspeed); Rservo.write(Rspeed);

}