# Ball sorting algorithm

Hello,

We are making robots like this WCP 2020. We need to sort by counting the number of balls we have received in the ball storage system.

1- We need to build a system that needs to sort how many balls are in the robot and accordingly. Do you have an example? Which sensors should we use? etc.

Good luck to all teams!

Iâ€™m a bit confused by what you mean by â€śsort.â€ť Are you trying to count how many balls you have in robot?

Using a beam break sensor or similar proximity detector, itâ€™s really easy.

Use a single integer variable to track the number of balls in your robot. Every time the beam transitions from unbroken to broken, add one. Whenever you shoot, reset it to zero. This assumes that you get rid of all the balls in your robot every time you shoot, but I donâ€™t see why you wouldnâ€™t already be doing that.

You could also add a sensor on the exit or watch a parameter like current to decrement the ball count as individual balls leave the robot.

1 Like

This depends on what your intake/storage system looks likeâ€¦ If you are doing internal indexer, there isnâ€™t a real way to check(through program) how many balls are in the robot itself, though you can check how much passed through your feeder.
Since you said robot like WCP 2020, I am assuming this will be a single ball intake system. So here are some ideas that are possible.

1. You can install a limit switch sideways in the way of the ball travel path. Have a counter that increases by one every time it is hit.
2. Like @iwilcove said, you can use a break beam sensor.
3. You can also see a change in current of your intake.
4. You can even use a vision sensor like limelight/chameleon/etc to detect the ball pass certain point and increase your ball counter.
1 Like

both counting and ensuring regular transmission to the shooter by number

Then I second iwilcoveâ€™s suggestion

I was working on a state machine for our intake/throat but then we pivoted and went a different direction.

Below is my state machine with the logic.

`S. Intake` and `S. Throat` represent the sensors in those locations

The ZIP contains pictures illustrating each step of the state machine. Hopefully someone can use it, or if not, get some inspiration for their own logic!

ShooterLogic.zip (1.1 MB)

1 Like

And if you do this (and you hold a max of five balls) then when it breaks, you can set the variable to five in case your other sensors missed one or over counted. In out robot, we only disable the intake when the top sensor is hit, not when the count reaches five.