Hello everybody!
I have only been working with python for about 4 days now, so i know my skillset is “somewhat” lackluster, but i fail to grasp what im doing wrong here…
I have a raspbery pico running a version of something called circuitpython.
I have 2 files; main.py and myDisplay.py.
some pseudo-code: (big chunks of code is missing to try to keep the “pseudo”-code as short as possible, but i think my error should be here somewhere…)
(main.py)
from myDisplay import doButtons
from myDisplay import doDisplay
buttonInterval = 0.1
displayInterval = 2.5
while True:
counter = counter + 1
doButtons()
if counter > (displayInterval / buttonInterval):
doDisplay()
counter = 0
time.sleep(buttonInterval)
(myDisplay.py)
BackLight_Val = 0.5
......
def doButtons(): #while True:
if button_a.read(): # if a button press is detected then...
if BackLight_Val < 0.9:
BackLight_Val = BackLight_Val + 0.1
print("MoreBrightness:")
print(BackLight_Val)
else:
print("MaxedBrightness:")
display.set_backlight(BackLight_Val)
if button_b.read():
if BackLight_Val > 0.11:
BackLight_Val = BackLight_Val - 0.1
print("LessBrightness:")
print(BackLight_Val)
else:
BackLight_Val = 0.1
print("NoBrightness:")
display.set_backlight(BackLight_Val)
......
The error im getting is:
Traceback (most recent call last):
File “”, line 15, in
File “myDisplay.py”, line 61, in doButtons
NameError: local variable referenced before assignment
The module error line number corresponds with the function call for the doButtons function
The doButtons line number corresponds with the first time i use the BackLight_val variable inside a function.
I have been working with C for the past deckade (Arduino), so this feels like a namespace issue, but my logic dictates that the variable name is already “global” (inside that particular “module” named myDisplay"), since its declared before the function.
Ive been trying to get around this issue with no success, the rest of my program does work fine, but it crashes when i try to use a button to modify the backlight intensity.