jarrah31
Posts: 125
Joined: Wed Oct 10, 2012 10:16 am

RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 4:48 pm

Hi,

I'm hoping to make a glowing snowman to display during Christmas. I've printed a 3d snowman model and have configured an RGB LED to light up the snowman from inside:

Image

I'm now trying to figure out how to gradually cycle a full spectrum of colours that transitions smoothly between them. As I'm no programmer I've been cobbling the code from various internet sources, but it doesn't do what I'm after.

Would someone be able to help please?

This is what I have so far. It fades to the main colours, but then jumps to the next one as it iterates through the for loop.

Code: Select all

import random, time
import RPi.GPIO as GPIO

RUNNING = True
GPIO.setmode(GPIO.BCM)
red = 17
green = 27
blue = 22

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)

def color(R, G, B, on_time):
    RED.ChangeDutyCycle(R)
    GREEN.ChangeDutyCycle(G)
    BLUE.ChangeDutyCycle(B)
    time.sleep(on_time)

    RED.ChangeDutyCycle(1)
    GREEN.ChangeDutyCycle(1)
    BLUE.ChangeDutyCycle(1)

try:
    while RUNNING:
        for x in range(1,-1,-1):
            for y in range(1,-1,-1):
                for z in range(1,-1,-1):
                    print (x,y,z)
                    for i in range(100,-1,-1):
                        color((x*i),(y*i),(z*i), .05)
                    for i in range(0,101):
			color((x*i),(y*i),(z*i), .05)

except KeyboardInterrupt:
    RUNNING = False
    print "\Quitting"

finally:
    GPIO.cleanup()

User avatar
PeterO
Posts: 5159
Joined: Sun Jul 22, 2012 4:14 pm

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 4:54 pm

Goggle for "RGB rainbow". You'll find examples of how to cycle RGB values through the spectrum

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 5:01 pm

PeterO wrote:Goggle for "RGB rainbow".
That was remarkably satisfying. It turned up http://krazydad.com/tutorials/makecolors.php which seems to cover the whole subject.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
PeterO
Posts: 5159
Joined: Sun Jul 22, 2012 4:14 pm

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 5:08 pm

My "Google Fu" is strong today :-)
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jarrah31
Posts: 125
Joined: Wed Oct 10, 2012 10:16 am

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 5:08 pm

True, I did come across that site whilst googling before I posted, but I didn't spend long looking at that page because it was in Javascript. Most GPIO tutorials I've found are based on Python using RPi GPIO, so I'm hoping to stay with that.

I've been looking through this: http://bsou.io/posts/color-gradients-with-python. I will certainly give it a go but I've no idea how to integrate it with turning GPIO pins on/off for example. (Code makes my brain hurt) :)

jarrah31
Posts: 125
Joined: Wed Oct 10, 2012 10:16 am

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 5:21 pm

PeterO wrote:My "Google Fu" is strong today :-)
I have been doing a lot of googling on this to try and find a fairly straightforward copy/paste code that cycles an RGB LED via a Raspberry Pi using GPIO pins. I just couldn't find a solution which is why I thought I'd ask for help on here.

I will continue to look though, I just hoped someone else had already written code to do this, or perhaps there was a Python master lurking on here who can think of the solution in 5 mins. :)

User avatar
PeterO
Posts: 5159
Joined: Sun Jul 22, 2012 4:14 pm

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 5:28 pm

Sorry we didn't meet your "do it all for me" expectations :roll:
PeterO

PS:

Several of the pages that Google finds contain exactly the information you need to get the correct sequence of R,G,B values you need.
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 5:39 pm

jarrah31 wrote:True, I did come across that site whilst googling before I posted, but I didn't spend long looking at that page because it was in Javascript. Most GPIO tutorials I've found are based on Python using RPi GPIO, so I'm hoping to stay with that.
Nothing wrong with Javascript. Also it's relatively easy to transcribe js into python, just knock out a few {} and the explicit variable declarations and push in some left hand whitespace.

The underlying maths and logic doesn't change.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
PeterO
Posts: 5159
Joined: Sun Jul 22, 2012 4:14 pm

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 5:46 pm

