Line follower program. Vex blocks

My team is practicing programming. We saw some teams use a line following program. We’re not sure if the program is monitoring the lines during the whole autonomous portion or just parts of it. We found some tutorials for a sample program online but still are not sure how to write the code so its always “on” while the robot is still doing other tasks?

Thank you

What do you mean by always “on”? That is always detecting lines? Or that the light itself never turns off? I am asking because I am not sure what your overall task is, which would help me help you


“On” as always detecting lines. For instance, in Squared Away could a line follower program always be “on” while still picking up cubes and balls.

Here is a scenario…move forward a certain distance while following a line. I know it’s hard to discuss without the code. (I will have to find the code my kids used). How do I code for example…travel 5 turns and drive straight (Line follow). Everytime I try it’s either I can go forward 5 turns or examine the line but, not move forward. I’m hitting a wall. Use Forever…While…some sort of loop? Hope I’m clear enough without seeing code.

Thank you

I am going to provide an example an explain why keeping it always on is tricky. Which program do you use? VEXCode IQ Blocks? ModKit? Or which one?

You do need to use what I call a forever-while loop, which is basically a while loop checking for the lines inside a forever loop.

No problem, here to help.

You can certainly do this. Multiple threads (tasks) in a program can operate at the same time.
Line following on the IQ field is tough though because you need to have an algorithm to iron out the junctions otherwise you are just going round in a square :joy:

1 Like

You are right, but what you can also do is have two color sensors and have each controlling on wheel; so if one gets out the other one brings you back on the line.

Vex IQ Blocks. Really, I just need something to go forward a certain distance while following the line. Make it a broadcast? I really appreciate any help. I’m trying to keep some of my teams interested while leading into the new game as well as try something new.

Thank you,

There are lots of ways to do it, but either way it goes beyond line following and can sometimes be easier achieved by using the lines to align then gyro to drive straight.

1 Like

You can also do that, that would probably be easier than having to deal with the needing of re-calibrate your color sensors depending on the lighting.

Here, you have two options right now, probably more if you keep researching.

The color sensor way:


Which requires you to find the average between the number that the black line represents and the color that the white line represents. Since the light sensor works by detecting how much light is reflected back. Once you are done with that, the you can start inputting numbers.

In my example I am using a greater than to detect anything is greater than my average or reflects the most light which would be white; otherwise is black.

So these two sensors control each wheel, moving one forward when is white and stopping it when is black. So if one wheel stops, the other sensor is on white, thus moving your robot forward when both sensors are detecting white at once.

On the other hand we have @calvc01 way:


This one requires a drive train and a gyro.

Which is aligning yourself with the line and then as you drive through it, you align yourself every so often to make sure you are still centered, and get to your target.

Hey @calvc01, if I am wrong, please correct me.

WOW. Great! I can totally see how both would work. We went gyroless. But, how do I add a distance in Cristia95’s version?

1 Like

C2. Are you one of the UK Blitz builders? I’ve watched some of your videos for guidance/ideas since Bankshot came out. I think we even exchanged a few emails.

:joy: yes, that was a while ago! I’ll try to write a post tonight on how you could handle this with just the colour sensor and encoders in the motors.

line follower

Here is something I had played around with. I copied the program from when I used Modkit and rewrote it in Blocks.

When, I used Modkit the robot actually turned right when I put in a turn right command . What’s missing is how far to turn right. This is why I used that command in Blocks. But, I discovered through other projects that Blocks doesn’t act quite the same as Modkit.

I just put in color degrees as a sample to look at…and yes, it says 1". I never ran it because I realized that it would travel only 1" and then read the line. If it was successful then it would continue for ever. Maybe?

1 Like


What you can do in order to travel a certain distance while following this method is using your motor encoder. Your motor encoder travels in degrees, so you would need to measure how many degrees it takes for you to reach your destination, that is why I put it in the print so that you can see it.

I also made a few changes to my code, because I realized my last example did not work, at least for the color sensor part.

P.S. Guide yourself off one motor, because both motors register different degree values.

Yes, it would run forever. I like how you are doing with drive train. I might with it today to see if it works. We can have our students doing drive train instead of two motors, since its a bit easier.

Could you clarify?

Is this robot going to stop at certain colored lines or try to stay on a line.

Either way, this can be activated or deactivated simply with code.

With one sensor, you can achieve this by constantly jerking left and right until the B/W value changes. This accomplishes the task but is slow.

With two+ sensors, you can allow the robot to move forward and self correct based on what both sensors see. For example, if one is set to see black and the other white, if the black sensor sees white and the white sees black, it can turn, for example, right and continue moving. Like @calvc01 said, make sure to add a section to detect the intersections or you may have a robot dance party.

1 Like


Thank you for your help. The robot will travel forward a certain distance. What I can’t wrap my brain around is how to code it to make it go that distance and follow the line. I can program it (when I have accessibility to my stuff in my classroom) to either use one or two sensors and go forward. But, it will just go forward indefinitely.

For example, if I go forward 5 turns. It will go forward 5 turns but, not read the line following code as it moves forward. I want it to do both. This is where I am lost.


It’s actually not easy using the Drivetrain because the only data you can get is from the Drivetrain gyro, not from the Drivertrain encoders. If you used separate motors instead of a drive train, you could then “repeat while encoder is < however far you want to go”.

1 Like

I’m not planning on using a drive train. I’ll try the repeat.

So, repeat (enter line follower program), while travelling X distance?