I'm trying to write a little script to control two raspberry pi's gpio output pins based on two factors: the state of GPIO.input.17 and the time of day.
I would like gpio.output.23 and gpio.output.25 to be low when gpio.input.17 is low.
I would like gpio.output.23 to go high when gpio.input.17 is high and the time is between 0700-2159.
I would like gpio.output.25 to go high when gpio.input.17 is high and the time is between 2200-0659.
so far, the code i've put together looks like this:
#!/usr/bin/python
import time
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(25, GPIO.OUT) # set GPIO 25 as output
GPIO.output(23, 0) # set GPIO 23 as low
GPIO.output(25, 0) # 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 (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()
I have LED's connected to pins 23 and 25, the times shown in the script are from my testing and the results i see with this code are:
Out.Pin 23 toggles between high and low following In.Pin.17's status whilst the time variable is true
Out.Pin 23 stops toggling between high and low whilst the time variable is not true
I feel like I have Out.Pin.23 working...
Out.Pin 25 immediately lights up when the code is executed and stays lit regardless of In.Pin.17's status or the time.
Please ignore the times in the script, they're from my testing and won't match up with the requirements above.
I'm a beginner with coding and writing scripts and would appreciate any assistance from the community.
Thanks