DougieLawson wrote: The underlying maths and logic doesn't change.
+1
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

jarrah31
Posts: 125
Joined: Wed Oct 10, 2012 10:16 am

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 5:47 pm

PeterO wrote:Sorry we didn't meet your "do it all for me" expectations :roll:
PeterO
Hmm, not the friendly place I thought it was.

I do understand where you're coming from though, I am a technical person and I do figure out a lot of things for myself. Others see me as an IT geek because of what i know about computers and the tinkering I do. I help a lot of friends/family/neighbours fix their computers. But please do realise, I'm not an expert at everything. I take my car to the garage because I don't know how to fix cars. I go to the doctors because I'm not an expert in medicine. I didn't build my house extension because I don't know how to lay bricks, but I can do plumbing and electrics.

However I am an expert in other areas. For example, I help large companies configure Microsoft Exchange to be fully resilient using storage controllers and backup technologies because I'm good at that. BUT I'm not an expert in Python, so I hoped I could ask for help from someone who is.

As I said earlier, I've already been trying to do this myself given the code example above. It works to an extent, and it demonstrates I have been trying.

I'm sorry for asking for help.....

User avatar
PeterO
Posts: 5159
Joined: Sun Jul 22, 2012 4:14 pm

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 5:51 pm

jarrah31 wrote:
PeterO wrote:Sorry we didn't meet your "do it all for me" expectations :roll:
PeterO
Hmm, not the friendly place I thought it was.
Oh, quite the opposite..... We are very friendly to people who actually take notice of the help they are given and don't just say "You didn't give me the whole solution"....

And you don't need help with python, the code you posted is OK, it just doesn't implement the right sequence of RGB changes, hence you got pointed at the information you need to fix your code.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
Douglas6
Posts: 4783
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 5:54 pm

Probably want to use Python's colorsys library for this

Code: Select all

colorsys.hls_to_rgb(h, l, s)
and range h (hue) from 0 to 1 in your loop. Keep l (lightness) = s (saturation) = a constant 0.5 to start, and adjust for your needs.
Last edited by Douglas6 on Thu Nov 19, 2015 6:03 pm, edited 1 time in total.

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 6:02 pm

jarrah31 wrote: I will continue to look though, I just hoped someone else had already written code to do this, or perhaps there was a Python master lurking on here who can think of the solution in 5 mins. :)
If you hadn't said that I might have been tempted to knock a bit of Javascript into a bit of python for you. But that kind of attitude elicits my TSMYOYO* response.

This forum is run by the RPF but 99% of the small corpus of folks who respond to threads is made up of a group of unpaid volunteers. Expecting the answer handed on a plate won't win you any friends.


*Tough s*** mate you're on your own.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

jarrah31
Posts: 125
Joined: Wed Oct 10, 2012 10:16 am

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 6:16 pm

PeterO wrote:
jarrah31 wrote:
PeterO wrote:Sorry we didn't meet your "do it all for me" expectations :roll:
PeterO
Hmm, not the friendly place I thought it was.
Oh, quite the opposite..... We are very friendly to people who actually take notice of the help they are given and don't just say "You didn't give me the whole solution"....

And you don't need help with python, the code you posted is OK, it just doesn't implement the right sequence of RGB changes, hence you got pointed at the information you need to fix your code.

PeterO
I'm sorry, I take the friendly comment back as it was unfair.
I had already put a fair bit of effort trying to figure it out. I just needed help with the RGB cycling part of the code. I will absolutely take a look at the examples and try to figure it out myself, I just hoped someone had already figured out the answer given that RGB LEDs and Python are quite common, and perhaps knew where the code had already been posted on a website somewhere.

I just don't understand Python well enough to create complex functions and algorithms myself, my brain isn't wired for that. :( Not everyone has the coding mind/ability to figure it out themselves. i will try though. :)

jarrah31
Posts: 125
Joined: Wed Oct 10, 2012 10:16 am

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 6:21 pm

DougieLawson wrote:
jarrah31 wrote: I will continue to look though, I just hoped someone else had already written code to do this, or perhaps there was a Python master lurking on here who can think of the solution in 5 mins. :)
If you hadn't said that I might have been tempted to knock a bit of Javascript into a bit of python for you. But that kind of attitude elicits my TSMYOYO* response.

This forum is run by the RPF but 99% of the small corpus of folks who respond to threads is made up of a group of unpaid volunteers. Expecting the answer handed on a plate won't win you any friends.


*Tough s*** mate you're on your own.
Why is it wrong to ask for help from people who are experts in their field? People ask me for IT solutions all the time in my job, it's why we have experts?
Why did this thread have to turn personal? Im just trying to explain why I was asking for help from people with more knowledge than me. :(

User avatar
Douglas6
Posts: 4783
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 6:32 pm

See my post above.

jarrah31
Posts: 125
Joined: Wed Oct 10, 2012 10:16 am

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 6:36 pm

DougieLawson wrote:
jarrah31 wrote: I will continue to look though, I just hoped someone else had already written code to do this, or perhaps there was a Python master lurking on here who can think of the solution in 5 mins. :)
If you hadn't said that I might have been tempted to knock a bit of Javascript into a bit of python for you. But that kind of attitude elicits my TSMYOYO* response.

*Tough s*** mate you're on your own.
Why? It came from a colleague of mine who is a Perl master and can literally knock up solutions in 5 mins. Others try first, but when stuck they ask him for help and he explains why his solution works so that the other person learns from it. I truly am sorry for offending you as it's the last thing I want to do.

Oh man, what a mess this thread has become. :(

Let me spend some time this evening trying to figure it out as I said I would earlier. There's been some good suggestions I want to try. I will have a look at the JavaScript again, but as a don't understand both languages it will probably take me many evenings to work it out, and fair enough I'll do that. :(

jarrah31
Posts: 125
Joined: Wed Oct 10, 2012 10:16 am

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 6:38 pm

Douglas6 wrote:See my post above.
Thanks Douglas, this is one of the examples I want to try out his evening as it looks very promising.

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 9:05 pm

jarrah31 wrote:Why is it wrong to ask for help from people who are experts in their field? People ask me for IT solutions all the time in my job, it's why we have experts?
Why did this thread have to turn personal? Im just trying to explain why I was asking for help from people with more knowledge than me. :(
Because you appeared to be demanding something. Ask nicely and folks may be interested enough to help.

Nothing personal, we just want folks to do their own research, do their own work and write their own code. You wouldn't expect your university professor to write your code for you, you may expect him/her to give hints/advice/examples of how to employ some interesting technique.

The Javascript stuff has all the theory and all the maths, what's so hard about using that as a design for a python or perl program.

If you have a perl master why not run perl on your RPi, it's got perl5 (revision 5 version 20 subversion 2) built in to Raspbian Jessie.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

jarrah31
Posts: 125
Joined: Wed Oct 10, 2012 10:16 am

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 9:14 pm

DougieLawson wrote:
jarrah31 wrote:Why is it wrong to ask for help from people who are experts in their field? People ask me for IT solutions all the time in my job, it's why we have experts?
Why did this thread have to turn personal? Im just trying to explain why I was asking for help from people with more knowledge than me. :(
Because you appeared to be demanding something. Ask nicely and folks may be interested enough to help.

Nothing personal, we just want folks to do their own research, do their own work and write their own code. You wouldn't expect your university professor to write your code for you, you may expect him/her to give hints/advice/examples of how to employ some interesting technique.

The Javascript stuff has all the theory and all the maths, what's so hard about using that as a design for a python or perl program.

If you have a perl master why not run perl on your RPi, it's got perl5 (revision 5 version 20 subversion 2) built in to Raspbian Jessie.
Thank you, and I'm sorry. I really didn't mean for it to appear demanding.

Perl is even harder to understand so I need to stick with Python to have a fighting chance. :)

I'm working on colorsys for the moment to try and figure out what the numbers are producing based on some examples I found by googling. I'll get there. :)

Code: Select all

hue=0
delta_n_hue=0.001
delta_t_hue=1./120

try:
	while(True):
			x=colorsys.hls_to_rgb(((hue*delta_n_hue)%1.0),0.1,1)
			
			print (x)
			colour=[255*x[0], 255*x[1], 255*x[2]]
			print (colour)
			RED.ChangeDutyCycle(x[0])
			GREEN.ChangeDutyCycle(x[1])
			BLUE.ChangeDutyCycle(x[2])
			time.sleep(0.1)
			hue = hue + delta_t_hue/10

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

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 9:44 pm

I've been debating whether or not to post in this because I do share Dougie and Peter's opinions, however, I'm very pleased to see that everyone's friends again!

For what it's worth, I've used the "wheel_color" method from this code before: https://github.com/adammhaile/RPi-LPD88 ... p/color.py
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: RGB Full Colour Smooth Cycle Help Please

Thu Nov 19, 2015 11:30 pm

jarrah31 wrote: Thank you, and I'm sorry. I really didn't mean for it to appear demanding.
Apology accepted. Now I'm going to have to look harder at that Javascript and see what happens when I try some of that stuff in python (or perl if I'm being very cruel).
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

