我正在尝试创建一个脚本来运行电动机门打开和关闭它,当它碰到限位开关时它会停止 . 门由光传感器激活 .

到目前为止,我有光传感器和电机工作正常的代码,但我不知道最好的位置是什么时候把“当开关顶部==真”停止电机 .

任何人都可以建议最好的陈述地点,还是我应该使用的功能?

while True:
        def relay( state ):
                if (state == 'Open'):
                        GPIO.output(3,False)
                else:
                        GPIO.output(3,True)
                return

        humidity, temperature = Adafruit_DHT.read_retry(11, 17)

        lcd.clear()
        lcd.cursor_pos = (1, 0)
        lcd.write_string("Temp:%dC"% temperature)

        if (GPIO.input(INPUT_PIN) == False):
                relay('Open')
                print('Open')
                lcd.cursor_pos = (0, 0)
                lcd.write_string("Door open")
        else:
                relay('Close')
                print('Close')
                lcd.cursor_pos = (0, 0)
                lcd.write_string("Door close")

time.sleep(1)

GPIO.cleanup()