Skip to content

Instantly share code, notes, and snippets.

@1ycx
Last active May 31, 2022 06:54
Show Gist options
  • Save 1ycx/1675e05a555568b496a2d725cf2f9766 to your computer and use it in GitHub Desktop.
Save 1ycx/1675e05a555568b496a2d725cf2f9766 to your computer and use it in GitHub Desktop.
Code for Water Alarm using Pi
#!/usr/bin/python3
import sys
import time
import RPi.GPIO as GPIO
# def gett():
# return time.strftime("%I:%M %p")
def change_state(inp1, inp2, ti, cleanup=False):
GPIO.output(pin1, inp1)
GPIO.output(pin2, inp2)
time.sleep(ti)
if cleanup: GPIO.cleanup()
if __name__ == "__main__":
pin1, pin2 = 18, 16
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin1, GPIO.OUT)
GPIO.setup(pin2, GPIO.OUT)
print(sys.argv)
# Flush the circuit on reboot
if len(sys.argv) > 1:
if sys.argv[1] == 'flush':
print("Flushing motor after reboot")
change_state(0, 1, 0.2)
change_state(0, 0, 0, True)
sys.exit()
# Run the motor
try:
while True: #t := gett() in ["6:00 AM", "6:01 AM", "6:02 AM", "6:03 AM", "6:04 AM"]:
for i in range(30):
change_state(1, 0, 0.50)
change_state(0, 0, 1.00)
except KeyboardInterrupt as e:
print(e)
except Exception as e:
print(e)
# Cleanup after
change_state(0, 0, 0, True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment