0

I'm trying to use a button to scroll through messages on an LCD display. I got a kit from Amazon which has a whole bunch of buttons, sensors, LEDs, etc. It also came with code to do various projects, but I want to combine the button press with the display. I found various code online, and managed to put something together, but it's acting very strange. When I run it, the message on the display prints a few characters at a time-- very slow. Pauses, slowly prints more, etc., until the string has been printed. It seems to work, and pause between messages until I press the button, as it should, but the display speed of each message is super slow-- printing only a few characters at a time. I suspect it has something to do with a loop/check, constantly checking the button and slowing down the display, but not sure.

Here's my code (it's relatively short).

#!/usr/bin/env python2.7


import RPi.GPIO as GPIO
import LCD1602
import time

GPIO.setmode(GPIO.BCM)

Button = 17 # GPIO-24, pin 17
GPIO.setup(Button, GPIO.IN, pull_up_down=GPIO.PUD_UP)
presses = 0

# clear the display
LCD1602.init(0x27, 1)



def my_callback(Button):
    global presses
    # start
    if GPIO.input(Button) == 0:
        # write to terminal
        print "Button Pressed\t", presses
        if presses == 0:
         # write to terminal
         print "First!"
         # write to display
         LCD1602.init(0x27, 1)
         LCD1602.write(0, 0, 'Welcome')
         LCD1602.write(0, 1, 'press button')
        if presses == 1:
         # write to terminal
         print "Second!"
         # write to display
         LCD1602.init(0x27, 1)
         LCD1602.write(0, 0, 'You pressed button!')
         LCD1602.write(0, 1, '..press again.')
        if presses == 2:
         # write to terminal
         print "Third!"
         # write to display
         LCD1602.init(0x27, 1)
         LCD1602.write(0, 0, 'Pressed again!')
         LCD1602.write(0, 1, '(press button).')
        if presses == 3:
         # write to terminal
         print "Forth!"
         # write to display
         LCD1602.init(0x27, 1)
         LCD1602.write(0, 0, 'Scrolling works!')
         LCD1602.write(0, 1, 'press button')
        if presses == 4:
         # write to terminal
         print "Fifth!"
         # write to display
         LCD1602.init(0x27, 1)
         LCD1602.write(0, 0, 'Done!')
         LCD1602.write(0, 1, 'press button.')
        presses += 1
    return # not needed, just for clarity

GPIO.add_event_detect(Button, GPIO.FALLING, callback=my_callback)


try:
    print "Waiting"
    while True:
        pass # replace with any of your code

except KeyboardInterrupt:
    pass
finally:
    print "\nRelease our used channel(s)"
    GPIO.cleanup([Button])

def main():
    pass

if __name__ == '__main__':
    main()

I'm not very good at python, so I don't know the exact commands. But I'm sure the issue would be obvious to someone who knows something about python!

Thanks!

1

One obvious optimisation is to change

try:
    print "Waiting"
    while True:
        pass # replace with any of your code

to

try:
    print "Waiting"
    while True:
        time.sleep(1) # Give other code a chance to run.

At the moment you are doing what is called a "busy spin", consuming all the CPU time in your while loop for no purpose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.