• 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

Suraj143

Active Member
Thread starter #1
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?
 

ronsimpson

Well-Known Member
Most Helpful Member
#3
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)
 

Suraj143

Active Member
Thread starter #4
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


Scale_Colour:
Temp_Green     = (Duty/255) * Green

Temp_Red     = (Duty/255) * Red

Temp_Blue     = (Duty/255) * Blue
The problem is in the "Scale_Colour" subroutine it needs 8X8 multiplication & 16by8 division routines, which may slow dowsn the Neo pixel update process :(
 
#5
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
 

ronsimpson

Well-Known Member
Most Helpful Member
#6
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.
 
#7
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
 

Latest threads

EE World Online Articles

Loading

 
Top