i need some help track line

i have problem in track line
the car only go not drive in black line(i think the sensot not work or…)
watch the Attachments have some pictures

iam try obstacle avoidance with this is code very good:
#include <Servo.h> //servo library
Servo myservo; // create servo object to control servo
int Echo = A4;
int Trig = A5;
int in1 = 6;
int in2 = 7;
int in3 = 8;
int in4 = 9;
int ENA = 5;
int ENB = 11;
int ABS = 110;
int rightDistance = 0,leftDistance = 0,middleDistance = 0 ;
void _mForward()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.println(“go forward!”);
}

void _mBack()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.println(“go back!”);
}

void _mleft()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.println(“go left!”);
}

void _mright()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.println(“go right!”);
}
void _mStop()
{
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);
Serial.println(“Stop!”);
}
/Ultrasonic distance measurement Sub function/
int Distance_test()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(20);
digitalWrite(Trig, LOW);
float Fdistance = pulseIn(Echo, HIGH);
Fdistance= Fdistance/58;
return (int)Fdistance;
}

void setup()
{
myservo.attach(3);// attach servo on pin 3 to servo object
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
_mStop();
}

void loop()
{
myservo.write(90);//setservo position according to scaled value
delay(500);
middleDistance = Distance_test();
#ifdef send
Serial.print(“middleDistance=”);
Serial.println(middleDistance);
#endif

if(middleDistance<=20)
{
_mStop();
delay(500);
myservo.write(5);
delay(1000);
rightDistance = Distance_test();

#ifdef send
Serial.print(“rightDistance=”);
Serial.println(rightDistance);
#endif

delay(500);
myservo.write(90);
delay(1000);
myservo.write(180);
delay(1000);
leftDistance = Distance_test();

#ifdef send
Serial.print(“leftDistance=”);
Serial.println(leftDistance);
#endif

delay(500);
myservo.write(90);
delay(1000);
if(rightDistance>leftDistance)
{
_mright();
delay(180);
}
else if(rightDistance<leftDistance)
{
_mleft();
delay(180);
}
else if((rightDistance<=20)||(leftDistance<=20))
{
_mBack();
delay(180);
}
else
{
_mForward();
}
}
else
_mForward();
}

gtrg.png

4444.png

A line following robot does NOT use delay(). Imagine trying to follow a curve if you only open your eyes once every half second, or less often, while blasting down the highway at 100 kph.

and this cable the three sensor track line watch the picture

and this code iam use it in track line but not work:
int in1 = 6;
int in2 = 7;
int in3 = 8;
int in4 = 9;
int ENA = 5;
int ENB = 11;
int ABS=120;
void _mForward()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.println(“go forward!”);
}

void _mBack()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.println(“go back!”);
}

void _mleft()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.println(“go left!”);
}

void _mright()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.println(“go right!”);
}
void _mStop()
{
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);
Serial.println(“Stop!”);
}

void setup()
{
Serial.begin(9600);
}

void loop() {
int num1,num2,num3;
num1=digitalRead(13);
num2=digitalRead(4);
num3=digitalRead(2);
if((num1==0)&&num2&&num3)
{
_mleft(); //The sensor detected that right car turn left immediately when it meets black line
delay(2);
while(1){
num2=digitalRead(2); //Cycle to judge degree of intermediate sensor,
if(num2==1)
{ _mleft(); //If num2==1 does not go to the middle position, continue to turn left.
delay(2);}
else
break; //Detection of num2==0 instructions turned over, out of the loop, detection of three sensors’ statusand then make appropriate action
} //The following and so on
}

else if(num2&&num1&&(num3==0))
{
_mright();
delay(2);
while(1)
{
num2=digitalRead(2);
if(num2==1){
_mright();
delay(2);}
else
break;
}
}
else
{
_mForward();
delay(2);
}
}

so pls any person can send to me the correct code

thanks

What do your debug prints tell you?

Where are the code tags?

AWOL:
What do your debug prints tell you?

Where are the code tags?

what you mean?

note: iam not expert only read some cataloge and do it :slight_smile:

I mean, you've got a load of Serial.print statements in your sketch to help you debug.
What do they tell you?

Why don't you use code tags?

you mean this file?

Line tracking car.pdf (326 KB)