Raspberry Pi-运行功能5秒
我试图让我的覆盆子Pi眨眼5秒钟,然后再进行5秒钟。因此,我有两个功能,一个用于眨眼我的红色LED,另一个用于蓝色的功能。我使用utime.sleep(0.5)
用于每半秒关闭LED的功能。
def blink_red():
RED.toggle()
BLUE.value(0)
utime.sleep(0.5)
def blink_blue():
BLUE.toggle()
RED.value(0)
utime.sleep(0.5)
为了执行代码,我使用utime.sleep(5)
,希望能使每个功能运行5秒钟,但是它不会使LED闪烁。它打开红色LED五秒钟,蓝色也打开5秒。
while True:
blink_red()
time.sleep(5)
blink_blue()
utime.sleep(5)
我需要更改我的代码的哪些部分,或者有更多的Pythonic方法可以做到这一点?
编辑:我正在运行Micropython
I'm trying to get my Raspberry Pi to blink a LED for 5 seconds, and then another one for 5 seconds as well. So I have two functions, one for blinking my red LED, and another for the blue one. I used utime.sleep(0.5)
for the functions to turn off the LEDs every half second.
def blink_red():
RED.toggle()
BLUE.value(0)
utime.sleep(0.5)
def blink_blue():
BLUE.toggle()
RED.value(0)
utime.sleep(0.5)
For the execution of the code, I made use of utime.sleep(5)
in hopes of getting each function to run for 5 seconds, however it doesn't make the LEDs blink. It turns the red led on for five seconds, and the blue one on for 5 seconds as well.
while True:
blink_red()
time.sleep(5)
blink_blue()
utime.sleep(5)
Which parts of my code do I need to change or is there a more pythonic way of doing this?
Edit: I am running micropython
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您的代码正在做您所要求的事情。
您的循环正在运行:
呼叫
blink_red()
:现在您回到循环中,然后调用
time.sleep(5)
。你看到这要去哪里了吗?
如果您想要
blink_red()
才能眨眼五秒钟,则需要类似:loop看起来像这样:(
假设您重新写下<<代码> blink_blue 。)
这是一个完整的程序;我正在使用ESP C3上的Micropython运行此操作(因此,它的语法可能与PI上的Micropopython略有不同,但是它应该在很大程度上相同):
It sounds like your code is doing what you've asked it do do.
Your loop is running:
When you call
blink_red()
:Now you're back in the loop, and you call
time.sleep(5)
.Do you see where this is going?
If you wanted
blink_red()
to blink the red LED for five seconds, you would need something like:And your
while
loop would look like this:(Assuming that you rewrote
blink_blue
as well.)Here's a complete program; I was running this using micropython on an ESP C3 (so it's possible it will have slightly different syntax than micropython on the Pi, but it should be largely the same):