2

I am currently bread-boarding two LEDs with 560 mOh resistors connected to the RPi over a ribbon cable that connects to the breadboard with a Pi Cobbler.

The problem I am getting isn't with the hardware, it's with the script. When I run it, I get this error:

led_g-r.py:11: RuntimeWarning: This channel is already in use, continuing anyway.  
Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(GREEN_LED, GPIO.OUT)
led_g-r.py:16: RuntimeWarning: This channel is already in use, continuing anyway.  
Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(RED_LED, GPIO.OUT)
Traceback (most recent call last):
  File "led_g-r.py", line 59, in 
    main()
  File "led_g-r.py", line 56, in main
    green.flash(3)
  File "led_g-r.py", line 33, in flash
    for flash in range(0, repeat):
AttributeError: LED instance has no attribute '__trunc__'

Can anyone see the problem in my code (included below). I wrote the script myself and I'm rather new to python so I probably will have a few problems/code that could be done better.

I wrote this with a class so that I could change around my projects with different LED configurations.

This is the code:

import RPi.GPIO as GPIO
import time       

GPIO.setmode(GPIO.BCM)

class LED:                                 
        def __init__(self, color, pin):
                self.color = color.lower()
                if self.color == 'green':
                        GREEN_LED = pin                          
                        GPIO.setup(GREEN_LED, GPIO.OUT)             
                        self.LED = 'GREEN_LED'                      

                elif self.color == 'red':
                        RED_LED = pin
                        GPIO.setup(RED_LED, GPIO.OUT)
                        self.LED = 'RED_LED'

        def OFF():                           

                GPIO.output(self.LED, False)

        def ON():
                GPIO.output(self.LED, True)


        def flash(repeat=1, length=1, cust=0):  # repeat is automatically 1
                                        # cust is automatically 0
                                        # length is automatically 1:
                                        # 1 = short-on/off for 3sec.
                                        # 2 = medium-on for 5sec. off for 3sec.
                                        # 3 = long-on for 10sec. off for 3 sec.
                                        # 4 = custom-set cust=length on in sec.
                for flash in range(0, repeat):       
                        ON()           
                        if length == '1':
                                time.sleep(3)
                        elif length == '2': 
                                time.sleep(5)
                        elif length == '3':
                                time.sleep(10)
                        elif length == '4':
                                time.sleep(self.cust)
                        OFF()
                        time.sleep(3)                            
                        ON()

        def SOS(repeat=1):
                flash(3, 1)
                flash(3, 3)
                flash(3, 1)                          

def main():                              
        green = LED('green', 18)

        red = LED('red', 23)                

        green.flash(3)                     
        red.flash(3)                          
main()

Thanks!

PS...If it helps, I can post a picture of the configuration. I also have connected my USB to TTL Console Cable so I can connect to the pi without having to use my awful screen.

1 Answer 1

6

Restart your Pi, and then use this code to clean up the GPIO pins when your program exits:

GPIO.cleanup()

The next time your program runs it should work.

2
  • This will get rid of the Attribute Error? Thanks! I will try this! But, overall, is my code good/okay? I'm trying to get good at Python, but I've taught myself via books and videos and Stack Exchanges (Here, Ask Ubuntu, Stack Overflow), so I don't know if my code is ok or not. Thanks! Commented Sep 15, 2013 at 12:06
  • 1
    I think you're seeing the attribute error because of the runtime warning about the channel already being in use. If you fix the runtime warning, the attribute error should go away.
    – Steve
    Commented Sep 15, 2013 at 12:10

Your Answer

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

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