0

I'm currently creating a program to make it possilble to control a multi-colour-LED with a python GUI. The GUI itself and everything else works fine, I'm just still having problems with creating a switch-function. I got a simple configuration for the Multi colour LED:

Pin 19: to green
Pin 21: to blue
Pin 23: to red
Pin 25: works as the GND for the LED

Now I have got this code:

import RPi.GPIO as GPIO
import time, sys

green = 0
blue = 0    #those are the status of the single LED inside of the 
red = 0     #multi colour LED(being off, so 0, at the start)

GPIO.setmode(GPIO.BOARD)

GPIO.setup(19, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)

GPIO.output(19, green)
GPIO.output(21, blue)
GPIO.output(23, red)

def switch(pin, colour):
    colour = not colour
    GPIO.output(pin, colour)
    return

try:
    while True:
        switch(19, green)
        time.sleep(0.5)
        switch(21, blue)
        time.sleep(0.5)
        switch(23, red)
        time.sleep(0.5)
except KeyboardInterrupt:
    GPIO.cleanup()
    sys.exit()

My main focus is on the switch function, since it can turn the single LED's on but can't turn them off again.

What could be the problem here and how to solve it?

0

I think you're assuming that the line colour = not colour is affecting the variable passed in? It's not: colour, being a parameter, is a local variable and won't affect the value of the globals red, green, or blue.

You probably want to return the altered value of colour and assign it to whatever you passed in. For example:

import RPi.GPIO as GPIO
import time, sys

green = 0
blue = 0    #those are the status of the single LED inside of the 
red = 0     #multi colour LED(being off, so 0, at the start)

GPIO.setmode(GPIO.BOARD)

GPIO.setup(19, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)

GPIO.output(19, green)
GPIO.output(21, blue)
GPIO.output(23, red)

def switch(pin, colour):
    colour = not colour
    GPIO.output(pin, colour)
    return colour

try:
    while True:
        green = switch(19, green)
        time.sleep(0.5)
        blue = switch(21, blue)
        time.sleep(0.5)
        red = switch(23, red)
        time.sleep(0.5)
except KeyboardInterrupt:
    GPIO.cleanup()
    sys.exit()

At this point I'll also shamelessly plug our GPIO Zero library by providing the equivalent code using its RGBLED class:

from gpiozero import RGBLED
from time import sleep

led = RGBLED(11, 10, 9) # GPIO Zero uses BCM pin numbering only
while True:
    led.green = not led.green
    sleep(0.5)
    led.blue = not led.blue
    sleep(0.5)
    led.red = not led.red
    sleep(0.5)
  • Is there a possibility to affect the global variable? – h_e_u_r_e_k_a Aug 23 '16 at 19:09
  • To affect the variable that's passed in: no. Python is strictly call-by-value (although you can pass a mutable object like a list in which case the function can affect the value of the mutable object). You can explicitly modify a global via the global statement but that wouldn't help you here as you can't specify the name of the global to affect as a variable. – Dave Jones Aug 23 '16 at 21:10
  • Silly me - just noticed you're not doing a simple switch between red, green, and blue - I'll fix the RGBLED version... – Dave Jones Aug 23 '16 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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