psutton
Posts: 178
Joined: Thu Jan 12, 2012 10:45 pm
Location: Devon
Contact: Website

GPIO pin access with tuples

Tue Mar 11, 2014 1:58 pm

Hi

I have one of Gordon Hendersons Ladder boards which I have connected to the raspberry pi and can send output to the pins to light up all 8 leds.

I am trying to compress some of the code required, so rather than :

GPIO.output(7, True) # blue 1
GPIO.output(11, True) # red 2
GPIO.output(12, True) # red 1
GPIO.output(13, True) # yellow 2
GPIO.output(15, True) # yellow 1
GPIO.output(16, True) # green 2
GPIO.output(18, True) # green 1
GPIO.output(22, True) # blue 2

I was thinking if I could store the pin numbers in a list then some how pass the values in the list to the GPIO command

print ("tupletest")
leds=["7","11","12","13","15","16","18","22"]
for x in range(0,8):
# GPIO.output(leds[int(x)],True)
print(leds[x])

This prints all the 8 values stored in the list



if I uncomment the GPIO line i get problems. I am getting in a real mess with this, it wants an integer somewhere

I get the following error

Traceback (most recent call last):
File "ladderboard3a.py", line 79, in <module>
GPIO.output(leds[int(x)],True)
TypeError: an integer is required

I am clearly not doing something right here, the loop works, it can read the list of numbers as the print statement can output them, but how do I pass this on to the GPIO.output statement


Would i be better off putting things like blue1, blue2 refering to the colour of the leds


thanks
Paul

User avatar
joan
Posts: 14849
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: GPIO pin access with tuples

Tue Mar 11, 2014 2:16 pm

Perhaps

Code: Select all

leds=[7, 11, 12, 13, 15, 16, 18, 22]
for x in leds:
   GPIO.output(x,True)

zedin
Posts: 38
Joined: Tue Oct 18, 2011 1:20 pm

Re: GPIO pin access with tuples

Tue Mar 11, 2014 3:18 pm

Main problem seems that you are making a LED list of string elements instead of numbers by using the quotes during list creation.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: GPIO pin access with tuples

Tue Mar 11, 2014 3:54 pm

Zedin's right - the error is becasue you've got a list of strings, whereas the function needs an integer. You could fix this error by changing that line to:

Code: Select all

GPIO.output(int(leds[x]),True)
but I certainly wouldn't recommend it as that's just covering up a number of other issues (i.e. list of strings, looping through range and using that as index to look up value in list). Joan's post is far better and is the more pythonic answer.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

scotty101
Posts: 3951
Joined: Fri Jun 08, 2012 6:03 pm

Re: GPIO pin access with tuples

Wed Mar 12, 2014 8:11 am

Also just to clear up your terminology in python

This is a list

Code: Select all

leds=["7","11","12","13","15","16","18","22"]
This is a tuple

Code: Select all

leds=("7","11","12","13","14","15","16","18","22")
See http://stackoverflow.com/questions/1708 ... o-use-each
http://stackoverflow.com/questions/1708510/python-list-vs-tuple-when-to-use-each wrote:Tuples are fixed size in nature whereas lists are dynamic.
In other words, a tuple is immutable whereas a list is mutable.

1.You can't add elements to a tuple. Tuples have no append or extend method.
2.You can't remove elements from a tuple. Tuples have no remove or pop method.
3.You can find elements in a tuple, since this doesn’t change the tuple.
4.You can also use the in operator to check if an element exists in the tuple.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Python”