How to have code continue to read one piece throughout the rest of the code?

24 views Asked by At

Okay, I am trying to figure out how I can have this code continue to check if the button is being read throughout the code, and if It reads the button has been pressed, the button tells it to the code attached to it.

def main():
    clear()
    display.set_pen(GREEN)
    display.text("Blink of an Eye", 30, 25, 240, 5)
    display.update()
    time.sleep(2)
    clear()
    screen = 1
    while True:
        while True:
            if button_b.is_pressed():
                clear()
                play()
                break
            elif button_a.is_pressed():
                clear()
                howToPlay()
                break
            else:
                display.set_pen(YELLOW)
                display.text('''How To Play:
Press 'A' ''', 15, 35, 240, 4)
                display.update()
                time.sleep(1)
                clear()
                display.set_pen(MAGENTA)
                display.text('''Play:
Press 'B' ''', 15, 35, 240, 4)
                display.update()
                time.sleep(1)
                clear()

Right now, the buttons only work at the start of the code (when the while statement restarts), which makes sense, but that isn't what I need it to do. I need it to constantly check the buttons and run the last bit of code simultaneously, so when the user presses the button while the code is running, it will do what the button says.

1

There are 1 answers

0
Peter I. On

There are some quirks in your code. I will go top to bottom: The line

screen = 1

can be omitted as "screen" is not used throughout the rest of the code. Maybe just a leftover from reducing the code, but anyways...

The next construct

    while True:
        while True:

is a cool one: you put an infinite loop around an infinite loop. Since the inner loop never terminates (at least during the runtime of the program) the outer loop will run exactly one time and never reach its end.

Then:

            if button_b.is_pressed():
                clear()
                play()
                break

What happens if you press button b? This "if" statement fires. Display is cleared and play is started. Then the while loop is terminated!? Ah, now I see why you used two infinity loops. Just use "continue" instead of "break" and you are fine. But moreover: Since there is no time.sleep() in here the loop is repeated immediately. I bet you are not fast enough to release the button in between. So you don't want to know if the button state is "pressed", because that happens for several loops in a row.

You better want to know if the state of the button changed from "not pressed" to "pressed":

    button_b_state = False
    # ... 
        while True:
            if button_b.is_pressed() and button_b_state == False:
                button_b_state = True # remember current state
                # ...
            else:
                button_b_state = False # remember current state

The same goes for button a. Just one question: why do you use "elif" for button a? That means you only want to know its state if button b is not pressed. What happens if I press both buttons at the same time?

And then the "else" part: This is in fact the user interface. Why put it behind an "else" wall? So it is only shown if no button is pressed. And why put the two texts in different colors at the same space on the display? In YELLOW and MAGENTA! I expect this combination to be difficult to focus on, especially when blinking at 2Hz.

HTH