简体   繁体   English

试图创建一个计时器来暂停 python 中的循环

[英]Trying to create a timer to pause a loop in python

Here I go messing with timers and motion sensors again.在这里,我 go 再次弄乱计时器和运动传感器。

I have a PIR motion sensor connected to a raspberry pi.我有一个 PIR 运动传感器连接到树莓派。 I want to have the motion sensor have a cooldown of one minute before it checks for motion again.我想让运动传感器在再次检查运动之前有一分钟的冷却时间。 I've been messing around with threading some... but I don't think I need that for this and would like to keep it simple.我一直在搞一些穿线......但我认为我不需要这样做并且希望保持简单。

GPIO.setmode(GPIO.BCM)
PIR_PIN = 11
GPIO.setup(PIR_PIN, GPIO.IN)

motion_cooldown = 60
start = 0

while True:
    if(time.time() < start + motion_cooldown):
        print("cooldown")
    else: 
        if PIR_PIN == 1:
            print("motion detected")
            #do some stuff
            start = time.time()
        elif PIR_PIN == 0:
            print("no motion")
            #do some stuff
            start = 0         #reset start to 0 so the loop continues

The current error I'm getting is " '<' not supported between instances of 'builtin_function_or_method and int" I assume that means I can't compare an int and the time.time() but I swear of done similar before and it worked fine.我得到的当前错误是“'builtin_function_or_method 和 int 的实例之间不支持'<'”我认为这意味着我无法比较 int 和 time.time() 但我发誓之前做过类似的事情并且它有效美好的。 Any suggestions welcome!欢迎任何建议!

This is likely to happen if you have not called the function, that is when you do something like start = time.time , but your code does not do that, and it is not reproducible.如果您没有调用 function,则可能会发生这种情况,也就是说,当您执行类似start = time.time的操作时,但您的代码不会那样做,而且它不可重现。 For pausing the loop you can use time.sleep(60) at the end of the while loop要暂停循环,您可以在while循环结束时使用time.sleep(60)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM