Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

RGB Fade - Code Problem

Status
Not open for further replies.

Suraj143

Active Member
I'm doing a neo pixel project & needs to do some patterns.Out of them there are fading patterns like, the colour must start from zero brightness to the full brightness & vise versa.

For the colurs like Red,Green,Blue it is straightforward. I can increment or decrement its value form 0 to 255.
Code:
Colour_2        movlw        .255        ;Red
            movwf        Red
            movlw        00h
            movwf        Green
            movlw        00h
            movwf        Blue
            retlw        00h

The problem is the colours like Orange, Pink,Sea Blue etc... (Not promary colurs) the vlaues are not reaching to 255 in its variables.
See the below orange colour, its Red Portion is reaching to 255, but its Green portion will end on 16.

Code:
Colour_6        movlw        b'00010000'            ;Orange
            movwf        Green
            movlw        b'11111111'
            movwf        Red
            movlw        b'00000000'
            movwf        Blue
            retlw        00h

Is there anyway to Fade colours on athis situation?
 
I do not know what hardware you have but I will try;

You know that Red goes from 0 to 255. (Green or Blue) Very simple.

If you have a color that is Red on at 100% and Green on at 50% and Blue on at 0%. (Red/Green color) R=255, G=127, B=0
For 1/2 brightness R=127, G=63, B=0.
For 1/4 brightness R=63, G=31, B=0.
for 1/8 brightness R=31, G15, B=0.
R=15, G=7, B=0
R=7, G=3, B=0

So you need to have a number for "brightness" and three numbers for color, "R", "G", "B". Where R,G,B=color.
output Red = (Brightness X R), output Green = (Brightness X G), output Blue = )Brightness X B)
 
HI, I found a solution.

Code:
Load_Orange
            movlw    .16
            movwf    Green
            movlw    .255
            movwf    Red
            clrf    Blue
            clrf    Duty

Fade_Up_Loop 
            incf    Duty,F
            movf    Duty,W
            btfsc    STATUS,Z
            goto   Fade_Dn
            call    Scale_Colour
            call    Send_Colour
            call    Delay_XX
            goto    Fade_Loop

Fade_Dn        movlw    .255
            movwf    Duty
Fade_Dn_    Loop    decf    Duty,F
            movf    Duty,W
            btfsc    STATUS,Z
            goto    Fade_Up_Loop
            call    Scale_Colour
            call    Send_Colour
            call    Delay_XX
            goto    Fade_Loop

