Optical Sensor Coding

Hello. I am currently coding an Optical Sensor to sense when a mobile goal is close enough to my claw for me to grab it. My current problem is that whenever I put a mobile goal close enough, it works, but it will not let go. It just keeps holding until I turn it off. I would like it to close and hold, until I click L1 to release. I do not know how to do this. Thank you for reading!

Update: The problem is not that the optical sensor is not working. The problem is that the second i click L1 to release the claw, the optical sensor senses the mobile goal and closes again. I do not want to put a delay in there, because I am worried that it will take longer to close the claw. Basically, what i am asking is if there is a way to turn off the sensor for 5 seconds after I tell it to release so the mobile goal can leave the system, then start back again.

1 Like

You really did not give enough information to let others help.
At minumum you need to told coding software, your current code.
Here is attempt to answer without information

if (L1.pressed() || Optical Sensor condition == False) release;
else if (Optical Sensor condition == True) grab;


Sorry. I can give more information in a second.

I added more onto the thread

We had a similar problem which we solved by instead of having a click release the goal, we programmed it to have the user hold down a controller trigger to keep our grabber open, and if the trigger was released, then automatic grabbing was allowed to happen

How would yoy code that?

Something like:

//your automatic grab function
if ( closeEnoughToGrab && !Controller. ButtonR2.pressing() ) {
  //grab goal
} else if ( Controller. ButtonR2.pressing() ) {
  //release goal (keep clamp open)
1 Like

Could I do something where if I hold it does one thing, but if I press it does another? I currently have L1 set to opening the claw when pressed once, but I am wondering if I could have both.

How is the brain supposed to know the difference between “hold” and “press”? The button is either down or up; the brain doesn’t know the difference between pushing a button for 50 msec and praying a button for 3,000 msec. Unless you want to set up your whole own timer situation to explain, no.

1 Like

Could you put that part of your code in the thread? I will be able to help you better then.