.pressed() function stopping all motors from running

Hello, my team programs in Python, and we have this code.

inpress = 0
def suck_intake():
    global inpress
    if (inpress == 0):
        intake.set_velocity(100, PERCENT)
        intake.spin(REVERSE)
        inpress = 1
    else:
        intake.set_velocity(0, PERCENT)
        intake.spin(REVERSE)
        inpress = 0

def (driver_control):
  
        if controller_1.buttonL2.pressed(suck_intake):  
            while(True):
                sleep(25)
                return Event(suck_intake)

Once we add this pressed function, none of our other motors run (They are in driver control, but removed from this post because it can get confusing to read) Does anybody have any ideas on how to solve this problem?

Have you read the command help or looked at the VEXcode examples code for the pressed() function ?

It is used to register a function that will be called when the button is “pressed”

It does not get used in a conditional (the if statement) and usually registration would not be inside of driver_control. You only posted (I assume) a small portion of your code, but correct use may be.

inpress = 0
def suck_intake():
    global inpress
    if (inpress == 0):
        intake.set_velocity(100, PERCENT)
        intake.spin(REVERSE)
        inpress = 1
    else:
        intake.set_velocity(0, PERCENT)
        intake.spin(REVERSE)
        inpress = 0

def autonomous():
    # some auton code
    pass

def usercontrol():
    while True:
        wait(20, MSEC)

controller_1.buttonL2.pressed(suck_intake)

comp = Competition(usercontrol, autonomous)
1 Like

We wanted to use this function so that we press a button once and the motor spins, and pressing it again makes it stop. Would it still work then? We are no longer by our robot.

We have looked at the example code, documentation, and have also looked at previous forum posts about this topic, but none of these seem to help us.

Also, if you know, why does putting the pressed function in driver control stop it from running?

Well, I can’t see the rest of the driver_control function (which in itself is defined incorrectly), but

if controller_1.buttonL2.pressed(suck_intake): 

probably evaluates to True, and then

while(True):
    sleep(25)

just blocks anything further down from running.

The code you posted is just completely incorrect in its use pf pressed()

1 Like

you would want to use pressing(), or rework your use of pressed().
pressed() is, as stated by @jpearman , an event handler
so whenever the code reads a line using pressed, it’ll assign a function to that button
for example, if I had this code:

while (1):
    Controller1.buttonX.pressed(suck_intake)
    sleep(20)

in this example, every 20 milliseconds, the code would register the suck intake function to the button, meaning it would do the same action multiple times. For example, if this looped two times (or 40 milliseconds), it would make the function run 2 times when the button is pressed. If it looped 5 times, or 100 milliseconds, it would make the function run 5 times when the button is pressed.

assuming you want a toggle (i’m not very good with python, forgive me):


def usercontrol:
# random code here

#outside usercontrol
controller_1.buttonL2.pressed(suck_intake)

alternatively, use pressing (much easier but delay)

def usercontrol:
if controller_1.buttonL2.pressing():
    suck_intake()
    wait(100, msec)
    # definitely a way to do it without the wait but idk

you might have treat this like pseudocode, i don’t know python syntax at all, but the idea is still there