RGB rainbow transition by PWM not working for me
Posted: Wed May 22, 2019 11:48 am
I'm trying to use the code kindly provided by another forum user here: viewtopic.php?t=126405
The code is for driving RGB LEDs through the rainbow via PWM.
The problem is that the code doesn't seem to work for me. I get the output printing to the terminal window but I don't get the expected colour transitions. I know that my pins are connected correctly because some other code (which does random transitions rather than deliberate rainbow transitions) does work.
Here's the code that I'm having a problem with:
and here's the code that is working okay for me:
Can anyone advise what I should do to find the cause of the problem?
Edit: could it be because I don't have colorsys? If that was the case, the code wouldn't run at all, right? When I type "pip install colorsys", I get the error:
so I don't actually know whether I have it installed but I would expect an error when I try to run the script if it was unable to import a required library, right?
The code is for driving RGB LEDs through the rainbow via PWM.
The problem is that the code doesn't seem to work for me. I get the output printing to the terminal window but I don't get the expected colour transitions. I know that my pins are connected correctly because some other code (which does random transitions rather than deliberate rainbow transitions) does work.
Here's the code that I'm having a problem with:
Code: Select all
import random, time
import RPi.GPIO as GPIO
import colorsys
RUNNING = True
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
red = 8
green = 10
blue = 12
GPIO.setup(red, GPIO.OUT)
GPIO.setup(green, GPIO.OUT)
GPIO.setup(blue, GPIO.OUT)
Freq = 100
RED = GPIO.PWM(red, Freq)
RED.start(100)
GREEN = GPIO.PWM(green, Freq)
GREEN.start(100)
BLUE = GPIO.PWM(blue, Freq)
BLUE.start(100)
outval = 128
def wheel_color(position):
"""Get color from wheel value (0 - 384)."""
if position < 0:
position = 0
if position > 384:
position = 384
if position < 128:
r = 127 - position % 128
g = position % 128
b = 0
elif position < 256:
g = 127 - position % 128
b = position % 128
r = 0
else:
b = 127 - position % 128
r = position % 128
g = 0
return r, g, b
try:
while(True):
for pos in range(0,385):
r, g, b = wheel_color(pos)
print (r, g, b)
percenttestr = (r/128.0)*100.0
percenttestg = (g/128.0)*100.0
percenttestb = (b/128.0)*100.0
print (percenttestr)
print (percenttestg)
print (percenttestb)
RED.ChangeDutyCycle(percenttestr)
GREEN.ChangeDutyCycle(percenttestg)
BLUE.ChangeDutyCycle(percenttestb)
time.sleep(0.1)
except KeyboardInterrupt:
GPIO.cleanup()
Code: Select all
import RPi.GPIO as GPIO
import threading
import time
import random
R = 8
G = 10
B = 12
PINS = [R,G,B]
def initialize_gpio():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(PINS, GPIO.OUT, initial=GPIO.LOW)
def color_test(channel, frequency, speed, step):
p = GPIO.PWM(channel, frequency)
p.start(50)
while True:
for dutyCycle in range(0, 101, step):
p.ChangeDutyCycle(dutyCycle)
time.sleep(speed)
for dutyCycle in range(100, -1, -step):
p.ChangeDutyCycle(dutyCycle)
time.sleep(speed)
def color_test_thread():
threads = []
threads.append(threading.Thread(target=color_test, args=(R, 200, 0.02, 1)))
threads.append(threading.Thread(target=color_test, args=(G, 200, 0.035, 1)))
threads.append(threading.Thread(target=color_test, args=(B, 200, 0.045, 1)))
for t in threads:
t.daemon = True
t.start()
for t in threads:
t.join()
def main():
try:
initialize_gpio()
print("\nPress ^C (control-C) to exit the program.\n")
color_test_thread()
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
if __name__ == '__main__':
main()
Can anyone advise what I should do to find the cause of the problem?
Edit: could it be because I don't have colorsys? If that was the case, the code wouldn't run at all, right? When I type "pip install colorsys", I get the error:
Code: Select all
[email protected]:~ $ pip install colorsys
Collecting colorsys
Could not find a version that satisfies the requirement colorsys (from versions: )
No matching distribution found for colorsys