How to program the vision sensor to sort colored balls

I am having issues trying to code the vision sensor to sort out the colored balls?

1 Like

There’s a few possible answers to this question, it’s a little vague.

Are you having trouble calibrating the sensor to recognize balls?

Do you just not know any methods of programming to actually remove aforementioned ball?

Or do you just not know where to start?

Uh, are you using VexCode pro or v5 blocks?

Quick note regarding vision sensor…

-It has a ridiculously wide range 9f vision, which can be bad for close range stuff like ball sorting. (It was designed for longer range object recognition in mind)

-Small lighting changes mess with the vision sensor big time, so be prepared for a ton of tuning, shuttering off parts of the vision sensor lens, possible making a little “box” to eliminate possible lighting issues, Ect.

-We switched to optical sensor for close range color detection, and then also used the distance sensor to actually detect the presence and position of the ball, so we know when to actually use the values of the optical sensor.

-What is your goal for sorting? We found that in auton and prog skills, color detection is unessesary, just use strategically placed limit switches.

-In driving, well, it certainly makes it easier on the driver, but it also means if something goes wrong, you need multiple failsafes to make up for it.

I’m asking this so you avoid “if other teams are doing it I should immediately do that.”

-Some teams don’t even have an ejector…
They rely on awesome driving. And probably other stuff too…

-Color sorting, depending on your method, will not be a quick build/program, it takes time.

However, if you have evaluated all decisions thoroughly and decide for multiple reason to definitely get color sorting working, go for it!
I would recommend the optical sensor though, but they might still be outta stock.

Vision sensor is possible, if very hard. You could prolly even simply use limit switches to detect the presence of the ball, and assign it a place (score or kick out) with two buttons…

Just getting he ideas flowing!

Also, you have checked the VEX knowledge base to learn the basics of the vision center, right? And prowler YouTube?

If not, that’s your first stop. Have a great season! :slight_smile:


I am using vexcode pro and I just don’t know where to start.

As GR8-who-RU mentioned, your question is a little vague to provide specific help.

If you have an optical sensor, I would strongly suggest using it as opposed to the vision sensor; it will make your life much easier.

If you still plan on using a vision sensor, I would suggest you look at the tutorials and examples in Vexcode Pro. You should also read the articles about the vision sensor on Vex Knowledge Base.

After you do that stuff, you should have a decent understanding of how the vision sensor works. From there, you can go to the vexCode Pro API (search “vexcode doxygen”) and you can read about all the vision sensor’s functions. There are not many so it isn’t difficult to figure out how they might interact in a given program.

If you decide to use an optical sensor, which I strongly recommend, then you should basically do the same thing (read the Knowledge Base articles, look at the examples, read the API). Using the optical will be much easier compared to the vision because the optical was practically made for the purpose of color sorting. I hope this helped.


the vision sensor is not great at recognizing colors when not in perfect lighting. In my tests, I could not get the sensor to tell the difference between red and blue even in pretty good lighting (about what you’d expect at an indoor competition).
I cannot vouch for the optical sensor since I’ve never tried it, but I’m guessing that it’s much better at seeing different colors in all sorts of lighting than the vision sensor, so that might be a good option.

in terms of learning how to write the code itself, you can probably find good examples in the API.


Well for starters you need at least 2 parts for a successful auto sorting program. The first is detecting the ball and the second is ejecting the ball. We will tackle the first part in a second. Your ejector needs to be built in such a way that you can consistently eject a ball with great speed every time. If you cannot do this then auto sorting will be very difficult and will not provide any benefit. If you can do this then detecting the color of the ball is the next step. if you do not have an optical sensor, you will need to position your vision sensor in a way that the only colors being detected are the balls coming into the intake. Try placing it pointed down on the front of the bot or facing sideways at the opening to the rollers. Once you have found the perfect spot to consistently detect the color then we can process the data we are receiving.

**The code **
The easiest way to do this is to create a separate task that handles detecting the color of the balls and control the rollers.

the general outline would be

void ejectBall(){
some function to consistently eject a ball. 

   take vision snapshot
   determine the color of a ball by the largest red or blue object

   if (!correct color && controller button pressing){

   else if (correct color && controller button pressing){
      score/intake normally

   else {
      stop rollers


Obviously there is a lot more that goes into that. You should use some variables to store the color of the ball and also the eject function can get quite complex as well. Like @GR8-who-RU stated you can use limit , distance, or line sensors in order to more accurately eject the unwanted ball. This isn’t the only way to do this of course but your goal should be to create an auto sorting program that simplifies the work that the driver has to do. If it is slow or inconsistent then it will negatively impact your robot and you will be better off just practicing.


Yeah, I can’t figure out the code that senses the color of the ball and then ejects or scores the colored ball. I have tried the get the brain the print blue ball of no blue ball. It is just not working for me.

Hmm, what about it? Is there a statement the code does not recognize
Or do you not quite know how to convert @Cadaver_42 's pseudocode into code?

I’m assuming you have indeed looked at the VEX knowledge base and gleaned all you can from that and YouTube as well? You probably won’t learn everything you need to know from here without some good research.
Please post your code. Its harder to help troubleshoot if we don’t really know what to help with.

1 Like

#include “vex.h”

using namespace vex;

event checkRed = event();
event checkBlue = event();

void hasBlueCallback() {
Brain.Screen.clearLine(1, black);
Brain.Screen.setCursor(Brain.Screen.row(), 1);
Brain.Screen.setCursor(1, 1);
if (Vision1.objectCount > 0) {
FRM.spin(directionType::rev,36, velocityUnits::pct);
FLM.spin(directionType::fwd, 36, velocityUnits::pct);
RRM.spin(directionType::rev, 36, velocityUnits::pct);
RLM.spin(directionType::fwd, 36, velocityUnits::pct);
} else {


void hasRedCallback() {
Brain.Screen.clearLine(3, black);
Brain.Screen.setCursor(Brain.Screen.row(), 1);
Brain.Screen.setCursor(3, 1);
if (Vision1.objectCount > 0) {
Brain.Screen.print(“Red Object Found”);
} else {
Brain.Screen.print(“No Red Object”);

int main() {
// Initializing Robot Configuration. DO NOT REMOVE!


while (true) {

wait(1, seconds);