1

I'm trying to make a Raspberry Pi (model B+) switch on a user chosen LED via GPIO by means of an io.output() function. However I have trouble getting my script to do so. The script is written in Python3.

# Set GPIO pins for each color
red = 11
yellow = 9
green = 10
# etc...

# LEDs are set as output
io.setup(red, io.OUT)
io.setup(yellow, io.OUT)
io.setup(green, io.OUT)
# etc...

# Function
def phase(color):
    io.output(color, io.HIGH)

color = input("Please choose a color like red, yellow, green, ...: ")

What I try to achieve is this: based on the choice the user makes, that particular LED is switched on.

The user has a choice of 10 colors.

I know this can be achieved by setting:

if color == 'red':
    io.output(red, io.HIGH)
elif color == 'yellow':
    io.output(yellow, io.HIGH)
elif color == 'green':
    io.output(green, io.HIGH)
#etc...

But I would like to only have 1 line of code in which the variable color is updated according to what color the user has chosen:

io.output(color, io.HIGH)

It seems that setting variables in Python3 works a little different then I thought (I'm used to PHP and not that experienced in Python3 yet).

I would expect the variable color to be processed by Python into the users input (let's say "red"), which should subsequently by processed into the corresponding GPIO pin number which is 11.

However that doesn't take place and I can't figure out why. I keep getting errors telling me that color should be an integer. And I thought variables in this case worked like color = red = 11.

How do I pass a variable in my io.output() function? Is that possible or am I stuck with having to write a line of code for each color LED? That seems very redundant to me but maybe I'm completely wrong here.

1
  • you can create dictionary {"red": 11, ...} to convert it and then you can use it. Commented Jul 7, 2019 at 0:55

2 Answers 2

1

Your variable color contains a string. You appear to want some way to map that string to an integer. A dictionary (dict) would be perfect for that and allow some other optimizations as well:

# Set GPIO pins for each color
colors = {
    'red': 11,
    'yellow': 9,
    'green': 10,
    # etc...
}

# LEDs are set as output
[io.setup(v, io.OUT) for k,v in colors]

# Function
def phase(color):
    io.output(colors[color], io.HIGH)

color = input("Please choose red, yellow or green: ")
phase(color)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Can't believe I missed the whole string vs. var thing.. Your code worked a treat but had to be slightly improved for it to be flawless in Python3: [io.setup(v, io.OUT) for k,v in colors.items()]
1

You can use dictionary to convert string to integer

all_colors = {
    "red": 11,
    "yellow": 9,
    "green": 10
}

color = input("Please choose a color like red, yellow, green, ...: ")
color = color.lower().strip()

if color in all_colors:
    value = all_colors[color]
    io.output( value, io.HIGH )
else:
    print("unknown color")

1 Comment

An elegant solution as well! I redesigned my input check with your code.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.