1

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"
4
  • 4
    So despite this being about gpio on a pi I’m not allowed to ask it here well this is jolly helpful. Is this why newbies are afraid to ask questions and then struggle to learn? Thanks for the info Aug 23, 2019 at 10:30
  • @Brick OK, you are right. I have deleted my comments.
    – Ingo
    Aug 23, 2019 at 16:52
  • Sorry for grumpiness earlier I was in a massive funk and this didn’t help.in all seriousness I have found it really difficult to get support at times I think if people really want to support coding it would be good to help newbies! Thanks again for your support now this one extra line of code has sorted my issue and help me to understand the variable function ! Aug 23, 2019 at 18:07
  • While I think the question could and should have been answered it remains a simple Python coding error, which is not Pi specific.
    – Milliways
    Aug 23, 2019 at 22:53

1 Answer 1

2

You're not actually re-checking GPIO4, you're just re-reading the variable mag1.

One way to fix this would be to add mag1=GPIO.input(4) after your time.sleep(20).

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)
        mag1=GPIO.input(4)

if mag1:
        print "The garage has closed"
else:
        print "The garage failed to close"
1
  • Perfect this nailed it with the one line! Aug 23, 2019 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.