HELP ( program for line folower withs only one captor. not sure of this program)

hello,
I'm new with arduino and I have to program a robot folower of line with only one captor. I program in a arduino uno. The robot is the rover five. The captor give 1 for a white line and 0 for a black line.
I can't test it yet and I want to no if it's can work with this program.
sorry for my english.

PS: "suiveurligne" is for the line folower
It's normal that the fonction capteur() is not done already already, it's will be a check action with PIR captor an servo to move it.

I put the code under because I don't know how to put it in a window

int E1 = 5;
int M1 = 4;
int E2 = 6;
int M2 = 7;
boolean sl = 2;
long temps;
long controlstop;

void setup()
{
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(sl, INPUT);

}

void avancer()
{ digitalWrite(M1,HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, 255); //PWM Speed Control
analogWrite(E2, 255); //PWM Speed Control

}

void reculer()
{ digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 255); //PWM Speed Control
analogWrite(E2, 255); //PWM Speed Control
}
void tdroit()
{

digitalWrite(M1,HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, 0); //PWM Speed Control
analogWrite(E2, 255); //PWM Speed Control
}
void tgauche()

{

digitalWrite(M1,HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, 255); //PWM Speed Control
analogWrite(E2, 0); //PWM Speed Control
}
void rgauche()
{ digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 255); //PWM Speed Control
analogWrite(E2, 0); //PWM Speed Control
}
void rdroit()
{ digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 0); //PWM Speed Control
analogWrite(E2, 255); //PWM Speed Control
}
void arret()
{
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 0); //PWM Speed Control
analogWrite(E2, 0); //PWM Speed Control
}
void capteur()
{

}
void loop() {
controlstop=millis();
while(digitalRead(sl) == 0)
{

avancer();
if (millis()-controlstop>=10000)
{
capteur();
controlstop = millis();
}
}
while(digitalRead(sl) == 1)
{
temps = millis();
while(digitalRead(sl)==1)
if(millis()-temps<1000)
{
tgauche();
}
if(millis()-temps>=1000 &&(millis()-temps<2000))
{

rgauche();
}
if(millis()-temps>=2000 &&(millis()-temps<3000))
{
tdroit();
}
if(millis()-temps>=3000 &&(millis()-temps<4000))
{
rdroit();
}
if(millis()-temps>=4000 &&(millis()-temps<5000))
{
reculer();
}

if(millis()-temps>=5000 &&(millis()-temps<6000))
{
tgauche();
}
if(millis()-temps>=6000 &&(millis()-temps<7000))
{

rgauche();
}
if(millis()-temps>=7000 &&(millis()-temps<8000))
{
tdroit();
}
if(millis()-temps>=8000 &&(millis()-temps<9000))
{
rdroit();
}
if(millis()-temps>=9000)
{
while(1){}
}
}
}

code_suiveurligne_minicop.ino (2.51 KB)

Most of us will not know what a "rover five" is so a link to it or more details might help. I can't tell if the code will work without knowing what motor driver you are using.

One thing I notice is that a boolean takes values of true or false. Putting the value 2 into one is bad. Perhaps it should have been a byte instead?

    if(millis()-temps>=9000)
    {
       while(1){}
    }

and then I wonder if you really mean this.

What sensor (English for capteur) are you going to use?

Steve

Help me out.
You are trying to follow a line using one sensor? The only way I can see that working is if you sweep the sensor back and forth so that you can tell if the line is moving to the left or the right as your vehicle moves.

thank you for your answers

I had the opportunity to test the program the program and it worked.(with some modification) and we didn't had the time to try with a real line so we don't no if it could folow the line with this program.

First for the boolean value, I have renplaced it by int and it's worked.

The rover 5 is this Rover 5 Robot Platform - ROB-10336 - SparkFun Electronics
and the motor driver is this 2x2A DC Motor Shield For Arduino - DFRobot

and this part of code:

if(millis()-temps>=9000)
{
while(1){}
}

is to put the arduino in a loop if he don't find the line after the different movement he did to search the line.

VINCE HERMAN

we try to follow the line with a serie of movement to do to check around the robot where is the line

to facilitate the comprehension, this is the translation of the different fonction to move:

avancer() : move forward
reculer(): move backward
tgauche(): turn on the left
tdroit(): turn on right
rgauche(): turn left backward
rdroit():turn right backward
arret(): stop

 if(millis()-temps>=9000)
    {
       while(1){}
    }

Your program will stuck in an endless loop here.

it's will be a check action with PIR captor an servo to move it.

Then include a servo, which moves the PIR sensor! This is exactly what vinceherman meant in answer #2.