0

Can you tell me where the syntax error in this code is??

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

GPIO.setup(4, GPIO.IN)

try: 
    while True:
        if (GPIO.input(4) == 1):
            print "button pressed"
        else:
            print "he"
GPIO.cleanup()

it should print button pressed when the gpio pin on my raspberry pi gets power when the button on the board is pressed. but i get an error all the time i run the code in the console running raspbian. most of the errors say "invalid syntax" and than the last codeline of the script.

1
  • Post the full error you're getting. Commented Feb 10, 2014 at 1:03

1 Answer 1

2

You have a try with no except clause. You need something like:

try:
    while True:
        if (GPIO.input(4) == 1):
            print "button pressed"
        else:
            print "he"
except:
    # handle exception here
    pass # if you want to ignore it
GPIO.cleanup()
Sign up to request clarification or add additional context in comments.

2 Comments

yeah now the syntax error is gone. great but i got this: RuntimeError: No access to /dev/mem. Try running as root! i tried running as root but still this error
That's a different problem and I can't help you with that one. You'll need to ask a new question.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.