-1

I'm working on an automation program with a remote. The remote needs the button to be turned off then on. I have a web service but it can only send the on signal and not the off signal. I want to make a python script that looks for the pin to be turned on and then turn it off. Here is what I have so far:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)

while True:
     if (GPIO.output(12,False)
     GPIO.output(12,False)
     time.sleep(.2)

I keep getting SyntaxError: invalid syntax line 9 GPIO.output(12,True)

1

Your missing a closing parenthesis and colon: Change this:

if (GPIO.output(12,False)

to

if (GPIO.output(12,False)):

You also need to indent the next twwo lines (so the last three lines should look like this:

if (GPIO.output(12,False)):
    GPIO.output(12,False)
    time.sleep(.2)
1

Steve's answer covers the syntax error which prevents the program from running. However, I doubt it will do what you intend it to do after the fix. I am right now not sure what GPIO.output(12,False) actually returns but that return value will be used to decide the conditional here. That is likely not what you want.

Either way calling GPIO.output(12,False) in this line if (GPIO.output(12,False)): will set this particular output to low. Which you do again in the next line, repeating ad infinitum. There is little point to that.

If you want to read back the state of something you will have to turn a GPIO pin to input, i.e. like this:

GPIO.setup(24, GPIO.OUT)           # set GPIO24 as an output 

while True:
    # do something

    if GPIO.input(24):
        # do something if input 24 is high

Learn more:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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