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])
添加回答
舉報