我正在使用树莓派,pi脸和python脚本来监控几个家庭传感器。我想将烟雾探测器的传感线添加到该列表中,但我需要if语句的一些帮助。
我不确定如何判断if语句以检查输入检测到信号的时间。在4秒内忽略(低电池唧唧声),超过4秒(检测到烟雾)警告我..
基本上我需要帮助编写下面的if语句。
if piface.digital_read(0)==0 >= 4 seconds:
# do x
else:
# do y
我需要一个循环吗?它可以像我上面那样简单吗? (当然编码正确!)
答案 0 :(得分:1)
像这样(未经测试的伪代码):
counter = 0
while True: #your main loop
smoke = digital_read() #assume 0 = no alarm, 1 = alarm
if smoke:
counter += 1
else:
counter = 0
if counter >= 4: #there was smoke for the last 4 seconds
call_the_fire_brigade()
time.sleep(1) #wait one second
我猜你可能需要一些循环。
答案 1 :(得分:0)
嗯,我认为一个好的解决方案是为每个探测器生成一个单独的线程,然后使用一个阻塞来获取一个循环的数字..比如
count = 0
while count < 4:
if piface.digital_read(0) == 0:
count += 1
else: count = 0
sleep(4000)
# ... rest of code ...