简体   繁体   中英

GPIO set error and keyboard interrupt - Python

I have written a code to turn a relay on for 2 seconds, then off but I am having issues.

Firstly one the program has run I get the following error, yet the GPIO is set:

`Traceback (most recent call last):
  File "relay_control.py", line 16, in <module>
    GPIO.output(2, GPIO.HIGH)
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)`

My second issue is that I want to set KeyboardInterrupt yet when I use except KeyboardInterrupt: I get an error and nothing runs, if I leave this out it goes through one loop and exits (as expected):

    File "relay_control.py", line 30
    except KeyboardInterrupt:
         ^
SyntaxError: invalid syntax

I am not sure what is wrong, the full code is below:

import RPi.GPIO as GPIO
from time import sleep

    GPIO.setmode(GPIO.BCM)

    # Set relay pins as output
    GPIO.setup(2, GPIO.OUT)
    GPIO.setup(3, GPIO.OUT)
    GPIO.setup(4, GPIO.OUT)
    GPIO.setup(17, GPIO.OUT)

    while (True):

        # Turn all relays ON
        GPIO.output(2, GPIO.HIGH)
        GPIO.output(3, GPIO.HIGH)
        GPIO.output(4, GPIO.HIGH)
        GPIO.output(17, GPIO.HIGH)
        # Sleep for 5 seconds
        sleep(2)
        # Turn all relays OFF
        GPIO.output(2, GPIO.LOW)
        GPIO.output(3, GPIO.LOW)
        GPIO.output(4, GPIO.LOW)
        GPIO.output(17, GPIO.LOW)
        # Sleep for 5 seconds
        sleep(2)

    except KeyboardInterrupt:
        print "Quit"
        GPIO.cleanup()

Somehow fixing the keyboard interrupt doesn't give any errors:

import RPi.GPIO as GPIO
from time import sleep

# The script as below using BCM GPIO 00..nn numbers
GPIO.setmode(GPIO.BCM)

# Set relay pins as output
GPIO.setup(2, GPIO.OUT)
GPIO.setup(3, GPIO.OUT)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)

try:
        while (True):

            # Turn all relays ON
            GPIO.output(2, GPIO.HIGH)
            GPIO.output(3, GPIO.HIGH)
            GPIO.output(4, GPIO.HIGH)
            GPIO.output(17, GPIO.HIGH)
            # Sleep for 5 seconds
            sleep(2)
            # Turn all relays OFF
            GPIO.output(2, GPIO.LOW)
            GPIO.output(3, GPIO.LOW)
            GPIO.output(4, GPIO.LOW)
            GPIO.output(17, GPIO.LOW)
            # Sleep for 5 seconds
            sleep(2)

except KeyboardInterrupt:
    print "Quit"
    GPIO.cleanup()

I have written a code to turn a relay on for 2 seconds, then off but I am having issues.

Firstly one the program has run I get the following error, yet the GPIO is set:

`Traceback (most recent call last):
  File "relay_control.py", line 16, in <module>
    GPIO.output(2, GPIO.HIGH)
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)`

My second issue is that I want to set KeyboardInterrupt yet when I use except KeyboardInterrupt: I get an error and nothing runs, if I leave this out it goes through one loop and exits (as expected):

    File "relay_control.py", line 30
    except KeyboardInterrupt:
         ^
SyntaxError: invalid syntax

I am not sure what is wrong, the full code is below:

import RPi.GPIO as GPIO
from time import sleep

    GPIO.setmode(GPIO.BCM)

    # Set relay pins as output
    GPIO.setup(2, GPIO.OUT)
    GPIO.setup(3, GPIO.OUT)
    GPIO.setup(4, GPIO.OUT)
    GPIO.setup(17, GPIO.OUT)

    while (True):

        # Turn all relays ON
        GPIO.output(2, GPIO.HIGH)
        GPIO.output(3, GPIO.HIGH)
        GPIO.output(4, GPIO.HIGH)
        GPIO.output(17, GPIO.HIGH)
        # Sleep for 5 seconds
        sleep(2)
        # Turn all relays OFF
        GPIO.output(2, GPIO.LOW)
        GPIO.output(3, GPIO.LOW)
        GPIO.output(4, GPIO.LOW)
        GPIO.output(17, GPIO.LOW)
        # Sleep for 5 seconds
        sleep(2)

    except KeyboardInterrupt:
        print "Quit"
        GPIO.cleanup()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM