Optical sensor help

Hi, I am new to using sensors so i looked an old post on the forum to get some help and it helped me slightly. I am planning on using an optical sensor to sense the colour of the roller and spin a motor until it is the other colour.
I have a few questions.

First, what do i swap blueball for in this line of code

1 Like

ok, According to the Vexcode API

in the line of code you are using, blueball is a Function, so when it detects an object then it runs blueball, the line of code you are probably looking for would be

(Optical.color()) this should return the color of what its looking at and then you can compare that to a range of acceptable values for the red and blue sides to know which side you are looking at, and spin the roller accordingly.


So do i not need Optical.objectDetected(blueball); ?

All that says is when an object is detected, run the function blueball, if you have a function named blue ball that will do somthing, then you need that, otherwise it is not something you need


My implementation of it was using hue since google said that is basically a fancy way of saying color. Here is the code I use for mine.

while(int(Optical.hue()) < 15 && int(Optical.hue()) > 5 ){
    RollerSpinner.spin(forward, 10, percent);

Though it is for red not blue you can just use std::cout << Optical.hue(); in a loop and point it at the roller to find what to set it at.


Hue is a good way to detect, being more precise, but the sensor already comes with color presets, as seen in Jpearman’s post here. you can just use Optical1.color() == vex::blue or a different color like Optical1.color() == vex::red. Hope this helps!


if(Optical.hue()){ intake.stop(brakeType::brake); }

for blue, the optical sensor detects blue as greater than 140. How do I put that into this line of code?

Use the same code from before but put the 5 as 140 and take out the first condition.

while(int(Optical.hue()) > 140){
Do stuff

You could also do optical.color() == blue

1 Like

Yeah but that’s less cool :confused:

1 Like

While theoretically correct, using ranges of hue values is safer in the real world as there may be different lighting, wear on the object, etc.

The value of hue in the HSV (Hue, Saturation, Value) encoding scheme is based on the orientation of the color wheel, explained in detail here: HSL and HSV - Wikipedia

else if (Optical.hue() <60){
  intake.spin(vex::directionType::fwd,190, vex::velocityUnits::pct);

      intake.spin(vex::directionType::rev,190, vex::velocityUnits::pct);
  else if(Controller1.ButtonL2.pressing()){
      intake.spin(vex::directionType::fwd,190, vex::velocityUnits::pct);

this is the code at the moment. The intake does not spin. How do I fix this?

I had a similar issue in my code and this is the problem. You are telling the motor to stop spinning if no bumpers are pressed regardless of what color the optical sees. This causes the motor to constantly stop even if the optical sensor sees the right color.

i removed that line of code and the motor still doesn’t spin. Any other suggestions?

Not connected to why it isn’t spinning but you should maybe remove the waitUntil unless you’re fine with not having any control over your robot until the roller is the correct color. Does it spin when you place it in front of the rollers? Does it spin when you use the bumpers?

it spins when i press the bumpers, but doesn’t spin when it detects the roller

Check if your hue values are correct using the devices menu

blue is above 140, red is below 50

I have decided to change my plan to having the optical sensor spin the motor in autonomous. I don’t know how to do this. Can anyone help me with this

I think the code would roughly be

waitUntil(roller is right color);