Quantcast
Viewing all articles
Browse latest Browse all 4861

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

Thanks @hippy for the explanation of flags. Your code works for me as a stand alone program that quickly spams the shell with "Button push detected" for as long as I have the button pressed. I tried plugging it into my example traffic lights code but the pico hangs at moment of button press with no error posted in the shell and the lights stop changing. I have to unplug and replug in the pico to make any changes after that. I already had button called out on pin 16 with the PULL_DOWN so I didn't change my line and left it with the machine.Pin nomenclature. Not sure if I should have done that. I had to put the line "from machine import Pin" for some reason I don't understand 'cause I already had the entire machine library imported. Anyway, this is how I changed the code, is this how you ment for me to try your interrupt request?

Code:

[import machinefrom machine import Pinimport utimeled_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 ButtonPushedHandler(self):    global button_pressed    while True:        utime.sleep(0.1)        if button.value() == 1:            button_pressed = True        button.irq(trigger=Pin.IRQ_RISING, handler=ButtonPushedHandler)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)/code]

Statistics: Posted by Rueful-Pi — Sun May 05, 2024 8:26 pm



Viewing all articles
Browse latest Browse all 4861

Trending Articles