Hi,
I am a beginner and I was hoping you could help me.
This traffic light code will only work once after pressing the button. It does not seem to reset.
Here is the code I am using :
import machine
import utime
import _thread
led_red = machine.Pin(15, machine.Pin.OUT)
led_amber = machine.Pin(14, machine.Pin.OUT)
led_green = machine.Pin(13, machine.Pin.OUT)
button = machine.Pin(16, machine.Pin.IN, machine.Pin.PULL_DOWN)
buzzer = machine.Pin(12, machine.Pin.OUT)
global button_pressed
button_pressed = False
def button_reader_thread():
global button_pressed
while True:
if button.value() == 1:
button_pressed = True
_thread.start_new_thread(button_reader_thread, ())
while True:
if button_pressed == True:
led_red.value(1)
for i in range(10):
buzzer.value(1)
utime.sleep(0.2)
buzzer.value(0)
utime.sleep(0.2)
global button_pressed
button_pressed = False
led_red.value(1)
utime.sleep(5)
led_amber.value(1)
utime.sleep(2)
led_red.value(0)
led_amber.value(0)
led_green.value(1)
utime.sleep(5)
led_green.value(0)
led_amber.value(1)
utime.sleep(5)
led_amber.value(0)
The Thony interpreter says:
Unhandled exception in thread started by <function button_reader_thread at 0x20008b90>
Traceback (most recent call last):
File āā, line 17, in button_reader_thread
NameError: name ābuttonā isnāt defined
Could you tell me why the switch is not resetting the buzzer?
Many thanks,
steve