3 回答
TA贡献1810条经验 获得超4个赞
虽然我可以在 IDLE 中运行它并遵循代码中的逻辑,但我无法将其集成到我的脚本中(它破坏了我首先工作的内容:GPIO.output.23 遵循 GPIO.input.17 的状态“在活动时间内”。
鉴于我的新手,你能指导我一下你的建议哪里出了问题吗?我喜欢整理 if/elif 语句的想法。
#!/usr/bin/python
import time
import datetime as dt
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
# Define Time Variables
now = dt.datetime.now().time()
day = dt.time(16, 32, 59, 000000)
night = dt.time(16, 33, 00, 000000)
# Setup GPIO pins
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # set GPIO 17 as input
GPIO.setup(23, GPIO.OUT) # set GPIO 23 as output
GPIO.setup(25, GPIO.OUT) # set GPIO 25 as output
GPIO.output(23, 0) # set GPIO 23 as low
GPIO.output(25, 1) # set GPIO 25 as low
while True:
dt = list(time.localtime())
hour = dt[3]
minute = dt[4]
second = dt[5]
time.sleep(1)
print hour,minute,second;
PIR_Active = GPIO.input(17)
if not PIR_Active:
GPIO.output(23, 0)
GPIO.output(25, 0)
elif all([PIR_Active, day < now < night]):
GPIO.output(23, 1)
else: all([PIR_Active, day < now < night]);
GPIO.output(25, 1)
time.sleep(1)
GPIO.cleanup()
TA贡献1712条经验 获得超3个赞
我已经设法解决了这个问题...我原来的 else 语句无效,因此我添加了第三个 elif 和我的“夜间”条件,并用将两个 GPIO.out 引脚设置为 1 的 else 完成了该语句。
我还反转了输出的“静止”状态,因为我认为我得到的继电器单元被应用了负值
这是带注释的工作代码:
#!/usr/bin/python
import time
from time import sleep
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
# Setup GPIO pins
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # set GPIO 17 as input
GPIO.setup(23, GPIO.OUT) # set GPIO 23 as output
GPIO.setup(24, GPIO.OUT) # set GPIO 25 as output
GPIO.output(23, 1) # set GPIO 23 as high - relay boards are neg applied to activate, this keeps the relay powered off without a signal
GPIO.output(24, 1) # set GPIO 25 as high - relay boards are neg applied to activate, this keeps the relay powered off without a signal
while True:
dt = list(time.localtime())
hour = dt[3]
minute = dt[4]
second = dt[5]
time.sleep(.01) #The first time.sleep command value impacts any similar statements made below it
print hour,minute,second;
PIR_Active = GPIO.input(17) #Define a condition which is met in the statements below
if not PIR_Active: #If the input is not active, reset both outputs to 'off'
GPIO.output(23, 1)
GPIO.output(24, 1)
elif (PIR_Active and (hour>=00 and hour<=6) and (minute >=00 and minute<=59) and (second >=0 and second<=59)):
GPIO.output(24, 0) #If all of the above is true, set this output to on
time.sleep(30) #Hold this output 'on' for 30 seconds
elif (PIR_Active and (hour>=7 and hour<=21) and (minute>=0 and minute<=59) and (second>=0 and second<=59)):
GPIO.output(23, 0) #If all of the above is true, set this output to on
time.sleep(30) #Hold this output 'on' for 30 seconds
elif (PIR_Active and (hour>=22 and hour<=23) and (minute >=00 and minute<=59) and (second >=0 and second<=59)):
GPIO.output(24, 0) #If all of the above is true, set this output to on
time.sleep(30) #Hold this output 'on' for 30 seconds
else: #Cleanly exit out of the if/elif statements with an else that:
GPIO.output(23, 1) #Resets this output to 'off'
GPIO.output(24, 1) #Resets this output to 'off'
GPIO.cleanup()
TA贡献1805条经验 获得超9个赞
首先,我承认这不是对你问题的完整答案,但对于评论来说太多了。
此部分答案的目的是建议简化日期时间测试。
设定
now
时间设定
day
时间设定
night
时间使用一个简单的逻辑运算符来测试是白天还是晚上。
例子:
import datetime as dt
now = dt.datetime.now().time()
day = dt.time(7, 00)
night = dt.time(22, 00)
# Test if it's day time.
now
>>> datetime.time(14, 8, 6, 000000)
day < now < night
>>> True
# Test again at midnight.
now
>>> datetime.time(0, 0)
day < now < night
>>> False
将此逻辑集成到您的代码中将有助于简化if/elif,and语句。
例如,这个:
(PIR_Active and (hour>=00 and hour<=6) and (minute >=00 and minute<=59) and (second >=0 and second<=59))
...可以变成这样 - 显然使用你自己的时间定义。
all([PIR_Active, day < now < night])
添加回答
举报