首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >利用time.monotonic函数控制CircuitPython中的LED矩阵

利用time.monotonic函数控制CircuitPython中的LED矩阵
EN

Stack Overflow用户
提问于 2022-10-09 07:45:10
回答 1查看 32关注 0票数 0

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

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

发布于 2022-10-15 07:25:37

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

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74002956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档