#import GPIO library
import RPi.GPIO as GPIO
#set GPIO numbering mode and define input pin
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16,GPIO.IN)
try:
while True:
if GPIO.input(16)==0:
print "Open_16"
else:
print "Closed_16"
finally:
#cleanup the GPIO pins before ending
GPIO.cleanup()
我如何修改上述脚本以监控2个GPIO引脚
16正在监视如何监控插针16并说插针33
谢谢你的帮助
最佳答案
#import GPIO library
import RPi.GPIO as GPIO
#set GPIO numbering mode and define input pin
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16,GPIO.IN)
GPIO.setup(33,GPIO.IN)
try:
while True:
if GPIO.input(16)==0:
print "Open_16"
else:
print "Closed_16"
if GPIO.input(33)==0:
print "Open_33"
else:
print "Closed_33"
finally:
#cleanup the GPIO pins before ending
GPIO.cleanup()
如果要在AND,OR,XOR等两个引脚的状态之间建立逻辑关系,请使用逻辑运算符(https://www.guru99.com/python-operators-complete-tutorial.html)
关于python - 监视PI上的两个GPIO引脚,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59985317/