我很难用time.donitric((让一组LED每隔半秒打开一次,每隔半秒重复关闭一次。这些LED通过I2C与矩阵驱动板连接,而不是Raspberry Pi Pico上的GPIO引脚。我如何修改下面的示例代码使其工作,因为我有两个定义为lead.on((和lead.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
仔细想想,答案很简单。只是需要休息一下。
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