我想打开和关闭一个带有一个按钮的LED(Micropython Raspberry Pi Pico)
大家好,您能帮我完成我的项目:
我想以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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您编写代码的方式,有效地具有
,而
循环抗击IRQ例程。如果您只希望两个按钮像切换开关一样,最简单的解决方案可能就是这样:我已经在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: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 theswitches on my board are connected to ground (so pressing a switch
brings the corresponding pin to logic
0
).