IQ Color Sensor Programming to Stop at a Black Line

My team is currently using Vexcode IQ Blocks and are having problems trying to get the robot to stop at a black line. We are able to stop at a white line using the code we made. We tried to reverse the sign from greater than to less than and the program does not do anything. Along with that we tried many different loops and other ways of reordering the stop and forward for the motors

If there’s any help we can get it would greatly be appreciated. Thank you.

Color Sensor Test.pdf (1001.4 KB)

This video will have the information you need.


We tried what was suggested but we just want to detect the black line once and then stop.

So the code at 8:55 doesn’t detect lines for you? Can you post your code?

I would like to do it once and not forever. It does detect lines, but I would like to break that forever loop if possible.

What you can do is create a variable that when you detect the line, you set it to a different value. Then, use a while loop that runs while the variable is equal to what you originally set it to be. That way, you end the loop.

1 Like

There is a break command, but you could also use a while loop instead.
While sensor value > threshold.


Tried this one and so far it has not worked. Color Sensor Test.pdf (1.8 MB)

You should either do the while loop or do the forever loop with an if-statement and a break. Don’t do both. Essentially, you are ending the loop after the code runs once.

I tried both solutions and if I put in a greater than sign it’ll go forward forever, but if I have a less than sign it will skip over everything.
Color Sensor Test (1).pdf (2.7 MB) Color Sensor Test (2).pdf (2.2 MB)

In your first example, the break is outside the condition if statement, so it breaks straight away and the robot never stops. Try moving break inside the if directly under the motor stops.

For the while loop, you should say:
While colour > threshold

Then outside the loop, stop the motors.

The colour sensor brightness value is greater on a whiter surface isnt it? So while it is greater than your black threshold (i.e. on white), drive forward.