Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4959

MicroPython • Traffic_Lights.py example in Get started with MicroPython on Raspberry Pi Pico

$
0
0
After completing the example code for Taffic Lights Controler (chapt 5) the book states on page 66, "Let the traffic lights loop through their cycle a few more times, then press the button again to trigger another crossing." Once I press the button, when the buzzer sounds I get an error

Code:

MPY: soft rebootUnhandled exception in thread started by <function button_reader_thread at 0x200076d0>Traceback (most recent call last):  File "<stdin>", line 17, in button_reader_threadNameError: name 'button' isn't defined
After this the button isn't recognised again until I restart Thonny and unplug and replug in the pico.

Here's the code I have loaded onto the Pico. Is there any error in this code? I've checked it against the example code over and over and I don't see anything out of place. Thanks for your help in advance.

Code:

import machineimport utimeimport _threadled_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_pressedbutton_pressed = Falsedef 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)

Statistics: Posted by Rueful-Pi — Sat May 04, 2024 10:05 pm



Viewing all articles
Browse latest Browse all 4959

Trending Articles