How do you program an optical sensor(c++)

For our color roller this year we are going to be using a flex wheel that spins with a motor and we want to have an optical sensor to determine what color the roller is on. When the desired color is showing on the color wheel we want our motor to stop spinning. There is one slight issue though, I don’t know how to code this. My knowledge of coding ends after driver control and autonomous. I use PROS for vex and I code in c++. I would really appreciate if you got back to me soon. Thank you.

4 Likes
  • Do you want to have a button have a button where if you're pressing it and the color sensed is wrong then spin the motor?
  • Do you want it to spin the motor if the color is wrong, without a button? (I would prefer this.)
  • If the flex wheel is going to be chained to your intake, what sort of buttons and automation do you want for it?
  • Do you know the functions in PROS for the optical sensor, or are you asking what they are, too?

I’m sure I can help you once I know what you want.

4 Likes

https://pros.cs.purdue.edu/v5/api/cpp/optical.html
^ optical sensor api

2 Likes

Thanks for responding, I would rather have it be one button makes it so that red is showing and another button makes it so that blue is showing.

Thanks for responding

Also I know what an optical sensor does but I’m not sure of how to code the functions in pros.

So you want something like

if RedButton.pressing():
  spin roller to red
if BlueButton.pressing():
  spin roller to blue

You don’t want something like

void pre_auton():
  while (isPre_auton()):
    if (screen.pressing()):
      if (screen.pressX() < 240):
        bool onRedAlliance = true
      else:
       bool onRedAlliance = false

to set the alliance color and then something in driver like

if (RollerButton.pressing()):
  spin roller to alliance color

YesSir sent you the link.

Is your roller spinner on (or going to be on) a separate motor than anything else? If so, we could make it just you drive up to the roller and if it’s on the wrong color, it spins the roller, without taking up one of your buttons and making you push it.

3 Likes

That’s what I want, I would rather not have to do it where it is automatic because then it would have to be in separate codes and if my driver goes into the wrong one then we won’t be able to score on the roller.

You could make a print statement that signifies what color the driver has chosen, For example a switch statement that would print Red or Blue depending on the value 1 or 0 assigned to it.

That is true, but I have a solution for this. See at the *.

That is not true.

// In pre-auton
if whatever is done:
  bool onRedAlliance = true;
else if something else is done:
  bool onRedAlliance = false;

// In driver
while (true):
  // Joystick control
  // Whatever buttons, etc.
  
  // For a button to spin, you could replace the following with what's at the **
  if optical sensor senses red and not onRedAlliance:
    spin roller;
  else if optical sensor senses blue and onRedAlliance:
    spin roller;
// **For a spin-roller button, do this instead of the ifs above
if rollerButton.pressing():
    if optical sensor senses red and not onRedAlliance:
      spin roller;
    else if optical sensor senses blue and onRedAlliance:
      spin roller;

* Here’s the solution to selecting the wrong alliance in pre-auton.

bool onRedAlliance;
// In pre-auton
if whatever is done:
  onRedAlliance = true;
else if something else is done:
  onRedAlliance = false;

// In driver
while true:
  // Whatever other controls
  if allianceToggleButton.pressing():
    while allianceToggleButton.pressing():
      wait
    onRedAlliance = not onRedAlliance;
// Here's another choosing the wrong alliance in pre-auton safeguard
// Put this in your driver control while loop
if redAllianceButton.pressing():
  onRedAlliance = true;
else if blueAllianceButton.pressing():
  onRedAlliance = false;

But if you insist, I’m willing to help you write a code that spins to the color chosen by a button pressed every time you want to spin a roller.

// In driver
while (true):
  if redRollerButton.pressing() and optical sensor senses blue:
    spin roller
  else if blueRollerButton.pressing() and optical sensor senses red:
    spin roller

If you get it, great. If not, explain what’s not working and I’ll help.

1 Like

I like the idea of an alliance button, but im confused on how to code a button to determine what color alliance you’re on

I’m not sure what you mean “determine”. A button cannot sense what alliance you’re on. Clear up what you want and I’m happy to help.

  1. If RedButton is pressed, spin the roller to red. If BlueButton is pressed, spin roller to blue.
  2. If RollerButton pressed, spin roller to alliance color.
  3. If roller is there, spin roller to aliance color. (Does not require driver to press a button to spin the roller.)
  4. In pre-auton, select alliance color.
  5. If RollerToggleButton pressed, set alliance to opposite color.
  6. If RedAllianceButton pressed, set alliance to red. If BlueAllianceButton pressed, set alliance to blue.

Those are basically the options. If you have another idea, I can probably help with that, too. Also, these are not all mutually exclusive. If I were doing it for my team, I would go with 3, 4, and 5. Select alliance color in pre-auton, drive around and spin rollers automatically, and if they spin to the wrong color, push the “on the other alliance” button to set the alliance setting right.

What do you want?

1 Like

I would like it to set alliance color pre auton

What ALL do you want? Just 2 and 4? Just 3 and 4? 3 and 4 and 5?