python脚本中的反向事件触发器

时间:2015-10-24 21:55:27

标签: python

我有一个throw开关,它在打开时触发下面脚本的相关输出,但在关闭时什么都不做。我希望它做相反的事情,即它在关闭时触发相关的输出,但是在打开时不会触发相关的输出,但是已经玩了各种参数无法让它做我想做的事情。

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)

1 个答案:

答案 0 :(得分:0)

在检查door_open()后,您是否尝试过这样的内容:

if (prev_input and (not input)):
    door_closed()

当然,您需要实施door_closed()