Problem with the line follower

Hi guys,
I have a problem with the edited code below. I have 5 line tracking sensors on my robot and two servos.
The problem is that the code dont work properly and my robot is turns around all the time. I don’t know what is wrong, maybe the whole code? :~

/*
Credits to Aklem
*/

#include <Servo.h>

Servo leftServo; //creats left servo object
Servo rightServo;
int s1Left = 2, s2Left = 4, s3Center = 7, s4Right = 8, s5Right = 11;
int lastline = -5;
int lineplace;
int line;

void setup()
{
	leftServo.attach(9);
	rightServo.attach(10); 
	Serial.begin(9600);
	/*Serial.printIn("HALLOOOO, TEST TEST");*/
}
void stopmoving()
{
	leftServo.write(90);
	rightServo.write(95);  
}
void spinleft()
{
	leftServo.write(0);
	rightServo.write(0);
}
void spinright()
{
	leftServo.write(180);
	rightServo.write(180);
}
void arcleft()
{
	leftServo.write(115);
	rightServo.write(0);
}
void arcright()
{
    leftServo.write(180);
    rightServo.write(75);
}
void moveleft()
{
    leftServo.write(110);
    rightServo.write(0);
}
void moveright()
{
    leftServo.write(180);
    rightServo.write(80);
}
void sharpleft()
{
	leftServo.write(102);
	rightServo.write(0);
}
void sharpright()
{
    leftServo.write(180);
    rightServo.write(87);
}
void moveforward()
{
    leftServo.write(180);
    rightServo.write(0);
}
void movebackward()
{
    leftServo.write(0);
    rightServo.write(180);
}

int getline(){
if (s1Left == 1)
{
  lineplace = -4;
}
if (s1Left == 1 && s2Left == 1)
{
  lineplace = -3;
}
if (s2Left == 1 && s1Left == 0)
{
  lineplace = -2;
}
if (s2Left == 1 && s3Center == 1)
{
  lineplace = -1;
}
if (s3Center == 1 && s2Left == 0 && s1Left == 0 && s4Right == 0 && s5Right == 0)
{
  lineplace = 0;
}
if (s3Center == 1 && s4Right == 1) 
{
  lineplace = 1;
}
if (s4Right == 1 && s3Center == 0)
{
  lineplace = 2;
}
if (s4Right == 1 && s5Right == 1)
{
  lineplace = 3;
}
if (s5Right == 1 && s4Right == 0)
{
  lineplace = 4;
}
if (s1Left == 0 && s2Left == 0 && s3Center == 0 && s4Right == 0 && s5Right == 0)
{
 lineplace = -10;
}

if (s1Left == 0 && s2Left == 0 && s3Center == 1 && s4Right == 0 && s5Right == 0)
{
 lineplace = 10;
}
return lineplace;
}


void loop()
{
	/*Serial.printIn("Start van testen");*/
	getSensorData();
	showSensorData();
	delay(1000);
	line = getline();
while (line == -10)
{
 line = getline();
 Serial.print("No line detected (spin to where it was last found):");
 Serial.println(line, DEC);
 if (lastline == -4)
 {
  spinleft(); 
  Serial.println("spinning left ");
 }
  if (lastline == 4)
 {
  spinright(); 
  Serial.println("spinning right ");
 }
 }

while (line == 10)
{
 
 stopmoving();
 line = getline();
 Serial.print("END (or picked up)(stop)");
 Serial.println(line, DEC);
 
 }

while (line == -4 || line == -3)
{
 sharpleft();
 line = getline();
 Serial.print("sharp left: ");
 Serial.println(line, DEC);
 lastline = -5;
 }

while (line == -3 || line == -2)
{
 moveleft();
 line = getline();
 Serial.print("move left: ");
 Serial.println(line, DEC);
 }

while (line == -1)
{
 arcleft();
 line = getline();
 Serial.print("arc left: ");
 Serial.println(line, DEC);
 }

while (line == 0)
{
 moveforward();
 line = getline();
 Serial.print("move forward: ");
 Serial.println(line, DEC);
 }

while (line == 1)
{
 arcright();
 line = getline();
 Serial.print("arc right: ");
 Serial.println(line, DEC);
 }

while (line == 2 || line == 3)
{
 moveright();
 line = getline();
 Serial.print("move right: ");
 Serial.println(line, DEC);
 }
 
 while (line == 4 )
{
 sharpright();
 line = getline();
 Serial.print("sharp right: ");
 Serial.println(line, DEC);
 lastline = 5;
 }



stopmoving();
Serial.println(line, DEC); 
}	

void getSensorData()
{
	s1Left = digitalRead(s1Left);
	s2Left = digitalRead(s2Left);
	s3Center = digitalRead(s3Center);
	s4Right = digitalRead(s4Right);
	s5Right = digitalRead(s5Right);
}
void showSensorData()
{
if (s1Left == 0){Serial.print("L1 ");} else {Serial.print ("L1 NOT");}
	if (s2Left == 0) {Serial.print("L2 ");} else {Serial.print ("L2 NOT");}
	if (s3Center == 1) {Serial.print("C1 ");} else {Serial.print ("C1 NOT");}
	if (s4Right == 0) {Serial.print("R1 ");} else {Serial.print ("R1 NOT");}
	if (s5Right== 0) {Serial.print("R2 ");} else {Serial.print ("R2 NOT");}
}


/* TRALALA */

What does the output from your Serial.print statements look like ?

It's says "spinning left" when I change the variables, then it's says "Spinning Right" then " sharp right" over and over again...

