Coding Optical sensor

Hello so someone helped me code this for my optical sensor and I tested the code but it did not tell our shooter to shot. My team wants the optical sensor to tell our shooter to automatically shot the tri-ball when it sees it. If someone could help me fix this code it would be greatly appreciated.

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;
	}
});

edit: code tags added by moderators, please remember to use them

Can you format the code so that it is easier to read? You put three ` in a row, no spaces, at the beginning and end of the code.

Example:

`` `
code

`` `

How close is the triball ? Optical sensor only senses color when the object is close, perhaps < 1 inch.

perhaps use a range of hues, which you can figure out, not vex::green.

2 Likes

The sensor very close to the tri ball less than 1 inch. When you place the tri-ball on top of the shooter platform the tri-ball is right on top of the optical sensor. Also how would I code hues I know how you have to go to devices on the brain and put the tri ball on the sensor but how do I code that.

ok, you copied some code and probably have no idea how it works, the questions you are asking suggests you probably should not be using events and lambda functions.

No need to do this.

sensor.integrationTime(5);

leave sensor integration time alone, 5mS would need lots of light

You have to think about what states you want the motor to go through, sure start the motor when the object is detected but, assuming the motor will cause the object to move, you probably do not want to stop the motor when the object is lost.

I would take the approach of using a state machine, kick it off when the triball is detected, and then run a sequence of events, start the motor, wait some amount of time, stop the motor, perhaps wait some more and then start over.

use the V5 dashboard to determine the hue (ie the color) of the triball, I don’t have one here but lets say it measures as 80 degrees. You would then use a test to see if a detected object was in the range of perhaps 40 to 120, you will need to determine these values.

here is a simple example that “may” do what you want, I know nothing about your robot and its motor configurations, just use this example to learn about how this might be done, don’t just cut and paste the code. (and yea, I did this on EXP rather than V5, but APIs are the same)

You may want to turn on the optical sensor led, I’ll leave that for you to figure out.

example
/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       james                                                     */
/*    Created:      11/7/2023, 6:00:33 PM                                     */
/*    Description:  EXP project                                               */
/*                                                                            */
/*----------------------------------------------------------------------------*/
#include "vex.h"

using namespace vex;

// A global instance of vex::brain used for printing to the EXP brain screen
vex::brain       Brain;

// define your global instances of motors and other devices here
optical          op1(PORT2);
motor            m1(PORT3);

int main() {
    int shootState = 0;
    int shootTime = 0;

    while(1) {       
        // debug
        Brain.Screen.printAt(10, 30, "%d", shootState);

        if( (shootState == 0) && (op1.isNearObject()) ) {
            if(  (op1.hue() > 40) && (op1.hue() < 120) ) {
              // some type of greenish object
              shootState = 1;
            }
        }

        switch( shootState ) {
          case  0:
            // nothing to do
            break;

          case  1:
            // start motor
            m1.spin(forward, 100, percent );
            shootState = 2;
            // run motor for 1000mS
            shootTime = 1000;
            break;

          case  2:
            // motor is running
            shootTime -= 20; // make same as sleep_for delay
            if( shootTime <= 0 ) {
              shootState = 3;
            }
            break;

          case  3:
            m1.stop();
            // leave motor stopped for 1000mS
            shootTime = 1000;
            shootState = 4;
            break;

          case  4:
            // motor is stopped, wait before testing optical
            shootTime -= 20; // make same as sleep_for delay
            if( shootTime <= 0 ) {
              shootState = 0;
            }
            break;
        }

        // Allow other tasks to run
        this_thread::sleep_for(20);
    }
}
2 Likes

Ok thank you so much I will try something like this.

Oh also would this go in driver control?

again, don’t just cut and paste the code, if you understand it then you can figure out where it goes.

using as part of the main loop in driver control is certainly one option.
but it could also be running as a separate task.

3 Likes

Ok thank you I won’t copy and paste the code.

Also is this what you meant by you may want to turn on the optical sensor led?
optcial.setLight(ledState::on);

pretty much, try it and see what happens.

2 Likes

Ok I will test it tomorrow when I have my robot