Line following Car that can automatically stop

#define LT_R !digitalRead(10)
#define LT_M !digitalRead(4)
#define LT_L !digitalRead(2)

#define ENA 5
#define ENB 6
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 11

#define carSpeed 150

void forward(){                //car drives forward
  analogWrite(ENA, carSpeed);
  analogWrite(ENB, carSpeed);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  Serial.println("vorwärts");
}

void back(){                   //car drives backwards, dont have a use yet.
  analogWrite(ENA, carSpeed);  //But i thought about it searching for the line by driving backwards/forwards for a short moment
  analogWrite(ENB, carSpeed);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  Serial.println("rückwärts");
}

void left(){                   //car drives left
  analogWrite(ENA, carSpeed);
  analogWrite(ENB, carSpeed);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  Serial.println("left");
}

void right(){                   //car drives right
  analogWrite(ENA, carSpeed);
  analogWrite(ENB, carSpeed);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  Serial.println("right");
}

void brake(){              //car stopping
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);
  Serial.println("stop");
}  
void setup(){              //setup for the line detection
  pinMode(LT_R,INPUT);
  pinMode(LT_M,INPUT);
  pinMode(LT_L,INPUT);
}

void loop() {       //Loop that makes the car follow the line
  if(LT_M){
  forward();
  }
  else if(LT_L){    //car drives left 
    left();
    while (LT_L);
   }
  else if (LT_R){    //car drives right
    right();
    while(LT_R);
  }
  else {             //car brakes if it cant detect the line,
    brake();         //currently it stops if it doesnt detect it for even the slightest moment
 }					 //This makes the car unable to follow the line properly
}					 //i want it to stop only if it doesnt detect it for longer than lets say 0.5 - 1 second
 

Alright this is the Code, its purpose is to make the car follow a black line using 3 sensors at the front. It works decently following the Line but now i wan't it to stop once it doesnt detect it.
I used a simple else at the end that makes the car stop once it doesn't detect the line but this work too well as the car stop even at the slightest.

I have thought about potetntially adding a timer that runs down once it doesnt detect the line and then stops the car. However i am pretty new to programming and my attempts failed wich involved using integers (int i = 0; i >=500; i++) but that didnt work for me.

At one point i had it happen where the car just spins to the right once it goes off-track but im an idiot and accidentally deleted that part..

i would appreciate any help :slight_smile:

You need to keep track of how many times in a row you land in the 'else` part of your code and then, if this gets large enough, brake.

Some thing like this:

unsigned int brake_count = 0;
const unsigned int brake_count_limit = 1000;  // some number you need to figure out
void loop() {       //Loop that makes the car follow the line
  if(LT_M){
    forward();
    brake_count = 0; // line found
  }
  else if(LT_L){    //car drives left 
    left();
    brake_count = 0; // line found
    while (LT_L);
   }
  else if (LT_R){    //car drives right
    right();
    brake_count = 0; // line found
    while(LT_R);
  }
  else {             //car brakes if it cant detect the line,
    brake_count++;
  if ( brake_count >= brake_count_limit ) {
    brake(); 
    while(1); // loop forever
  }
 }					 //This makes the car unable to follow the line properly
}					 //i want it to stop only if it doesnt detect it for longer than lets say 0.5 - 1 second
1 Like

Wow thank you, i‘ll try this as soon as i can. Unfortunately earliest i can test it is tuesday as the arduino cars are at school and i do not own one haha.

I‘ll update this thread if it works!

instead of reversing a motor to change direction, what about slowing one motor down?

enum { DirFor = LOW, DirRev = HIGH };

void move (
    int   speedLeft,
    int   speedRight,,
    int   direction )
{
    Serial.println ("vorwärts");

    digitalWrite (IN1, ! direction);
    digitalWrite (IN2,   direction);
    digitalWrite (IN3,   direction);
    digitalWrite (IN4, ! direction);

    analogWrite (ENA, speedLeft);
    analogWrite (ENB, speedRight);
}

and shouldn't you monitor the outside sensors to keep moving forward (not sure what if sensor is HIGH/LOW when on the line)

void loop () {
    if (! LT_L && ! LT_R)
        move (SpdNorm, SpdNorm, DirFor);

no need for a while loop to turn

    else if (LT_L)
        move (SpdNorm, SpdLow, DirFor);

but may want to undo any turn for probably half the time a correction was made

may want to turn harder if the middle sensor is also off the line

    else if (LT_L && LT_M )
        move (SpdNorm, SpdVeryLow, DirFor);
    else if (LT_L)
        move (SpdNorm, SpdLow, DirFor);

no need for brake()

        move (0, 0, DirFor);

@blh64 seems simple enough

Expand the macro.

How does that work?
What about the outputs?
Edit: talk to this guy - their code looks a lot like yours

Edit to edit: re-reading this topic, it looks like you're having an identity crisis.
Thoughts and prayers.

So about expanding the macro,

LT_L/R/M are just three infrared sensors that detect the line, or rather dark and bright. There is no output for those so its only as an input.

Now, expand the macro with the pinMode.

what does digitalRead() return and what pin # is being passed to pinMode()? (shouldn't it be 10, 4, or 2)?

not sure any of the comments are of any value if the code doesn't work

as far as i understand what happens here:

It creates the constant values LT_L/M/R, these get a value from the pins 10,4,2 (the 3 infrared sensors under the car).

and with pinMode it just uses these pins values through the previously defined LT_L/M/R

the code works as it is and the car follows the line, im just trying to figure out how to make it stop without it stopping suddenly in a curve.

can you elaborate on what you mean by expanding the macro? im pretty new so i only got the basic basics down haha

the #define describes text that is replaced everywhere in the code with the defined value. in other words

  pinMode (!digitalRead(10);
  pinMode (!digitalRead(4));
  pinMode (!digitalRead(2));

so is pinMode(digitalRead(10), the same as pinMode(LT_R, or is it different? and if so how different is it?

it's pinMode (! digitalRead (10)); that the define is

it's exactly the same.
don't know why you think LT_R is somehow just the pin# when used in pinMode()

What about all those pins you're using a digitalWrite on? (IN1 to IN4)

those are the pins for the servo motors, ENA and ENB control the speed of the left and right side and IN1-4 are the 4 motors that spin the wheel

But you want them to be outputs, right?
You don't just want to turn the pullup resistors on an input on and off.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.