亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

基于 GPIO 輸入和時間的 Python rpi GPIO 輸出控制

基于 GPIO 輸入和時間的 Python rpi GPIO 輸出控制

慕碼人8056858 2023-12-20 19:47:37
我正在嘗試編寫一個小腳本來根據兩個因素控制兩個樹莓派的 GPIO 輸出引腳:GPIO.input.17 的狀態和一天中的時間。當 gpio.input.17 為低電平時,我希望 gpio.output.23 和 gpio.output.25 為低電平。我希望當 gpio.input.17 為高且時間在 0700-2159 之間時 gpio.output.23 變高。我希望當 gpio.input.17 為高且時間在 2200-0659 之間時 gpio.output.25 變高。到目前為止,我整理的代碼如下所示:#!/usr/bin/pythonimport timeimport RPi.GPIO as GPIOGPIO.setwarnings(False)GPIO.setmode(GPIO.BCM)# Setup GPIO pinsGPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)     # set GPIO 17 as inputGPIO.setup(23, GPIO.OUT)                                # set GPIO 23 as outputGPIO.setup(25, GPIO.OUT)                                # set GPIO 25 as outputGPIO.output(23, 0)                                      # set GPIO 23 as lowGPIO.output(25, 0)                                      # set GPIO 25 as lowwhile 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 (PIR_Active and (hour>=00 and hour<=6) and (minute >=00 and minute<=59) and (second >=0 and second<=59)):        GPIO.output(25, 1)    elif (PIR_Active and (hour>=7 and hour<=11) and (minute>=0 and minute<=36) and (second>=0 and second<=59)):        GPIO.output(23, 1)    else: (PIR_Active and (hour>=11 and hour<=23) and (minute >=37 and minute<=59) and (second >=0 and second<=59));    GPIO.output(25, 1)time.sleep(1)GPIO.cleanup()我將 LED 連接到引腳 23 和 25,腳本中顯示的時間來自我的測試,我通過此代碼看到的結果是:Out.Pin 23 按照 In.Pin.17 的狀態在高電平和低電平之間切換,同時時間變量是真的Out.Pin 23 停止在高低之間切換,而時間變量不正確我感覺 Out.Pin.23 正在工作...當代碼執行時,Out.Pin 25 立即亮起,并且無論 In.Pin.17 的狀態或時間如何,都保持亮起。請忽略腳本中的時間,它們來自我的測試,與上述要求不符。我是編碼和編寫腳本的初學者,非常感謝社區的任何幫助。
查看完整描述

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()


查看完整回答
反對 回復 2023-12-20
?
交互式愛情

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()


查看完整回答
反對 回復 2023-12-20
?
Cats萌萌

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])


查看完整回答
反對 回復 2023-12-20
  • 3 回答
  • 0 關注
  • 272 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號