Color sensing - color as 1 object not a bunch (optical sensor shoots every 20ms)

I am trying to create code for a class project in which students use the robot to count the number of times they drive over colored lines. It needs to keep track of the number of times it sees a line and change the number on the screen as it passes over. I’ve tried a bunch of different codes, booleans, defining my own blocks, etc and can’t get it to work. My students are using block code (as am I).
Any tips would be greatly appreciated!
image

I think you need to put the "if sensed then … " in a forever block. What you are telling it to do right now is to check whether or not it senses something once, whereas you should be telling it to check all the time. If you are using an older model of VEX, you could put a wait .1 second block at the bottom so it doesn’t overheat.

That didn’t work either. It doesn’t even turn red when it sees red.

I have a couple of different things then
#1 try separating the "when started into two parts. One part is just the forever block, the other everything else, and keep the “when I receive seered” separate as well.
#2 try setting light power to 100%. Depending on how picky the sensor is, it might be seeing it as a dark red instead of regular red.
I haven’t worked a ton with optical sensors, so this is the extent of my knowledge. Sorry if neither of these work.

put the if optical detects red inside the while true loop.

What do you mean by that?

A couple of comments.

What color is the background that the red line is on ? If perhaps white, then this will probably impact the line detection for the following reason.

The optical sensor works by reading the amount of red, green and blue light reflected from an object. It uses these values to compute a hue value, a range of hues is detected as a color, red happens to be +/- 20 degrees around a hue of 0.
Black, white and grey are not detected as colors, the reason being that, under ideal conditions, they would reflect equal amounts of red, green and blue light. In practice, if the lighting trends towards red, hue is likely to be 0 and the sensor would return a red color as the optical sensor has no concept of “no color”, it has to return something.

Assuming you can work around that (perhaps change to green lines), then you will need to detect not just that a line is present but that you have changed from a “no line detect” state to a “line detect” state and use that to trigger the number being incremented by one. Once detected, you need to wait until the line is not detected before looking for another one.

1 Like

I have also tried it to reading red. It doesn’t have a problem reading the color itself. The problem arises in the counting. It sends the signal for color every 20 milliseconds so as it sees the colored line, it counts too fast and doesn’t just see the line as 1 object. I need to figure out how to get it to see the color and then when it stops seeing it to make that count as one object

That’s why I said.

you will need to detect not just that a line is present but that you have changed from a “no line detect” state to a “line detect” state and use that to trigger the number being incremented by one.

Here is an example.

2 Likes

Wow, you are so helpful! I was struggling with the concept you were explaining, the actual code helps me so much. You are a lifesaver. Thank you so very much!
Now all I have to do is add driving but that shouldn’t be hard.