【问题标题】:Using time.monotonic function to control an LED matrix in CircuitPython在 CircuitPython 中使用 time.monotonic 函数控制 LED 矩阵
【发布时间】:2022-10-15 15:25:37
【问题描述】:

我无法使用 time.monotonic() 让一组 LED 每半秒打开一次并重复每半秒关闭一次。这些 LED 通过 I2C 与矩阵驱动板连接,而不是 Raspberry Pi Pico 上的 GPIO 引脚。我如何修改下面的示例代码以使其工作,因为我有两个函数定义为 led.on() 和 led.off() 假设 i2c 接口已创建

import time
import digitalio
import board

# How long we want the LED to stay on
BLINK_ON_DURATION = 0.5

# How long we want the LED to stay off
BLINK_OFF_DURATION = 0.5

# When we last changed the LED state
LAST_BLINK_TIME = -1

# Setup the LED pin.
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT

while True:
  # Store the current time to refer to later.
  now = time.monotonic()
  if not led.value:
      # Is it time to turn on?
      if now >= LAST_BLINK_TIME + BLINK_OFF_DURATION:
          led.value = True
          LAST_BLINK_TIME = now
  if led.value:
      # Is it time to turn off?
      if now >= LAST_BLINK_TIME + BLINK_ON_DURATION:
          led.value = False
          LAST_BLINK_TIME = now

【问题讨论】:

  • 上面的代码有什么问题?您是否遇到错误,或者它的行为不符合预期?
  • 我不得不走开,想了一会儿。我想多了,只需要重写一些代码以使其适合我。

标签: python time adafruit-circuitpython


【解决方案1】:

想了想,答案很简单。只是需要休息一下。

initial = time.monotonic()
while True:
     now = time.monotonic()
     if  now - initial > 0 and now - initial < 0.5:        
         sec_on()
     if now - initial > 0.5 and now - initial <1:
        sec_off()
     if now - initial > 1:
        initial = now

【讨论】:

    猜你喜欢
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多