RPI python time.sleep导致GPIO上的错误事件检测

时间:2017-07-11 22:22:05

标签: python raspberry-pi gpio

我已经注意到这段代码,我已经将它们放在一起进行测试如果任何EMI影响了我的输入。每次我启动我第一次运行这个脚本时会得到一个错误的假设。

#!/usr/bin/python


import RPi.GPIO as GPIO
from time import sleep

GPIO.setwarnings(False)

out1 = 17
out2 = 27
in1 = 26
in2 = 23

GPIO.setmode(GPIO.BCM)
GPIO.setup(out1, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(out2, GPIO.OUT, initial=GPIO.HIGH)

GPIO.setup(in1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(in2, GPIO.IN, pull_up_down=GPIO.PUD_UP)


def read2(channel):
    print "GPIO 2"


def read20(channel):
    print "Button"


def read24(channel):
    print "Switch"


if __name__ == '__main__':

    print "start"

    sleep(1)
    print "waited"

    while True:
        print GPIO.input(out1)
        if GPIO.input(out1) == 0:
            print("LOW")
            GPIO.remove_event_detect(in1)
            GPIO.remove_event_detect(in2)

            GPIO.output(out1, GPIO.LOW)
            GPIO.output(out2, GPIO.LOW)

            print "one1"
            GPIO.add_event_detect(in1, GPIO.BOTH, callback=read2, bouncetime=300)
            GPIO.add_event_detect(in2, GPIO.BOTH, callback=read24, bouncetime=300)
            print "one2"

            print "pretime1"
            sleep(1)
            print "posttime1"

            print("HIGH")
            GPIO.remove_event_detect(in1)
            GPIO.remove_event_detect(in2)

            GPIO.output(out1, GPIO.HIGH)
            GPIO.output(out2, GPIO.HIGH)

            GPIO.add_event_detect(in1, GPIO.BOTH, callback=read2, bouncetime=300)
            GPIO.add_event_detect(in2, GPIO.BOTH, callback=read24, bouncetime=300)

            print("HIGH2")
            GPIO.remove_event_detect(in1)
            GPIO.remove_event_detect(in2)

            GPIO.output(out1, GPIO.HIGH)
            GPIO.output(out2, GPIO.HIGH)

            print "one2"
            GPIO.add_event_detect(in1, GPIO.BOTH, callback=read2, bouncetime=300)
            GPIO.add_event_detect(in2, GPIO.BOTH, callback=read24, bouncetime=300)
            print "two2"

            print "pretime2"
            sleep(1)
            print "posttime2"
            print("LOW2")
            GPIO.remove_event_detect(in1)
            GPIO.remove_event_detect(in2)

            GPIO.output(out1, GPIO.LOW)
            GPIO.output(out2, GPIO.LOW)

            GPIO.add_event_detect(in1, GPIO.BOTH, callback=read2, bouncetime=300)            # print "Turn on " , GPIO.input(24)
            GPIO.add_event_detect(in2, GPIO.BOTH, callback=read24, bouncetime=300)


        sleep(1)

我已将其追踪到 time.sleep ,导致此事发生。

是否有人经历过这种情况和/或有人可以尝试看看是否会在他们的设置上复制。

如果我在这段代码上评论 time.sleep ,那么误报就会停止发生。但是,我没有拖延,哈哈哈哈,

        print "pretime1"
        sleep(1)
        print "posttime1"

0 个答案:

没有答案