jarrah31
Posts: 125
Joined: Wed Oct 10, 2012 10:16 am

Re: RGB Full Colour Smooth Cycle Help Please

Fri Nov 20, 2015 10:37 am

elParaguayo wrote:I've been debating whether or not to post in this because I do share Dougie and Peter's opinions, however, I'm very pleased to see that everyone's friends again!

For what it's worth, I've used the "wheel_color" method from this code before: https://github.com/adammhaile/RPi-LPD88 ... p/color.py
Thanks.

I think the problem I'm coming up against is that most of the code I find is designed to work with LED light strips, so I'm struggling to see how I can map that to RPi.GPIO's PWM mode with a max freq of 100.

I think with the colour wheel example I need to map the output (0-256) into 0-100, but before I get that far I'm still working out how to properly use functions (the following code for example errors that Color isn't defined, so I'm googling on how to fix that atm for example):

Code: Select all

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 Color(r, g, b)

try:
	while(True):
		for pos in range(0,385):
			rgbcol = wheel_color(pos)
			print (rgbcol)
			#RED.ChangeDutyCycle(rgbcol[0])
			#GREEN.ChangeDutyCycle(rgbcol[1])
			#BLUE.ChangeDutyCycle(rgbcol[2])
			time.sleep(0.1)

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

Re: RGB Full Colour Smooth Cycle Help Please

Fri Nov 20, 2015 11:08 am

You don't need "Color" in that, it could equally just return the R, G and B values. Just change the line to

Code: Select all

return r, g, b
Then, when you call the function, you'd do:

Code: Select all

r, g, b = wheel_color(100)
It's trivial to convert the output to your desired range. NB, you may not want to use the full 0-100 range (e.g. if you look at the wheel_color function it only outputs values in 0-128 range (despite the fact that RGB values could go up to 255) as, above that, the colours will begin to get washed out and everything may look white).

However, the wheel function does just gives nice transition through the spectrum in RGB values. How you translate that into a PWM setting, I have no idea, as I've never used PWM so I don't know how it works. Assuming that there's a specific setting for various brightnesses of each colour, I would have thought it would be simple to create a function to convert RGB values into the necessary PWM settings.

if it's as simple as changing the duty cycle setting (which is a range of 0-100) then you could just divide the individual rgb values by 2.55 as that would give you the equivalent values in a 0-100 range.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

jarrah31
Posts: 125
Joined: Wed Oct 10, 2012 10:16 am

Re: RGB Full Colour Smooth Cycle Help Please

Fri Nov 20, 2015 11:36 am

elParaguayo wrote:You don't need "Color" in that, it could equally just return the R, G and B values. Just change the line to

Code: Select all

return r, g, b
Then, when you call the function, you'd do:

Code: Select all

r, g, b = wheel_color(100)
Success, thank you very much elParaguayo for pointing me in the right direction. This is what I have (ignore the prints as they were for debugging):

Code: Select all

import random, time
import RPi.GPIO as GPIO
import colorsys
 
RUNNING = True
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
red = 17
green = 27
blue = 22
 
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()

Return to “Python”