2 Replies - 166 Views - Last Post: 21 August 2014 - 03:31 PM Rate Topic: -----

#1 timr1972  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-August 14

Python TypeError

Posted 21 August 2014 - 03:12 PM

I'm not the first and I'm sure I won't be the last but I just can't get my head around the TypeError I am receiving.

import RPi.GPIO as GPIO, sys
from time import sleep

leds = [4, 17, 27]
buttons = [18, 23, 24, 25]

GPIO.setmode(GPIO.BCM)

def setupLED(ledNumber):
  GPIO.setup(leds[ledNumber], GPIO.OUT)

def setupBUTTON(buttonNumber):
  GPIO.setup(buttons[buttonNumber], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  GPIO.add_event_detect(buttons[buttonNumber], GPIO.RISING, callback=led1, bouncetime=300)

def led1():
  print("LED ON")

try:
  for leds in leds:
    print(leds)
    setupLED(leds)

  setupBUTTON(0)
  setupBUTTON(1)
  setupBUTTON(2)
  setupBUTTON(3)

  while True:
    sleep(0.001)

except KeyboardInterrupt:
  print("\nKeyboard Exit.")

except:
  print("Unexpected error:", sys.exc_info()[0])
  raise

finally:
  GPIO.cleanup()



The above is a very simple bit of code on a Raspberry, in python and if you look at the setupLED(leds) on line 22 this is where it fails but I just can't get my head around why if I write it out in long format (setupLED(0), setupLED(1) etc..) it works...

Can someone point this numpty in the right direction?

Is This A Good Question/Topic? 0
  • +

Replies To: Python TypeError

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3491
  • View blog
  • Posts: 11,910
  • Joined: 12-December 12

Re: Python TypeError

Posted 21 August 2014 - 03:28 PM

Post the full error message. If necessary, disable the error-handling temporarily to see the full details.

Note that it is not sensible to do things like this:
	  for leds in leds:

It is obviously confusing and will, at some point, cause problems.

This post has been edited by andrewsw: 21 August 2014 - 03:29 PM

Was This Post Helpful? 0
  • +
  • -

#3 timr1972  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-August 14

Re: Python TypeError

Posted 21 August 2014 - 03:31 PM

Good point thanks :)/>

I have changed the code so it works, differently and maybe is good enough, but for reference the error is

pi@TLRPI1 ~ $ sudo python3 led2.py
4
Unexpected error: <class 'TypeError'>
led2.py:50: RuntimeWarning: No channels have been set up yet - nothing to clean up!  Try cleaning up at the end of your program instead!
  GPIO.cleanup()
Traceback (most recent call last):
  File "led2.py", line 23, in <module>
    setupLED(x)
  File "led2.py", line 10, in setupLED
    GPIO.setup(leds[ledNumber], GPIO.OUT)
TypeError: 'int' object is not subscriptable



import RPi.GPIO as GPIO, sys
from time import sleep

leds = [4, 17, 27]
buttons = [18, 23, 24, 25]

GPIO.setmode(GPIO.BCM)

def setupLED(ledNumber):
  GPIO.setup(leds[ledNumber], GPIO.OUT)

def setupBUTTON(buttonNumber):
  GPIO.setup(buttons[buttonNumber], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  GPIO.add_event_detect(buttons[buttonNumber], GPIO.RISING, callback=led1, bouncetime=300)

def led1():
  print("LED ON")

try:
  x = 0
  while x < len(leds):
    setupLED(x)
    x = x +1

  setupBUTTON(0)
  setupBUTTON(1)
  setupBUTTON(2)
  setupBUTTON(3)

  while True:
    sleep(0.001)

except KeyboardInterrupt:
  print("\nKeyboard Exit.")

except:
  print("Unexpected error:", sys.exc_info()[0])
  raise

finally:
  GPIO.cleanup()


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1