Optical Sensor refreshes too fast

I recently received an optical sensor that I intend to use for the automatic sorting of balls. However, the problem I have run into is that my optical sensor will refresh and not give the function enough time to run and eject the ball. I tried a code that looked like this:
if (Optical1.color() == blue) {
Ratch.spin(forward);
wait(1,sec);
} else if (Optical1.color() == red) {
Ratch.spin(reverse);
wait(1,sec);
}
The problem I ran into with this program is that the entire robot will wait for the whole second. This is a problem as the robot will be unresponsive other than running the motor for this time. My question is, is there any way to slow down the refresh time of the optical sensor? If I could do this I believe there would be enough time between refreshes for the ball to be entirely ejected. Any help would be greatly appreciated. Thanks!

1 Like

If you only want optical sensor readings. for example, every 1 second, then sample the optical sensor in a separate thread and store the result in a variable you can read in your main driver control loop. FYI, the optical sensor may not give you an exact reading for “blue” and “red”, it’s calibrated to work well with some VEX objects, but depending on your environment you may need to read the hue of the object and then make a determination as to what the color is.

7 Likes

Would the color read different depending on the orientation of it?

I would not recommend slowing down the refresh rate as you would get slower and inconsistent response times. Instead, use similar code to what you showed but in a separate task from the rest of your control code.

3 Likes

I suggest you experiment. Go to the optical sensor dashboard and see what range of hues you read for the objects on your robot, try with the optical sensor led on and off.

optical

The range of hues we consider red is 340 to 20 degrees (40 degree range). If you find your ball is slightly outside that range, then read hue directly and set the boundaries accordingly.

6 Likes

Whats the advantage to hue over something like rgb?

Typically you will be looking for something with HSV in certain ranges, not RGB in ranges. If you really want though, you can convert HSV to RGB.

2 Likes

I see, so its easier mathematically/logically?