How to use the controller pressed function in python?

I want to be able to just press R1 on my controller, and my wings will pop out, and I will not have to hold them out. How would I code this python?

I’m not going to code it for you, but one way would be to make a boolean variable (true or false), then make an if statement that changes true to false or false to true. Then, make another if statement that checks if the variable == true and have it deploy the wings.

There is probably a simpler/better way to do this but my way should work!

Thank you so much. I will code this, and I will ask again if there are any difficulties. Thanks.

2 Likes

This is a simple way to do it Open is a boolean starting as false
Set up code : Wings = DigitalOut(brain.three_wire_port.a)
When the button is pressed something like this could happen
Wings.set(not Open)
Open = not Open

If you use python, set a variable and name it something like Wings_Flip_Down or whatever you would like. The name doesn’t matter, but the action defined does. I don’t know how pneumatics work if you do use them, but if you employed the wings like my team did(we used motors), all you need to is code the button so the motor turns 90 Degrees or so. As for defining, add a layer, create a name for it, and type it in this format:

Def Wing_Arms_Flip():
I named our wings Wing_Arms so here is that code
Wing_Arms spin(forward, 90, degrees)

Once you have the variable defined, you can employ that and assign it to the button. This next part took me a little bit to get, with the configuring and coding, so my advice: REMEMBER THE BUTTON ASSIGNED. The code to assign an action or variable to a button is as follows, using the previous example:

     Controller1_buttonR1.pressed()

Insert your variable defined into those parentheses. This code shows what will happen when button R1 is pressed. Since it has no variable input, nothing will happen when the button is pressed.

Hope this helped, Happy Coding!

You could also add a delay of 1-2 seconds in case you accidentally press the button twice. It will ignore all presses except for the first one.

This is called debouncing.

1 Like

What do you mean by making a variable? I don’t understand how it is implemented here.

In your forever loop, put (2) if/then statements.

If button pressed AND motor is OFF, then turn motor ON.

and

If button pressed AND motor is ON, then turn motor OFF.

You WILL need to add a small delay in the then/else sections otherwise the code will loop (forever loop) and reactivate before you can release the button.

1 Like

The variable is the name you give to the action that you want to happen. When you define a variable, you’re telling the program what that name means and what you want it to do.

You implement it into the code by inserting the variable name into the parentheses; those parentheses basically tell the program what to execute if the button is pressed

If you’re not getting a basic concept like making a variable… maybe drop back and code in blocks for now (and compare the generated code from blocks).

1 Like

No, I don’t get how the variable is being used here.

You watch the button and use it to change a variable value, typically from 0/off to 1/on.

Elsewhere in your code, an if/then statement watches the variable and turns the motor(s) off/on depending on the value.

Or you can look back at my post and do it all with (2) if/then statements or (1) if/then/else statement.

Can you show me an example of this? I don’t want you to do the whole process for me, but I just wanna see what it might look like because I need to see it in order to understand it. Thanks.

I’d say to post your code you have so far and ask for troubleshooting. Or, as I suggested, go back to blocks and then view its generated code.

Here is an example:

isOn = False
cooldown =False
def epicFunction():
 if(cooldown): 
  return
 if (isOn):
  #Do Something is it is on to turn it off 
  isOn = False
  cooldown = True
  #Add a wait here 
  cooldown = False
  return
 #Turn it on
 IsOn = True
 cooldown = True
 #Add a wait here 
 cooldown = False
  

This might have some syntax errors(I haven’t wrote in python in a bit)

Its very easy to create a test program in blocks to learn how the code renders. You can then incorporate snippets into your programming. Make sure you study it and understand how it works. Too often students simply copy / paste. If they don’t understand how it works it may introduce a bug in other parts of the program.

I built the following in blocks.
image

Then I used the Python Code Viewer to get the results.
image

  • IsWingOut is a variable to remember the state of the wings.
  • onevent_controller_1buttonL2_pressed_0 is a function to move the wings. After the function is called the variable is flipped so next time the wings move in the opposite direction. You can rename the function but you will also need to change the name below in the .pressed event.
  • The system event handlers at the bottom bind the button pressed on the controller to the function that moves the wings.
IsWingOut = True

def when_started1():
    global IsWingOut
    IsWingOut = True

def onevent_controller_1buttonL2_pressed_0():
    global IsWingOut
    if IsWingOut:
        digital_out_a.set(True)
    else:
        digital_out_a.set(False)
    IsWingOut = not IsWingOut

# system event handlers
controller_1.buttonL2.pressed(onevent_controller_1buttonL2_pressed_0)

# add 15ms delay to make sure events are registered correctly.
wait(15, MSEC)

when_started1()

This process doesn’t create very clean code but it is readable. You will probably see opportunities to make it better.

1 Like

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