Coding an Optical Sensor - Vex V5 Text Pro

Hey my team is using an optical sensor this season to sense the ball color. I tried to program it myself, but it doesn’t seem to work. Can anyone tell what’s wrong?

void usercontrol(void) {
  OpticalSensor.setLightPower(100, percent); 
  OpticalSensor.setLight(ledState::on); 
while (1) {
if(OpticalSensor.color() == blue){
  TopMotor.spin(reverse);
  BottomMotor.spin(forward);
  
}

wait(20, msec);
  } 
 }

https://api.vexcode.cloud/v5/html/classvex_1_1color.html

Just shooting in the dark, perhaps you need to say color::blue? Does the program give any errors?

2 Likes

No, there’s no errors in the program, but on the brain it says “Jump Table Error”


2 Likes

then you need to update vexos to 1.0.12 and make sure you are running the latest version of VEXcode.

8 Likes

Okay, thank you, I will do that immediately

So we updated the brain (turns out it was three updates behind) and the program works. Kinda. It worked at first but then stopped right away. Right now there is an error, it says “Use of undeclared identifier, ‘blueball’.” This is the newest version of the program:

void usercontrol(void) {
OpticalSensor.setLightPower(100, percent); 
OpticalSensor.setLight(ledState::on);
TopMotor.setVelocity(100, percent);
TopMotor2.setVelocity(100, percent); 

while (1) {
if(OpticalSensor.color() == blue){
OpticalSensor.objectDetected(blueball); 
}

wait(20, msec);
return;
}
} 

void blueball(void) {

TopMotor2.spinFor(fwd, -1000, degrees, false);
TopMotor.spinFor(fwd, 1000, degrees, false);



}

 //Callbacks
 int main() {
 // Set up callbacks for autonomous and driver control periods.
Competition.autonomous(autonomous);
Competition.drivercontrol(usercontrol);
OpticalSensor.objectDetected(blueball); 
 // Run the pre-autonomous function.
pre_auton();

// Prevent main from exiting with an infinite loop.
while (true) {
  wait(100, msec);
}
}

It might be due to the order in which you have the functions. You are defining the blueball function after you call it in where I assume is the user control function. I would move the blueball portion of the code above where you define the usercontrol function.

1 Like

What was the code part for? It’s literally exactly the same

Yea, idk why it quoted like that

Well I moved it to before the “while” part and more errors showed up

try moving to the part before void usercontrol(void) so that when you are calling the function, its not in void usercontrol(void)

Ah yes, no errors now. Thank you. I will see if it will work now

No problem
(20 Characters)

For some reason, the program doesn’t sense the color. It runs the same program for blue that it does red.

 void blueball(void) { 
   TopMotor2.spinFor(fwd, 1300, degrees, false); 
   TopMotor.spinFor(fwd, 1300, degrees, false); 
   }
void redball(void) {
   TopMotor2.spinFor(fwd, -1300, degrees, false); 
   TopMotor.spinFor(fwd, 1300, degrees, false); 
}
void usercontrol(void) { 
 OpticalSensor.setLightPower(100, percent); 
 OpticalSensor.setLight(ledState::on); 
TopMotor.setVelocity(100, percent); 
TopMotor2.setVelocity(100, percent); 



while (1) { 
 if(OpticalSensor.color() == blue){ 
 OpticalSensor.objectDetected(blueball); 
 } 
 if(OpticalSensor.color() == red){ 
 OpticalSensor.objectDetected(redball); 
 } 
 wait(20, msec); return; } }

try using an else if statement for sensing red rather than using another if statement. That way, no repetition is possible.

I tried that, this is the new program we would actually use. Now nothing happens.

void blueball(void) { 
   
   }
void redball(void) {
   TopMotor2.spinFor(fwd, -1300, degrees, false); 
   TopMotor.spinFor(fwd, 1300, degrees, false); 
}
void usercontrol(void) { 
OpticalSensor.setLightPower(100, percent); 
OpticalSensor.setLight(ledState::on); 
TopMotor.setVelocity(100, percent); 
TopMotor2.setVelocity(100, percent); 



while (1) { 
 if(OpticalSensor.color() == red){ 
 OpticalSensor.objectDetected(redball); 
 } 
 else if(OpticalSensor.color() == blue){ 
 OpticalSensor.objectDetected(blueball); 
 } 
 wait(20, msec); return; } }

Okay I fixed that. But now it’s just not sensing color, I have no program for blue ball and it still runs

Don’t use event registration calls in a loop.

while (1) { 
 if(OpticalSensor.color() == red){ 
 OpticalSensor.objectDetected(redball); 
 } 
 else if(OpticalSensor.color() == blue){ 
 OpticalSensor.objectDetected(blueball); 
 } 

just call your two functions directly.

if(OpticalSensor.color() == red){ 
   redball(); 
} 

etc.

2 Likes

Yeah turns out we dont want anything to happen with the blue ball though, so new program:

void redball(void) {
    TopMotor2.spinFor(fwd, -1300, degrees, false); 
    TopMotor.spinFor(fwd, 1300, degrees, false); 
}
void usercontrol(void) { 
OpticalSensor.setLightPower(100, percent); 
OpticalSensor.setLight(ledState::on); 
TopMotor.setVelocity(100, percent); 
TopMotor2.setVelocity(100, percent); 



while (1) { 
 if(OpticalSensor.color() == red){ 
 OpticalSensor.objectDetected(redball); 
 } 
 else if(OpticalSensor.color() == blue){ 
       TopMotor.stop();
      TopMotor2.stop();
 } 
 wait(20, msec); return; } }

But when the blue ball is sensed it still runs.