0

I was following the book called Get Started with MicroPython on Raspberry Pi Pico and i succesfully made it to the 65th page but i cannot continue.

I wrote the code just like in the book (i use VS Code and the Pico-W-Go extension not the Thonny IDE but i hadn't have any issues so far with it) but when i upload it or just simply run it on the Pico, the REPL freezes, the button for the pedestrian system doesn't work and i have to nuke.uf2 the pico and reflash before i can use it again. Everything other works fine, i read that multithreading is crap in uPython and my case is happened to others but i'm here for a solution. Anyone has even solved this or know other multithreading modes in uPython for Pico?

Also i got the Pylance error/warning "button_pressed" is assigned before global declaration Pylance for line 40 but it should be good code.

I knew C, but i want to play with other langauges too so i don't want to switch. :)

import machine
import utime
import _thread
#library for multithreading to work

led_red = machine.Pin(15, machine.Pin.OUT)
led_yellow = machine.Pin(14, machine.Pin.OUT)
led_green = machine.Pin(13, machine.Pin.OUT)
button = machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_DOWN)
led_pedestrianAllow = machine.Pin(25, machine.Pin.OUT)

global button_pressed
#global variable for multithreading
button_pressed = False

def button_reader_thread():
    #defines the thread on the second core
    global button_pressed
    #allows the global variable to be written to and not only read it
    while True:
        if button.value() == 1:
            button_pressed == True
        utime.sleep(0.01)

_thread.start_new_thread(button_reader_thread, ())
#the thread need to be started manually

led_red.value(0)
led_yellow.value(0)
led_green.value(0)

while True:
    if button_pressed == True:
        led_red.value(1)
        for i in range(10):
            led_pedestrianAllow.value(1)
            utime.sleep(0.2)
            led_pedestrianAllow.value(0)
            utime.sleep(0.2)
        global button_pressed
        button_pressed = False
    led_red.value(1)
    utime.sleep(5)
    led_yellow.value(1)
    utime.sleep(5)
    led_red.value(0)
    led_yellow.value(0)
    led_green.value(1)
    utime.sleep(5)
    led_green.value(0)
    led_yellow.value(1)
    utime.sleep(5)
    led_yellow.value(0)

Only thing i changed is the buzzer for the built in led because it was annoying.

bcseh202
  • 9
  • 5

0 Answers0