1

I'm using the Raspberry Pi 3 and controlling three LEDs with Python. I could say I'm good with Python. Here's my code:

import RPi.GPIO as GPIO
import time

#GPIO Pins
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17,GPIO.OUT)
GPIO.setup(27,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)

def led(color,state):
    if state == "on":
        if color == "g": #green
            GPIO.output(27,GPIO.HIGH)
        elif color == "y": #yellow
            GPIO.output(22,GPIO.HIGH)
        elif color == "r": #red
            GPIO.output(17,GPIO.HIGH)
        print ("LED on")
    elif state == "off":
        if color == "g":
            GPIO.output(27,GPIO.LOW)
        elif color == "y":
            GPIO.output(22,GPIO.LOW)
        elif color == "r":
            GPIO.output(17,GPIO.LOW)
        print ("LED off")

while True:
    leds_col = input("Color (r, g, y): ")
    leds_stat = input("On or Off: ")
    led(leds_col, leds_stat)

I have a function called led() that takes two arguments, color (g, y or r) and state (on or off). In the while loop, leds_col asks for the color in the console and leds_stat for the status. Now what I'm trying to achieve is rather than asking for the colour in a different line and the status of the led in another line, is to combine them two. So for example I'd write on the console:

g, on

And it would turn on the green LED. I know i can just use an if statement like: if led_input == "g, on": GPIO.output(27,GPIO.HIGH) But I'm sure there's a better way to do this.

Nour_Dev
  • 55
  • 1
  • 6

3 Answers3

5

Use string.split():

while True: 
    what = input("Color [r,g,y] and state [on,off] (ex.: 'r on'): ").strip().lower().split()
    if len(what)==2:
        leds_col,leds_stat = what
        # sort the color input to reduce possible values
        leds_col = ''.join(sorted(leds_col))
        if leds_col not in "r g y gr ry gy gry" or leds_stat not in "on off":
            continue
    else:
        continue
    led(leds_col, leds_stat)

If invalid input is given, this will continue to ask until inputs are valid. See Asking the user for input until they give a valid response for more ideas on input validation.


Unrelated - but you can optimize your led-function:

def led(color,state):
    d = {"on":GPIO.HIGH, "off":GPIO.LOW,
         "g":27, "y":22, "r":17}

    for c in color:
        GPIO.output(d[c],d[state])
    print("LED",color,state)

by using a lookup dictionary: see dict()

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Thanks you! Works flawlessly. But how about if I want to turn on multiple leds at one like `rgy on`? – Nour_Dev Dec 23 '18 at 11:45
  • 1
    @NourTheDev You need to enhance the "ok"-check to also pass multiple color values and loop over all the colors given - see edit. – Patrick Artner Dec 23 '18 at 11:55
3

This approach might help. By taking two inputs with a space in between. You can split the input based on a space or any other character. It will then place the color and state into two different variables.

>>> leds_col,leds_stat = input('Color(r,g,y) and State(on,off):').split(' ')
Color(r,g,y) and State(on,off):g on
>>> print(leds_col)
g
>>> print(leds_stat)
on
>>> 

Alternatively, you can also use comma or any other character as a separator between the color and state.

Anurag A S
  • 725
  • 10
  • 23
2

str.split() will be able to split your single line input into two (or more) variables:

>>> inp = input()
1,2
>>> inp
'1,2'
>>> a, b = inp.split(',')
>>> a
'1'
>>> b
'2'
gosuto
  • 5,422
  • 6
  • 36
  • 57