Raspberry Pi-运行功能5秒

发布于 2025-02-03 13:17:04 字数 542 浏览 1 评论 0原文

我试图让我的覆盆子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 技术交流群。

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

发布评论

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

评论(1

二手情话 2025-02-10 13:17:04

它打开红色的LED五秒钟(而蓝色的LED关闭),然后打开蓝色的LED 5秒(而红色也关闭)。

听起来您的代码正在做您所要求的事情。

  1. 您的循环正在运行:

      blink_red()
      时间。
      blink_blue()
      utime.sleep(5)
     
  2. 呼叫blink_red()

     ##您切换红色LED(一次)
      red.toggle()
    
      #您关闭蓝色LED
      蓝色。价值(0)
    
      #你等待0.5秒
      utime.sleep(0.5)
    
      #...然后您返回主循环
     
  3. 现在您回到循环中,然后调用time.sleep(5)

你看到这要去哪里了吗?

如果您想要blink_red()才能眨眼五秒钟,则需要类似:

def blink_red():
    BLUE.off()
    for i in range(5):
        RED.toggle()
        utime.sleep(0.5)
        RED.toggle()
        utime.sleep(0.5)

loop看起来像这样:(

while True:
  blink_red()
  blink_blue()

假设您重新写下<<代码> blink_blue 。)


这是一个完整的程序;我正在使用ESP C3上的Micropython运行此操作(因此,它的语法可能与PI上的Micropopython略有不同,但是它应该在很大程度上相同):

try:
    import time
except ImportError:
    import utime as time

from machine import Pin


RED = Pin(3, Pin.OUT)
BLUE = Pin(5, Pin.OUT)

PINS = [RED, BLUE]


def blink_pin(pin):
    for i in range(5):
        pin.on()
        time.sleep(0.5)
        pin.off()
        time.sleep(0.5)


# ensure everything is off to start
for pin in PINS:
    pin.off()

while True:
    blink_pin(RED)
    blink_pin(BLUE)

It turns the red led on for five seconds (while the blue one is off), and then turns the blue one on for 5 seconds (while red is off as well).

It sounds like your code is doing what you've asked it do do.

  1. Your loop is running:

      blink_red()
      time.sleep(5)
      blink_blue()
      utime.sleep(5)
    
  2. When you call blink_red():

      # you toggle the RED LED (once)
      RED.toggle()
    
      # you turn off the blue LED
      BLUE.value(0)
    
      # you wait 0.5 seconds
      utime.sleep(0.5)
    
      # ...and then you return to the main loop
    
  3. 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:

def blink_red():
    BLUE.off()
    for i in range(5):
        RED.toggle()
        utime.sleep(0.5)
        RED.toggle()
        utime.sleep(0.5)

And your while loop would look like this:

while True:
  blink_red()
  blink_blue()

(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):

try:
    import time
except ImportError:
    import utime as time

from machine import Pin


RED = Pin(3, Pin.OUT)
BLUE = Pin(5, Pin.OUT)

PINS = [RED, BLUE]


def blink_pin(pin):
    for i in range(5):
        pin.on()
        time.sleep(0.5)
        pin.off()
        time.sleep(0.5)


# ensure everything is off to start
for pin in PINS:
    pin.off()

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