Help Me With Line Finder Robot

hello everyone I have made a line follower robot using arduino onu with QTR-8 sensor, everything is okay so far it following the line perfectly,

but i want to add an operation that is the robot If got away from the line how can it search and get back to the line again automatically ?

Iv’e been searching for the code almost 1 month but i couldn’t find or write it plz help me with the code

and this is my current code

#include <AFMotor.h>
#include <QTRSensors.h>

AF_DCMotor motor1(1, MOTOR12_1KHZ ); //create motor #1 using M1 output on Motor Drive Shield, set to 1kHz PWM frequency
AF_DCMotor motor2(2, MOTOR12_1KHZ ); //create motor #2 using M2 output on Motor Drive Shield, set to 1kHz PWM frequency

#define KP 2 //experiment to determine this, start by something small that just makes your bot follow the line at a slow speed
#define KD 5 //experiment to determine this, slowly increase the speeds and adjust this value. ( Note: Kp < Kd)
#define M1_minumum_speed 150 //minimum speed of the Motor1
#define M2_minumum_speed 150 //minimum speed of the Motor2
#define M1_maksimum_speed 250 //max. speed of the Motor1
#define M2_maksimum_speed 250 //max. speed of the Motor2
#define MIDDLE_SENSOR 4 //number of middle sensor used
#define NUM_SENSORS 5 //number of sensors used
#define TIMEOUT 2500 //waits for 2500 us for sensor outputs to go low
#define EMITTER_PIN 2 //emitterPin is the Arduino digital pin that controls whether the IR LEDs are on or off. Emitter is controlled by digital pin 2
#define DEBUG 0

//sensors 0 through 5 are connected to analog inputs 0 through 5, respectively
QTRSensorsRC qtrrc((unsigned char) { A4,A3,A2,A1,A0} ,NUM_SENSORS, TIMEOUT, EMITTER_PIN);

unsigned int sensorValues[NUM_SENSORS];

void setup()
{
delay(1500);
manual_calibration();
set_motors(0,0);
}

int lastError = 0;
int last_proportional = 0;
int integral = 0;

void loop()
{
unsigned int sensors[5];
int position = qtrrc.readLine(sensors); //get calibrated readings along with the line position, refer to the QTR Sensors Arduino Library for more details on line position.
int error = position - 2000;

int motorSpeed = KP * error + KD * (error - lastError);
lastError = error;

int leftMotorSpeed = M1_minumum_speed + motorSpeed;
int rightMotorSpeed = M2_minumum_speed - motorSpeed;

// set motor speeds using the two motor speed variables above
set_motors(leftMotorSpeed, rightMotorSpeed);
}

void set_motors(int motor1speed, int motor2speed)
{
if (motor1speed > M1_maksimum_speed ) motor1speed = M1_maksimum_speed;
if (motor2speed > M2_maksimum_speed ) motor2speed = M2_maksimum_speed;
if (motor1speed < 0) motor1speed = 0;
if (motor2speed < 0) motor2speed = 0;
motor1.setSpeed(motor1speed);
motor2.setSpeed(motor2speed);
motor1.run(FORWARD);
motor2.run(FORWARD);
}

//calibrate for sometime by sliding the sensors across the line, or you may use auto-calibration instead
void manual_calibration() {

int i;
for (i = 0; i < 250; i++)
{
qtrrc.calibrate(QTR_EMITTERS_ON);
delay(20);
}

if (DEBUG) {
Serial.begin(9600);
for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtrrc.calibratedMinimumOn*);*
Serial.print(’ ');
}
Serial.println();

for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print(qtrrc.calibratedMaximumOn*);*
Serial.print(’ ');
}
Serial.println();
Serial.println();
}
}

STOP CROSS-POSTING!

Duplicates deleted.

Remember to use code tags when posting code.

ashton87:
but i want to add an operation that is the robot If got away from the line how can it search and get back to the line again automatically ?

Leaving aside the question of how you might write a program finding a line not a simple thing. You could make the robot move in some direction hoping that it will run across a line. But what direction would you choose and when would you decide that the direction was never going to cross a line? And if you did decide that you were searching in the wrong place, how would you decide where to search next? There is also the problem of making the robot move in a straight line if it does not have a line to follow.

This is the sort of thing that is easy for a human with a sophisticated vision system, but could be incredibly difficult for a robot with a few line-detector sensors.

I have not looked at your program. To make it easier for people to help you please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum Your code is too long for me to study quickly without copying to a text editor.

…R

Everything they said, PLUS, hit CTRL-T when you are in the IDE. This auto formats the code making it much easier to read, much easier to find mismatched brackets.

For posting this question FOUR TIMES, @ashton87 is enjoying a brief forum timeout, to give them a chance to read the forum posting guidelines.