This is the code I am using to operate a relay to open my garage and the garage status is checked by a magnetic switch. However there is an issue with the logic can someone help me please as I am very new to Python.
If the garage is already closed (gpio 4 input true) it should not attempt to close. (this bit is working)
If it then does close (gpio 18 high) it should wait 20 seconds and recheck gpio 4. If it is closed it should report this as such. However it always tells me that it has failed to close. Can someone help me correct this error please?
Thanks!
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
mag1=GPIO.input(4)
if mag1:
print "The garage is already closed"
time.sleep(1)
else:
GPIO.output(18,GPIO.HIGH)
time.sleep(0.5)
GPIO.output(18,GPIO.LOW)
print "close pressed" #delete if works and delete## above
time.sleep(20)
if mag1:
print "The garage has closed"
else:
print "The garage failed to close"