我想打开和关闭一个带有一个按钮的LED(Micropython Raspberry Pi Pico)

发布于 2025-01-26 05:18:10 字数 1673 浏览 2 评论 0原文

大家好,您能帮我完成我的项目:

我想以2种模式打开LED: 1个按钮一个应该开始的,5秒钟后,它应该关闭 2带按两个按钮,它应该打开并待在,然后如果按下按钮2,我希望将其关闭。 这是我的代码,我知道我应该比较不同的状态,但是我不明白,我可以使用另一个按钮,但是我喜欢学习如何使用内存。

from machine import Pin, Timer
import time
White_LED = Pin(15, Pin.OUT)
button1 = Pin(14, Pin.IN, Pin.PULL_DOWN) 
button2 = Pin(13, Pin.IN, Pin.PULL_DOWN)
def func(pin):
    if button2.value() == 1:
        White_LED.on()
    
while True:
    button2.irq(func)
    if button1.value() == 1:
        White_LED.on()
        time.sleep(5)
        White_LED.off()

我现在可以通过两个功能来解决o解决它,但是问题是按钮2不会像应有的快速反应,我必须将其推几次以转动LED,

from machine import Pin, Timer
import time

White_LED = Pin(15, Pin.OUT)
Blue_LED = Pin(16,Pin.OUT) 

button1 = Pin(14, Pin.IN, Pin.PULL_DOWN) 
button2 = Pin(13, Pin.IN, Pin.PULL_DOWN)


def func(pin):
    if button2.value() == 1 & White_LED.value()== 0:
        White_LED.on()
    
def func2(pin):
    if button2.value() == 1 & White_LED.value()== 1:
        White_LED.off()

while True:
    button2.irq(func)
    button2.irq(func2)
    if button1.value() == 1:
        White_LED.on()
        time.sleep(5)
        White_LED.off()

我做了,我做到了,看来它正在工作:

from machine import Pin, Timer
import time

White_LED = Pin(15, Pin.OUT) 

button1 = Pin(14, Pin.IN, Pin.PULL_DOWN) 
button2 = Pin(13, Pin.IN, Pin.PULL_DOWN)




while True:
    if button1.value() == 1:
        White_LED.on()
        time.sleep(5)
        White_LED.off()
    elif button2.value() == 1:
        if White_LED.value()==0:
            time.sleep(2)
            White_LED.on()
        else:
            time.sleep(2)
            White_LED.off()

Guys Hi could you please help me with my project:

I want to turn on an LED with 2 modes:
1-with button one it should starts and after 5 second it should tun off
2-with button two, it should turn on and stays ON and then if I push Button 2 I want it to be turned off.
here is my code, I know I should compare different states but I don't understand it, I can use another button, but I like to learn how to use memory.

from machine import Pin, Timer
import time
White_LED = Pin(15, Pin.OUT)
button1 = Pin(14, Pin.IN, Pin.PULL_DOWN) 
button2 = Pin(13, Pin.IN, Pin.PULL_DOWN)
def func(pin):
    if button2.value() == 1:
        White_LED.on()
    
while True:
    button2.irq(func)
    if button1.value() == 1:
        White_LED.on()
        time.sleep(5)
        White_LED.off()

I could manage o solve it with two functions now, but the problem is that the button2 won't react as fast as it should and I have to push it couple of times to turn the LED, ON and OFF

from machine import Pin, Timer
import time

White_LED = Pin(15, Pin.OUT)
Blue_LED = Pin(16,Pin.OUT) 

button1 = Pin(14, Pin.IN, Pin.PULL_DOWN) 
button2 = Pin(13, Pin.IN, Pin.PULL_DOWN)


def func(pin):
    if button2.value() == 1 & White_LED.value()== 0:
        White_LED.on()
    
def func2(pin):
    if button2.value() == 1 & White_LED.value()== 1:
        White_LED.off()

while True:
    button2.irq(func)
    button2.irq(func2)
    if button1.value() == 1:
        White_LED.on()
        time.sleep(5)
        White_LED.off()

I did this and it seems it's working:

from machine import Pin, Timer
import time

White_LED = Pin(15, Pin.OUT) 

button1 = Pin(14, Pin.IN, Pin.PULL_DOWN) 
button2 = Pin(13, Pin.IN, Pin.PULL_DOWN)




while True:
    if button1.value() == 1:
        White_LED.on()
        time.sleep(5)
        White_LED.off()
    elif button2.value() == 1:
        if White_LED.value()==0:
            time.sleep(2)
            White_LED.on()
        else:
            time.sleep(2)
            White_LED.off()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想你的星星会说话 2025-02-02 05:18:10

您编写代码的方式,有效地具有,而循环抗击IRQ例程。如果您只希望两个按钮像切换开关一样,最简单的解决方案可能就是这样:

from machine import Pin

LED = Pin(2, Pin.OUT)
button1 = Pin(14, Pin.IN, Pin.PULL_UP)
button2 = Pin(12, Pin.IN, Pin.PULL_UP)

while True:
    if LED() and not button1():
        print("ON")
        LED(0)
    elif not LED() and not button2():
        print("OFF")
        LED(1)

我已经在WEMOS D1 Mini运行Micropython v1.18上测试了此代码。

请注意,我已将按钮配置为pin.pull_up,并且
板上的开关连接到地面(因此按开关
将相应的引脚带到逻辑0)。


The way you've written your code, you effectively have your while loop fighting your IRQ routines. If you just want the two buttons to act like toggle switches, the easiest solution is probably something like this:

from machine import Pin

LED = Pin(2, Pin.OUT)
button1 = Pin(14, Pin.IN, Pin.PULL_UP)
button2 = Pin(12, Pin.IN, Pin.PULL_UP)

while True:
    if LED() and not button1():
        print("ON")
        LED(0)
    elif not LED() and not button2():
        print("OFF")
        LED(1)

I've tested this code on Wemos D1 Mini running micropython v1.18.

Note that I've configured the buttons as Pin.PULL_UP, and the
switches on my board are connected to ground (so pressing a switch
brings the corresponding pin to logic 0).


enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文