我很难用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
发布于 2022-10-15 07:25:37
想一想,答案很简单。只是需要休息一下。
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
https://stackoverflow.com/questions/74002956
复制相似问题