python 中的 GPIO 条件
GPIO conditional in python
我在 python 中编写子条件时遇到了一些问题。
希望有人可以查看我的代码:
第一个条件是读取GPIO,(24),另一个条件是好像文件存在。
如果文件存在,则无需执行任何操作。
这是我的初始代码:
#!/usr/bin/python
import os
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.IN)
GPIO.setup(22, GPIO.OUT)
file = "22.gp"
try:
while True:
if GPIO.input(24):
print "Port 24 is 1/HIGH/True - LED ON"
if os.path.exists(file)
print "Already light ON"
else:
GPIO.output(22, 1)
file = open(file, "w+")
sleep(20)
else:
print "Port 24 is 0/LOW/False - LED OFF"
GPIO.output(22, 0)
if os.path.exists(file):
os.remove(file)
else:
print("sorry, I cannot remove %s file:" % file)
sleep(0.1)
finally:
GPIO.cleanup()
这段代码给出了这样的错误:
print "Port 24 is 1/HIGH/True - LED ON"
^
IndentationError: expected an indented block
因为,python 不使用花括号或分号,为了理解范围和语句结束需要缩进。
if GPIO.input(24):
print "Port 24 is 1/HIGH/True - LED ON"
把这个改成那个(所有 if/else,循环):
if GPIO.input(24):
print "Port 24 is 1/HIGH/True - LED ON"
使用 4 space 是一个标准。关注一下。
我在 python 中编写子条件时遇到了一些问题。 希望有人可以查看我的代码:
第一个条件是读取GPIO,(24),另一个条件是好像文件存在。 如果文件存在,则无需执行任何操作。
这是我的初始代码: #!/usr/bin/python
import os
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.IN)
GPIO.setup(22, GPIO.OUT)
file = "22.gp"
try:
while True:
if GPIO.input(24):
print "Port 24 is 1/HIGH/True - LED ON"
if os.path.exists(file)
print "Already light ON"
else:
GPIO.output(22, 1)
file = open(file, "w+")
sleep(20)
else:
print "Port 24 is 0/LOW/False - LED OFF"
GPIO.output(22, 0)
if os.path.exists(file):
os.remove(file)
else:
print("sorry, I cannot remove %s file:" % file)
sleep(0.1)
finally:
GPIO.cleanup()
这段代码给出了这样的错误:
print "Port 24 is 1/HIGH/True - LED ON"
^
IndentationError: expected an indented block
因为,python 不使用花括号或分号,为了理解范围和语句结束需要缩进。
if GPIO.input(24):
print "Port 24 is 1/HIGH/True - LED ON"
把这个改成那个(所有 if/else,循环):
if GPIO.input(24):
print "Port 24 is 1/HIGH/True - LED ON"
使用 4 space 是一个标准。关注一下。