Line Follower, Gate Passing

Hi guys. I’m working on an Arduino Car project that can follow line and detect obstacles. My car can follow the line, there is no problem up to here. But I have some problem about detect obstacle. Because the obstacles are always moving periodically up and down, non stop. Here is my codes;

#define SensorSol 4 //left sensor

#define echoPin 12

#define trigPin 11

#define SensorSag 2 //right sensor

long sure, uzaklik; //duration, distance

#define MotorR1 9

#define MotorR2 8

#define MotorRE 10

#define MotorL1 7

#define MotorL2 6

#define MotorLE 5

void setup() {

pinMode(echoPin, INPUT);

pinMode(trigPin, OUTPUT);

pinMode(SensorSol, INPUT);

pinMode(SensorSag, INPUT);

pinMode(MotorR1, OUTPUT);

pinMode(MotorR2, OUTPUT);

pinMode(MotorRE, OUTPUT);

pinMode(MotorL1, OUTPUT);

pinMode(MotorL2, OUTPUT);

pinMode(MotorLE, OUTPUT);

Serial.begin(9600);

}

void loop() {

digitalWrite(trigPin, LOW);

delayMicroseconds(5);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

sure = pulseIn(echoPin, HIGH);

uzaklik = (sure / 2) / 29.1 ;

Serial.print("uzaklik: ");

Serial.println(uzaklik);

if (uzaklik > 5 ) {

if(digitalRead(SensorSol) == 0 && digitalRead(SensorSag) == 0){

ileri();

}

if(digitalRead(SensorSol) == 0 && digitalRead(SensorSag) == 1){

sag();

}

if(digitalRead(SensorSol) == 0 && digitalRead(SensorSag) == 0){

ileri();

}

if(digitalRead(SensorSol) == 1 && digitalRead(SensorSag) == 0){

sol();

}

if(digitalRead(SensorSol) == 0 && digitalRead(SensorSag) == 0){

ileri();

}

}

if (uzaklik < 5){

dur();

delay(500);

//HERE IS THE PROBLEM??? HOW CAN I DO??

//check the distance (the moment when the distance>5 suddenly);

//void pass;

//delay(1000);

//void stop

//delay(500);

//turn back void move forward;

}

}

void ileri(){ // move forward

digitalWrite(MotorR1, HIGH);

digitalWrite(MotorR2, LOW);

analogWrite(MotorRE, 60);

digitalWrite(MotorL1, HIGH);

digitalWrite(MotorL2, LOW);

analogWrite(MotorLE, 80);

}

void sag(){ //right

digitalWrite(MotorR1, HIGH);

digitalWrite(MotorR2, LOW);

analogWrite(MotorRE, 0);

digitalWrite(MotorL1, HIGH);

digitalWrite(MotorL2, LOW);

analogWrite(MotorLE, 80);

}

void sol(){ //left

digitalWrite(MotorR1, HIGH);

digitalWrite(MotorR2, LOW);

analogWrite(MotorRE, 80);

digitalWrite(MotorL1, HIGH);

digitalWrite(MotorL2, LOW);

analogWrite(MotorLE, 0);

}

void dur(){ // stop

digitalWrite(MotorR1, HIGH);

digitalWrite(MotorR2, LOW);

analogWrite(MotorRE, 0);

digitalWrite(MotorL1, HIGH);

digitalWrite(MotorL2, LOW);

analogWrite(MotorLE, 0);

}

void gec(){ //pass obstacle

digitalWrite(MotorR1, HIGH);

digitalWrite(MotorR2, LOW);

analogWrite(MotorRE, 250);

digitalWrite(MotorL1, HIGH);

digitalWrite(MotorL2, LOW);

analogWrite(MotorLE, 250);

}

Or can I do this with MZ80? Which one is better, Mz80 or HCSR04 to pass the moving gates?
Because my car must pass the gate at the first moment when there is no obstacle

Here is the video about my project.

HCSR04 would be a better fit than Mz80 since you cannot get precise distance with the latter. It only gives an on-off output.

You also mentioned a 2 cm distance, which the Mz80 cannot read, since it can only read as low as 3 cm

1 Like

I’m using both sensors. Hcsr04 and Mz80. With Hcsr08 im trying to measure the distance and with the Mz80, im trying to count the on/off gate. If Mz80 count 3 times, then car will pass the gate.
if(distance<5){
for (counter=0; counter<3; counter++){
stop();}
for(counter =0; counter >=3; counter++){
pass();}
}
I wrote a code like this but someone says ''you must use ‘while’ ‘’
Like this;

int counter=0;

do{

if(counter==5) 
 break;

if (IsThereObstacle())
   counter++;

 delay(200);

}while(true);
Pass();