Question

So I have this script for controlling an LCD plate where I want the backlight to change every time you press a button, but also want to do other things as well. Currently this doesn't work and it doesn't display the messages I want it to.

This is the change backlight bit of code:

while True:
    for b in btn:
        if lcd.buttonPressed(b):
            lcd.backlight(col[randint(0,5)])

And then I want to go on and run a bit of code that prints a string to the lcd and stuff, in this way:

lcd.message("This is a string")

but the script doesn't ever print the string, it just stays on the backlight change-y bit.

Basically I'm creating an index of letters that you can move through using the LCD buttons, and want the backlight to change each time you press a button.

Was it helpful?

Solution

The standard way is to have a single loop containing both. In the loop you first handles any input or state changes. Then at the end of your loop you update display. This is a fairly common paradigm in game programming see pygame for tons of examples. You would then only break once your program is terminating. One thing worth pointing out is that you would not want to block on checking for key press (or any input), otherwise you would hold up your display waiting for input.

sudo code would be something like:

while True:
    for event in key_presses():
        handle_event(event)  # stuff that happens as a result of input
    update_state()  # stuff that happens regardless of input
    update_display()  # everything that changes the display (backlight, text, anything)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top