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