我正在raspberry pi上运行python脚本
基本上,我希望相机每5秒拍一张照片,但前提是我将布尔值设置为true,并在物理按钮上切换
最初我将其设置为true,然后在while(true)循环中,我想检查变量是否设置为true,如果设置为true,则每5秒开始拍照。问题是,如果我使用类似time time.sleep(5)
的东西,它实际上冻结了一切,包括支票。再加上我对按钮使用去抖动,我就不可能真正切换脚本,因为我必须在5秒的等待时间后按下它,正好用于值检查。。。我一直在四处寻找,我认为可能的解决方案应该包括线程,但我不能对此束手无策。我想到的一种解决方法是查看系统时间,如果秒数是5的倍数,则拍照(全部在主循环内)。这似乎有点粗略
脚本如下:
### Imports
from goprocam import GoProCamera, constants
import board
import digitalio
from adafruit_debouncer import Debouncer
import os
import shutil
import time
### GoPro settings
goproCamera = GoProCamera.GoPro()
### Button settings
pin = digitalio.DigitalInOut(board.D12)
pin.direction = digitalio.Direction.INPUT
pin.pull = digitalio.Pull.UP
switch = Debouncer(pin, interval=0.1)
save = False #this is the variable
while(True):
switch.update()
if switch.fell:
print("Pressed, toggling value")
save = not save
if save:
goproCamera.take_photo()
goproCamera.downloadLastMedia()
time.sleep(5)
干杯! 也许是这样的
我们在主循环中调用上面的函数
将while循环内容包装到函数上:
现在,我们创建一个最初设置为False的标志,以避免创建多个线程。 注意,我在while循环之前做了这个。我们只需要一个起始值
让我知道它是否有效。=)
这里有一些东西可以尝试:
根据您想要的具体行为类型,您可能需要调整调用
time.time()
的时间和频率我最终做了什么:
相关问题 更多 >
编程相关推荐