programming comet tail problems with leds

Thread Starter

jayanath

Joined Dec 11, 2010
17
Above thread is older. cannot reply. I have following code. but all leds are fading and getting on together. no running action. how can i make chaser action with fading effect. i mean comet tail. pls help me?


Rich (BB code):
#include <pic.h> 
#define FADE_RATE 12 
#define INITIAL_WIDTH 128 
void fade()
{
unsigned char rate = FADE_RATE;
unsigned char pulse_width = INITIAL_WIDTH;
unsigned char count;
while(pulse_width){ 
PORTB= 0b11111111; 
for(count=0; count<pulse_width; count++){ 
asm("NOP");
}
PORTB=0b00000000; 
for(; count; count++){ 
asm("NOP"); 
}
if(!rate--){ 
pulse_width--; 
rate = FADE_RATE; 
} 
} 
} 
int main(void) {
char b; 
TRISB = 0b00000000; 
while(1){ 
for(b=0x02; b; b<<=1){ 
PORTB = b; 
fade(b>>1); 
}
for(b=0x40; b; b>>=1){ 
PORTB = b; 
fade(b<<1); 
}
} 
}
 
Last edited by a moderator:

Brownout

Joined Jan 10, 2012
2,390
There is much about your code that I don't understand. Why do you define fade() to take no parameters, but invoke it with parameters. Why do you decrement 'rate' then re-assign it to the default value within the loop?

In order to make the fade 'chase', you'll need to assign each bit individually, and not all at once as you now do. You'll need another loop with another controlling vaiable, and make each bit assignment a function of the controlling variable and the pulse_width variable. Use the new controlling variable as a bit shift in the assignment.
 
Top