The problem is in the "Scale_Colour" subroutine it needs 8X8 multiplication & 16by8 division routines, which may slow dowsn the Neo pixel update process :(
 
Last edited:
The problem with trying to fade colours is that scaling a small value through 256 steps [0-255] means the output only changes occasioanlly as there are no fractions.

eg. if one colour ratio is RGB 255, 32,32 the red will reduce every step, but green and blue will only change every 8th step, giving varying hues.

ps. You never need to do a literal division by 256 (or 255, which is near enough the same in this use) in code.
eg. Just multiply the colour by the duty then use the high byte of the result, that is the full result value / 256.

For intermediate power-of-two division (or multiplication) do the appropriate shift or rotate.

Also -
Some PICs (PIC18 & higher?) have a hardware multiplier and can do 8x8 operations in a single instruction, eg. "MULWF"; does the one you are using? That could pretty much eliminate all the scaling delays.

http://technology.niagarac.on.ca/staff/mboldin/18F_Instruction_Set/MULWF.html
 
eg. if one colour ratio is RGB 255, 32,32 the red will reduce every step, but green and blue will only change every 8th step, giving varying hues.
The color difference between any two LED next to each other is far greater than 1/255.
The early flat TVs I designed had only 8 bit X3 video and no one noticed.
Some of the advertising signs that show video have 6 bit video. When your are driving down the road there is not time to notice the video is not of quality.
 
This is how I did mine:
Code:
'ansel = 0
'cmcon0 = 7

cmcon  = 7

trisio = %000000
gpio   = %000000
x var byte
y var byte
z var byte
xflag var bit
yflag var bit
zflag var bit
 x=0
 y=0
 z=0
 xflag=1
 yflag=1
 zflag=1

main:
 pwm gpio.0,x,1               'blue
 if xflag = 1 then x=x+1
 if xflag = 0 then x=x-1
 if x = 255 then xflag = 0    '3,627,120 cycles with 255, 254, 252
 If x = 0   then xflag = 1

 pwm gpio.1,y,1               'green
 if yflag = 1 then y=y+2
 if yflag = 0 then y=y-1
 if y = 254 then yflag = 0   
 If y = 0   then yflag = 1
 
 pwm gpio.2,z,1               'red
 if zflag = 1 then z=z+3
 if zflag = 0 then z=z-1
 if z = 252 then zflag = 0
 If z = 0   then zflag = 1
 
 if x = 0 and y = 0 and z = 0 then finito
 goto main
 
finito:
 pause 2000
 goto main
 
HI Thanks for the Codes & IDeas.

I'm in the last part of my project & stucked on it.I want to do a rainbow colour fade.See the video, it has 8 colums (8channels).I want same thing to do.


Needs to do the same fade to my 8 LEDs.Did a colour test on a strip & the values are in the excel table.

I want an idea how to shift colours in a fading manner...!!!
 

Attachments

  • Col Chart.png
    Col Chart.png
    8.4 KB · Views: 247
For simplicity I scale down the 255(R) 255(G) 255(B) values to 64 levels (64 levels indirectly represent full 8 bits using another table).So the code needs to deal with 64 levels.
 

Attachments

  • Col_2.png
    Col_2.png
    5 KB · Views: 251
I want an idea how to shift colours in a fading manner...!!!
Shift with out fade: A, B, C, ..... is time
A) colors =1,2,3,4,5,6,7,8 B) colors = 8,1,2,3,4,5,6,7 C)colors = 7, 8,1,2,3,4,5,6 ........... you know how to do this.

Shift with 1bit fade:
A) colors =1,2,3,4,5,6,7,8 B) colors = [1/2+8/2], [2/2+1/2],[3/2+2/2],[4/2+3/2],[5/2+4/2],[6/2+5/2],[7/2+6/2],[8/2+7/2] C)colors = 8,1,2,3,4,5,6,7
For time A you will output 1,2,3,4,5,6,7,8.
For time B you need some colors that are in between A & C. (find the average of A &C)
For time C you will output 8,1,2,3,4,5,6,7

For a 2 bit fade:
A) colors =1,2,3,4,5,6,7,8
B) [1*0.75+8*0.25] The colors will be mostly like A and a little bit like E
C) [1*0.5+8*0.5] The colors will be in the middle of A and E
D) [1*0.25+8*0.75] The colors will be a little like A and mostly like E
E)colors = 8,1,2,3,4,5,6,7

That is confusing. I hope you understand. What I want to do is insert steps that are 1/2 of a step. (or 1/4 of a step or 1/8 of a step)
 
Hi Ron thanks for your idea,

I did a hard coding & it worked.This is how I did,
Arranged the 8 colours in an excel sheet & find out the maximum increment from one colour to the other colour. For easy coding I found this value is 128.You can see in every colour change there is 128 levels. I incremented a variable upto 128 & compare with the "To Be Colour Variables".

Pretty long subroutine, but somehow it worked.

Note: That For colour Orange the HTML cololur values are RGB"255, 128, 000" , But for neopixels the orange should have RGB"255, 32, 000".

While colour changing I cannot see the colour orange is appearing.Because out of 128 fading levels the "Green 32 point" is 1/4th of my duty (128).
 

Attachments

  • Colour chrt.png
    Colour chrt.png
    6.1 KB · Views: 239
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top