Skip to content

Instantly share code, notes, and snippets.

@benfasoli
Last active July 13, 2016 14:41
Show Gist options
  • Save benfasoli/aaab88702720d5602ac85be938da544a to your computer and use it in GitHub Desktop.
Save benfasoli/aaab88702720d5602ac85be938da544a to your computer and use it in GitHub Desktop.
Raspberry Pi based timed valve controller.
#!/usr/bin/python
# Ben Fasoli
import datetime
import RPi.GPIO as GPIO
from time import sleep
std_pins = [11, 13, 15, 29]
qc_pins = [11, 13, 15, 31, 33]
all_pins = [11, 13, 15, 29, 31, 33]
t = datetime.datetime.utcnow()
# Initialize GPIO
GPIO.setmode(GPIO.BOARD)
for pin in all_pins: GPIO.setup(pin, GPIO.OUT)
def set_pin(num):
not_pin = list(set(all_pins) - set([num]))
for pin in not_pin: GPIO.output(pin, 1)
GPIO.output(num, 0)
outstr = t.strftime('%Y-%m-%d %H:%M:%S.%f') + ', ' + str(num) + '\n'
print outstr
with (open('/home/pi/valve_log.dat', 'a+')) as f:
f.write(outstr)
def std_routine():
if t.minute % 10 is 0 and t.second is 0:
pin_num = std_pins[pin_idx]
set_pin(pin_num)
sleep(1)
return(True)
else: return(False)
def qc_routine():
if t.minute % 2 is 0 and t.second is 0:
pin_num = qc_pins[qcpin_idx]
set_pin(pin_num)
sleep(1)
return(True)
else: return(False)
pin_idx = 0
qcpin_idx = 0
pin_num = std_pins[3]
set_pin(pin_num)
while True:
t = datetime.datetime.utcnow()
if t.hour % 6 is 0 and t.minute >= 20 and t.minute < 30:
if qc_routine():
pin_idx = 0
qcpin_idx = qcpin_idx + 1
else:
if std_routine():
qcpin_idx = 0
pin_idx = pin_idx + 1
if pin_idx > 3: pin_idx = 0
sleep(0.05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment