I am trying to display a number on a Dot Matrix ltp305 display that increases with each button press, enabled by the pressing of a normal button connected to the GPIO. I am sure everything is correctly wired, as both the button and the display work fine on other programs. I am using a Raspberry Pi Zero WH with a breadboard. Here is my code:
import RPi.GPIO as GPIO
from ltp305 import LTP305
import threading
number = 0
left = 0
right = 0
display = LTP305(address=0x61)
display.clear()
display.show()
def signal_handler(signal, frame):
print('Exiting!')
GPIO.cleanup()
sys.exit(0)
def button_callback(channel):
print('Button was pushed! Adding to counter!')
global number
number = number + 1
if number == 99:
number = 0
writenumber = str(number)
global left
global right
if len(writenumber) == 2:
res_first = test_str[0:len(test_str)//2]
res_second = test_str[len(test_str)//2]
else:
left = 0
right = writenumber
left = int(left)
right = int(right)
global display
display.set_character(0, left)
display.set_character(5, right)
display.show()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback)
forever = threading.Event()
forever.wait()
Here is the error message:
Traceback (most recent call last):
File "/home/pi/Code/GPIO/Pin1-10_DotMatrixCounter.py", line 36, in button_callback
display.set_character(0, left)
File "/usr/local/lib/python3.7/dist-packages/ltp305/__init__.py", line 127, in set_character
char = font[char]
KeyError: 0
I have checked multiple other forums and documentation, but there seems to be nobody who has had the same error message in the same context as me. The only other times I have found KeyError in a forum post is when that error is in their code, and not in the code of a package. A strange observation I found is that all other 'display.set_character()' lines were in a loop, but when I tried that it made no difference. Maybe it is because it is in a defined command.
The button is connected to 3v3 and GPIO15, and the Dot Matrix breakout is connected to the other 3v3, GPIO2, GPIO3, GPIO4 and Ground, as it would usually be if directly attached to the Pi.
left
is defined and may not be initialised as you have anif len(writenumber) == 2
condition that does not set left to anything. I cannot see what you are doing withres_first
orres_second
- copy paste error??? Odd though, I would have thought the int may have errored!