英文:
How can I increment a count with IRQs and light an LED using Micropython on Raspberry Pi Pico?
问题
如何使用 Micropython 使用 IRQ,计数到特定值,然后当达到该特定值时点亮 LED。
我正在使用 Micropython 和 Thonny 以及树莓派 Pico,不过这不重要,但我的操作系统是 Ubuntu 22.04 现在针对我的问题。
我已经将引脚14设置为对上升的 IRQ 做出反应,然后在这段时间内它打印“button pushed”。现在我该如何让由按钮按下引起的这些上升的 IRQ 递增到给定的计数,当达到该计数时,使引脚13变高以点亮 LED?我已经设置好引脚14,它正在工作。我已经设置好了引脚13并连接了一个 LED,它也在工作。但我是新手,正在尝试将 IRQ 和计数(达到4)结合起来,使引脚13变高,但这使我感到困惑。咖啡没有帮助...
以下是我目前的代码...
import machine
import utime
button = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN)
led = machine.Pin(13, machine.Pin.OUT)
count = 0
def button_handler(pin):
utime.sleep_ms(100)
if pin.value():
print("button pushed")
print(count)
led.toggle()
button.irq(trigger=machine.Pin.IRQ_RISING, handler=button_handler)
英文:
How to have micropython using an IRQ, count to a certain value then when equaling that certain value light a led
I am using micropython with Thonny and a Raspberry Pi Pico and not that it matters but my OS is Ubuntu 22.04 now for my question.
I have pin14 setup to react to a rising IRQ upon which time it prints "button pushed". Now how would I have those rising IRQ's caused by a button push increment to a given count and when that count is reached have pin13 go high to light an LED? I have pin14 setup and its working I have pin13 setup with a led and its working. But i am new and trying to tie together an IRQ and a Count up to (4) to make pin13 go high but it is eluding me. Coffee isnt helping....
Following is what I currently have...
import machine
import utime
button = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN)
led = machine.Pin(13, machine.Pin.OUT)
count = 0
def button_handler(pin):
utime.sleep_ms(100)
if pin.value():
print("button pushed")
print(count)
led.toggle()
button.irq(trigger=machine.Pin.IRQ_RISING, handler=button_handler)
-Brian-
答案1
得分: 1
First, you need to tell your handler that you want to use the global variable "count". Then, you have to modify it when the interrupt occurs.
An interrupt service routine (ISR) should be as fast as possible, so don't put a delay in there.
You can also omit the check whether the button is pressed. The ISR is triggered because of the button press, so no need to verify that again.
def button_handler(pin):
global count # declare the count variable as global
print("button pushed")
count += 1 # modify the global counter
print(count)
if count > 3 :
led.toggle()
count = 0 # don't forget to reset the counter!
英文:
First, you need to tell your handler that you want to use the global variable "count". Then, you have to modify it when the interrupt occurs.
An interrupt service routine (ISR) should be as fast as possible, so don't put a delay in there.
You can also omit the check wether the button is pressed. The ISR is triggered because of the button press, so no need to verify that again.
def button_handler(pin):
global count # declare the count variable as global
print("button pushed")
count += 1 # modify the global counter
print(count)
if count > 3 :
led.toggle()
count = 0 # dont't forget to reset the counter!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论