Coding a Optical Sensor

Hello I am new to coding and my team wants to use a optical sensor for our shooter. So when we place the tri-ball on the platform of the shooter the shooter will automatically shoot the tri-ball. I have tried to research it but don’t understand most of it. I also use Vex V5 pro c++ so if you could help me code this it would be greatly appreciated.

There are a few ways you could go about this, although the process is going to be roughly as follows:

  • Wait until the optical sensor sees a specific object or color (green) in this case.
  • Run the shooter motor at full speed.
  • Wait until the object is gone or no longer detected by the sensor (launched by the shooter).
  • Stop the motor.
  • Repeat the process.

vex::optical provides a few ways that could be used to potentially detect a triball in front of the sensor.

  • You can look at the color (hue) that the sensor sees and wait until that color is green using vex::optical::hue or vex::optical::color.
  • You can use the sensor’s object detection to see if something is in front of the sensor.

In this example, i’m going to use both, so it’ll wait until it sees an object and run the shooter if the object is green, then stop the shooter once the green object is no longer detected by the sensor:

bool triball_found = false;

sensor.integrationTime(5); // Set the sensor's integration time to 5ms for a faster update rate
// sensor.objectDetectThreshold(255); // Optional: Tune this value if the sensor doesn't see anything. It can be from 0-255 Higher numbers will make the optical sensor more sensitive to objects farther away.

// Event handler for object detection. This code will run every time the optical sensor sees anything in front of it.
sensor.objectDetected([]() {
	// Check if the object that it sees is green
	if (sensor.color() == vex::green) {
		triball_found = true; // We found a triball
		shooter_motor.spin(vex::forward, 100, vex::percent); // Run the shooter motor
	}
});

// If we lost sight of the object, assume it was launched and stop the shooter motor.
sensor.objectLost([]() {
	// Check if the object that we lost was detected as green by the sensor originally.
	if (triball_found) {
		shooter_motor.stop();
		triball_found = false;
	}
});
2 Likes

Thank you so much I will test this when I have my robot. One more question would this go under driver control?

I also plugged that code into my code and this

Make triball_found a global variable

2 Likes

Sorry I don’t know what you mean by that could you explain a little more thank you.

triball_found should go outside of drivercontrol at the top level of your program so that the events can access it. The rest should ideally go outside of your driver while loop, but stay inside of drivercontrol as to not spam vexcode with new events.

1 Like

Thank you so much it is no longer red I will test it tomorrow.

So I tested that code today and the optical sensor did not tell the shooter to shoot the triball I will have some photos of the code soon but I just plugged in the code you sent.

Uploading: A2637041-A6BA-4D51-B9BE-99F65483869A.jpeg…
Here are some photos of the code slap is our shooter sorry if the pictures aren’t the best.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.