Some comments in your code would help to understand what you expect to happen rather than what is happening.

Can you please comment all of the lines like

if (s1Left == 1 && s2Left == 1)

to indicate what such a case means and what should happen ? Does it matter that after each test of values the routine does not stop testing and carries on testing each of the other combinations ? What if two tests are true ?

I assume that you have tested the 5 line sensors, that they all work and produce the values that you are testing for.

if (s1Left == 1 && s2Left == 1)

when the left sensors 1 & 2 are detecting the black line then the car must move a little bit to the left, but that's not working.

Does it matter that after each test of values the routine does not stop testing and carries on testing each of the other combinations ?

It must be testing whole the time, because my robot moves whole to time ..

What if two tests are true ?

Two test can't be true because the several sensors can't detect the black line at the same time.

Yes i has tested my 5 sensors and they work properly.

int s1Left = 2, s2Left = 4, s3Center = 7, s4Right = 8, s5Right = 11;

that's my sensor and the connections on the arduino uno

Can you please explain how the sensors are arranged ? Are they in a straight line across the vehicle ?

I assume that the servos that you are using are free rotating ones and that you are using them to drive the vehicle.

When the 2 left sensors detect the line at the same time (is that possible ?) you call the spinleft function. What do these 2 lines do ?

	leftServo.write(0);
	rightServo.write(0);

Do they move the servos to position 0 ?
You are not looking for the line whilst spinning the vehicle so what happens if the spinLeft function causes the line to move outside the line of sensors, assuming that is how they are arranged ?

Can you please explain how the sensors are arranged ? Are they in a straight line across the vehicle ?

The sensors are at the front of the vehicle, in a straight line.

I assume that the servos that you are using are free rotating ones and that you are using them to drive the vehicle.

The servos are continuous rotation(360°), and i'm using them to drive the vehicle.

You are not looking for the line whilst spinning the vehicle so what happens if the spinLeft function causes the line to move outside the line of sensors, assuming that is how they are arranged ?

The vehicle must then search for the line by turning around.

That's my fault, the 2 left sensors can't detect the line at the same time.

Here are some pics:


Those sensors look a long way apart. How wide is the line you're trying to follow, compared to that sensor spacing?

It's a black tape of 1cm

That doesn't really answer my question. I wanted to know how that related to the sensor spacing. Let me try asking another way. If you move the array of sensors over the tape, does the tape appear on multiple sensors, or does it ever disappear from all sensors, as it crosses the gap between sensors?

The gap between the sensors can I change, multiple sensors can detect the black line but I want that only the "s3center" detect always the black line and the other sensors may not detect the line then the vehicle must drive forward, when the others sensors detect the line then must the vehicle spin left of right depends what sensor.

You are still not answering the question.

Place the vehicle over the line so that the middle sensor is in the middle of the line. Make a note of which sensors are reporting 'dark' and 'light'.

Slowly move the vehicle sideways until all the sensors are clear of the line. Each time the sensor readings change during this process, make a note of the new readings.

Now describe what readings you got.

Is it this?

LIGHT LIGHT DARK LIGHT LIGHT
LIGHT DARK LIGHT LIGHT LIGHT
DARK LIGHT LIGHT LIGHT LIGHT
LIGHT LIGHT LIGHT LIGHT LIGHT

Probably it isn't, because it's unlikely that you have got all the sensors aligned exactly so that the line is detected by one sensor at the precise instant it stops being detected by the previous sensor.

If there is overlap between the sensors you might get this:

LIGHT LIGHT DARK LIGHT LIGHT
LIGHT DARK DARK LIGHT LIGHT
LIGHT DARK LIGHT LIGHT LIGHT
DARK DARK LIGHT LIGHT LIGHT
DARK LIGHT LIGHT LIGHT LIGHT
LIGHT LIGHT LIGHT LIGHT LIGHT

If there is underlap, you might get this:

LIGHT LIGHT DARK LIGHT LIGHT
LIGHT LIGHT LIGHT LIGHT LIGHT
LIGHT DARK LIGHT LIGHT LIGHT
LIGHT LIGHT LIGHT LIGHT LIGHT
DARK LIGHT LIGHT LIGHT LIGHT
LIGHT LIGHT LIGHT LIGHT LIGHT

Only you can see what you actually get, but it's vital to know this in order to design the algorithm that decides where the line is based on the sensor readings.

I have something like this:

L1	L2	cent.	R2	R1
light	light	dark	light	light
light	light	light	dark	light
light	light	light	dark	dark
light	light	light	light	dark
light	dark	light	light	light
dark	dark	light	light	light

when I have note "dark" it's mean that the sensor detect the line.

I began with the middle sensor.

So it looks as if there’s some overlap between the outer two sensors on each side, but no overlap with the central one?

PeterH:
So it looks as if there’s some overlap between the outer two sensors on each side, but no overlap with the central one?

yes, the central one don’t overlap with any other sensor…

In getline(), replace all the statements like if (s1Left == 1) with if (digitalRead(s1Left) == HIGH) so that your logic is based on the sensor input values rather than the pin numbers. I haven't checked through it in detail but the basic approach of looking for combinations of sensors being high/low looks credible, as long as you are only dealing with the simple case of a line at right angles to the array of sensors.

Having made that change I suggest you comment out or disable all the code to do with motors, add code to print out lineplace whenever it changes, and move your 'bot by hand over a sample line and confirm that it outputs the correct values for lineplace at the correct times.