I am receiving the following error from the code below.
I have marked this in the code, see: if global_bPbPushed == True: #line 44, error occurs here***
Any help would be appreciated.
James
========================= RESTART =========================
>>> %Run switches.py
Traceback (most recent call last):
File "/home/james/Public/mpProjects/Project_4/switches.py", line 66, in <module>
File "/home/james/Public/mpProjects/Project_4/switches.py", line 44, in mainloop
NameError: local variable referenced before assignment
Code: Select all
#switches.py, Project_4
#Micropython and Pyboard V1.1
#Date August 29, 2021
import micropython
import machine
import pyb
from pyb import Pin
micropython.alloc_emergency_exception_buf(100)
#for identifying pyboard LEDs
red = 1
green = 2
yellow = 3
blue = 4
#setup push buttons
pb1 = Pin('X1', Pin. IN, Pin.PULL_UP)
pb2 = Pin('X2', Pin.IN, Pin.PULL_UP)
#setup variables
global global_nNumPbPushed
global global_bPbPushed
global_bPbPushed = False
global_nNumPbPushed = None
#ISR for push buttons
#note that pin is type int
def pbChanged(pin):
print('Pin changed = ', pin)
global global_bPbPushed
global_bPbPushed = True
#main loop
def mainloop():
while True:
if global_bPbPushed == True: #line 44, error occurs here***
state = machine.disable_irq() #disable all interrupts
if global_nNumPbPushed == 0:
pyb.LED(green).on()
elif global_nNumPbPushed == 1:
pyb.LED(blue).on()
else: #neither switch pressed so do nothing
pass
machine.enable_irq(state) #re-enable all interrupts to previous state
else:
pass
global_bPbPushed = False
global_nNumPbPushed = None
#run main loop
mainloop()