Line following robot coding

  • List item

Hi guys. I am very new to the community and had a very little background about coding and electronics. I had decided to order a line following robot’s starter kit and successfully assembled them and got it running along the black line. However, i wanted to put more functions into the robot where it can stop at particular location(horizontal blackline) for a period of time then continues to move forward to another station and repeat the same loop. If success, i am considering building a bigger cart with 2 DC 36V motors that can actually transport goods from one destination to another in a repeated loop sense. Hope you guys can give me some advices on the coding prior to the line following robot kit and if possible, on my ideal project.

Heap thanks

That's a cool idea. What kind of help are you looking for?
Have you looked for those features or ones like them in youtube videos?

I am looking for codes that could be added into the original codes that were provided by the Kit's seller that would allow the robot to stop for...let's say 10 seconds..when it detects the black line(horizontally) with both sides of IR sensors then continue to move forward until it hits another stops.


/-------definning Inputs------/
int LS = 2; // left sensor
int RS = 3; // right sensor

/-------definning Outputs------/
int LM1 = 4; // left motor
int LM2 = 5; // left motor
int RM1 = 6; // right motor
int RM2 = 7; // right motor

/-------PWM ควบคุมความเร็วมอเตอร์-----/
int PWM1 = 10; // PWM left motor
int PWM2 = 11; // PWM right motor
int SPEED = 90; // Speed PWM สามารถปรับความเร็วได้ถึง 255

void setup()
{
pinMode(LS, INPUT);
pinMode(RS, INPUT);
pinMode(LM1, OUTPUT);
pinMode(LM2, OUTPUT);
pinMode(RM1, OUTPUT);
pinMode(RM2, OUTPUT);
pinMode(PWM1, OUTPUT);
pinMode(PWM2, OUTPUT);
}

void loop()
{

if (!(digitalRead(LS)) && !(digitalRead(RS))) // Move Forward
{
analogWrite(PWM1, SPEED);
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
analogWrite(PWM2, SPEED);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}

if (!(digitalRead(LS)) && digitalRead(RS)) // Turn right
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, LOW);
analogWrite(PWM2, SPEED);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}

if (digitalRead(LS) && !(digitalRead(RS))) // turn left
{
analogWrite(PWM1, SPEED);
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, LOW);
}

if (digitalRead(LS) && digitalRead(RS)) // stop
{
digitalWrite(LM1, LOW);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, LOW);
}

}

Right now, it moves forward if both sensors are active. So you can't use that condition to indicate a pause position. You would need additional sensors.

Please edit your post and put the code in code tags. It's explained in the forum guidelines.

Sorry for putting the codes in the wrong place. Could you please suggest what kind of sensor(I assume one additional IR sensor) should I use and what codes should I be adding into the previous sketch?

Load thanks!

Thanks, that is much better. Do you understand the code of the existing sketch? The existing sensors?

1 Like

From my understanding, Left sensor and right sensor both work by defining the reflected black light and non-black light as (0,0),(1,0),(0,1) for each side and 0 and 1 equate to Low and High in the sketch. To stop, both sides of the sensors should be read LS(0,0) and RS(0,0).

That is correct almost. It would be when LS==0 and RS==0. I'm not sure why you're using the (n,n) notation. Each sensor has only one output. So all you have to do is mount another sensor, call it PS for pause? Then, pause when PS==0.

You can execute a 10 second delay with:

delay(10000);

Thanks a lot mate. This helps a lot. Please correct me if I'm wrong. Let say I mounted one additional IR(called PS) sensor on the most right corner of the front bumper, when the robot hits the horizontal black line, the PS sensor would detect the black line and pause with the delay(10000) function and would resume forwarding again....?

That sounds like an accurate description...

Let's think out of the box, too... what if, instead of just stopping when the two existing sensors see white, pause 10 seconds, then run forward for another 1/2 second and resume normal operation. Then, a gap in the line would be enough, you wouldn't need any additional sensors.

Jeez, I just reordered another additional IR sensor after the previous comment..lol. But that is fine. I am trying to digest what you said. So you mean to think it another way, I should find a way to code the sensors to continue running for a period of time(1/2) sec, then, the robot would be stopping at a gap and hence start looking for the black line again????

Your timing is backwards. First you see a gap, then you wait 10 seconds. That's all you really need, but the 1/2 second idea is just to ensure that you continue on the path. Suppose you got lost - then you would sit there forever because you would always see white, and wait another 10 seconds. The 1/2 second unconditional move forward is to get you back on the line.

That keeps the robot "trying" by moving incrementally every 1/2 second until it finds some line.

I have a kind of test question for you:

  1. What happens now, when the robot loses the line (goes off course)? Refer to the existing code.

Keeps going forever until it detects any black line again????

Read the code and try again.

My brain is getting numb trying to comprehend all the possibilities bro. Lol

The possibilities are not numerous:

  1. turn left
  2. turn right
  3. go forward
  4. stop
    The program has in line documentation that says what happens...

Understanding what it does now, is necessary if you want to make changes. Do you have the robot in front of you, to try?

Considering that you are thinking of building a giant version of this, it seems important...

Is it the !exclamationmark we are talking about??
This is the only possible observable sign I could spot on.

To make things less confusing, substitute "not" for "!" in those lines, also "and" for "&&" to help understand, e.g.

if (digitalRead(LS) and not (digitalRead(RS))) // turn left

another cleaner way is:

if ( digitalRead(LS) == HIGH and digitalRead(RS) == LOW ) // turn left
1 Like

Not at the moment, but I will bring the robot back to my place at this weekend :slight_smile: what Arduino are capable of really amaze me for the projects to come!

Aha! Got it. So there it is. If(LS and RS) are not detecting any black lines, just move forward right??!

Seems like I had reached my maximum comments limitation for my first day. Thanks bro. Really appreciate it. Will get back with the correct answer for you. Please stay tuned!