# Pressing Your Buttons

The `Button()` function returns a button object, and with that object you have access to a wide array of button functions. To use the `Button()` function, as we have done throughout the guide, you simply call:

    Button(pin)

where `pin` is the number of the GPIO pin that is labeled on the Lid Connector Board. The simplest thing we can do with a button, as we've done previously, is to see if the button is being pressed:

from rstem.button import Button my_button = Button(14) print(my_button.is_pressed())

The above checks (and prints out) if the button attached to GPIO14 is currently being pressed.

The `is_pressed()` function is simple and easy to use, but has a couple issues. One issue is that it only checks to see if the button is being pressed at the time it is checked, so if our code isn't checking very frequently for a button press, the press can be missed.

For example, say we write some code that checks once a second to see if the button is pressed. If a user pushes and releases the button quickly (for example, in a quarter of a second), it is likely the that the code would miss the button press — it might check for a press before the button is pressed and then check again after the button was pressed, but if it doesn't check during the press, the code will not know the press happened.

The Button object can fix this problem with a couple new functions. The function `presses()` returns the number of times the button has been pressed since the last time the function was called.

Similarly, the function `releases()` returns the number of times the button has been released since the last time the function was called. For example, if you run the following code, and push the button several time while it is running (during the `sleep()`, the output will be the number of presses:

from rstem.button import Button import time my_button = Button(14) time.sleep(3) print(my_button.presses())

# Waiting for Presses

There are often times when you don't want to do anything in your code except wait until a certain button is pressed. A simple way to do this is highlighted with arrows below:

from rstem.button import Button import time my_button = Button(14) *while my_button.presses() == 0: * time.sleep(0.01) print("The button was pressed!")

The above code will wait until the button is pressed (that is, the `while` loop keeps running while there are zero `presses()`, and then it outputs a message once a press occurs.

While the code above will work, there's a simpler and more efficient way to do this using the function `wait()`, provided by the Button object:

from rstem.button import Button import time my_button = Button(14) *my_button.wait() print("The button was pressed!")

The Button object also provides a function, `wait_many(buttons)` that will wait for any button in a list of `buttons` to be pressed. But, because `wait_many()` works with a bunch of buttons together, and not a specific button, the way you call it is a little different.

To call the `wait_many()` function, you need call `Button.wait_many(buttons)`. For example, say you had the same four buttons hooked up as in the project. Then, to wait for any one of them to be pressed:

from rstem.button import Button my_buttons = [Button(14), Button(15), Button(25), Button(7)] pressed_button = Button.wait_many(my_buttons) print("Button", pressed_button, "was pressed!")