Micro python code for traffic lights using raspberry pi pico

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

1 Like

This thread suggests a solution to the particular error message.

1 Like

Hi Stephen,

What was your code looking to do?
Would it be possible to attach a drawing of how you would like it to work?

Hi Stephen

Without the hardware it is not possible to replicate the fault but here are some suggestions.
The following shortens and simplifies the code.

from machine import Pin
from time import sleep

It is always a good idea to initialize outputs before the loop starts e.g.

led_red.value(1)

Print statements can help diagnose faults e.g.

print("button pressed")

Try this

from machine import Pin
from time import sleep

led_red = Pin(15, Pin.OUT)
led_amber = Pin(14, Pin.OUT)
led_green = Pin(13, Pin.OUT)
button = Pin(16, Pin.IN, Pin.PULL_DOWN)
buzzer = Pin(12, Pin.OUT)

# initialize outputs
led_red.value(1)
led_amber.value(0)
led_green.value(0)
buzzer.value(0)

while True:
    led_red.value(1) # start with red
    print("begin loop with red")
    if button.value():
        print("button pressed")
        for i in range(10):
            buzzer.value(1)
            sleep(0.2)
            buzzer.value(0)
            sleep(0.2)
    sleep(5)
    led_amber.value(1)
    print("red + amber")
    sleep(2)
    led_red.value(0)
    led_amber.value(0)
    led_green.value(1)
    print("green")
    sleep(5)
    led_green.value(0)
    led_amber.value(1)
    print("amber???")
    sleep(5)
    led_amber.value(0)
1 Like

Hi Steve,

In your button_reader_thread function you have globalā€™d the button_pressed var, but you have not globalā€™d the button var as well, hence the error: NameError: name ā€˜buttonā€™ isnā€™t defined.

Try this:

def button_reader_thread():
    global button  # <-- Add this line
    global button_pressed
    # ... rest of code