[英]reverse event trigger in python script
I have a throw switch which triggers the relevant output from the script below when open but does nothing when it is closed. 我有一个throw开关,该开关在打开时会触发下面脚本的相关输出,但是在关闭时什么都不做。 I would like it to do the opposite ie it triggers the relevant output when closed but not when open but having played around with the various parameters cannot get it to do what I want.
我希望它做相反的事情,即它在关闭时触发相关的输出,但在打开时不触发,但是经过各种参数的尝试后,它无法执行我想要的操作。
import RPi.GPIO as GPIO
import smtplib
import time
import datetime
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# function for the door opening
def door_open():
print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
#initialise a previous input variable to 0 (assume button not pressed last)
prev_input = 0
while True:
#take a reading
input = GPIO.input(21)
#if the last reading was low and this one high, print
if ((not prev_input) and input):
door_open()
#update previous input
prev_input = input
#slight pause to debounce
time.sleep(0.1)
Have you tried putting something like this after the check for door_open()
: 您是否尝试过在检查
door_open()
之后放入类似内容:
if (prev_input and (not input)):
door_closed()
You'll need to implement door_closed()
, of course. 当然,您需要实现
door_closed()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.