Error when running GPIO.cleanup()
Posted: Thu Feb 19, 2015 2:18 pm
I have written a program to output data to a WS2803 LED controller that is connected to GPIO17/GPIO18. The code itself runs properly, which is to say that the one LED I currently have connected does exactly what I expect, but I am getting an error when I CTRL+C the program to get it to stop. I think this pertains to my GPIO.cleanup() call which I added inside a try/except block to stop the "this channel is already in use" warnings when I run the program multiple times.
Here is my code:
And here is the error i get when I stop the program:
Without the GPIO.cleanup() I get the following errors when I run the program more than one time:
Clearly I am doing something wrong. I have general familiarity with embedded systems (some arduino/pic experience) but usually do my development in .NET, this is my first ever time using python. Can anybody spot my mistake? Thanks in advance for any help!
Here is my code:
Code: Select all
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
clock = 17
mosi = 18
GPIO.setup(clock,GPIO.OUT)
GPIO.setup(mosi,GPIO.OUT)
GPIO.output(clock,True)
GPIO.output(mosi,False)
counter = 0
def writeByte(value):
# do something to write the byte
mask = 0x80
for i in range(8):
GPIO.output(clock,False)
if (value & mask):
GPIO.output(mosi,True)
else:
GPIO.output(mosi,False)
GPIO.output(clock,True)
mask >>= 1
while True:
try:
GPIO.output(clock,False)
sleep(0.0005)
for i in range(3):
writeByte(counter)
for i in range(15):
writeByte(0)
counter = counter + 1
if (counter > 255):
counter = 0
except KeyboardInterrupt:
GPIO.cleanup()
Code: Select all
~/spi $ sudo python test.py
^CTraceback (most recent call last):
File "test.py", line 32, in <module>
GPIO.output(clock,False)
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)
Code: Select all
test.py:8: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(clock,GPIO.OUT)
test.py:9: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(mosi,GPIO.OUT)