programming comet tail problems with leds

Discussion in 'General Electronics Chat' started by jayanath, Dec 25, 2013.

  1. jayanath

    Thread Starter New Member

    Dec 11, 2010
    17
    0
    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?


    Code ( (Unknown Language)):
    1.  
    2. #include <pic.h>
    3. #define FADE_RATE 12
    4. #define INITIAL_WIDTH 128
    5. void fade()
    6. {
    7. unsigned char rate = FADE_RATE;
    8. unsigned char pulse_width = INITIAL_WIDTH;
    9. unsigned char count;
    10. while(pulse_width){
    11. PORTB= 0b11111111;
    12. for(count=0; count<pulse_width; count++){
    13. asm("NOP");
    14. }
    15. PORTB=0b00000000;
    16. for(; count; count++){
    17. asm("NOP");
    18. }
    19. if(!rate--){
    20. pulse_width--;
    21. rate = FADE_RATE;
    22. }
    23. }
    24. }
    25. int main(void) {
    26. char b;
    27. TRISB = 0b00000000;
    28. while(1){
    29. for(b=0x02; b; b<<=1){
    30. PORTB = b;
    31. fade(b>>1);
    32. }
    33. for(b=0x40; b; b>>=1){
    34. PORTB = b;
    35. fade(b<<1);
    36. }
    37. }
    38. }
    39.  
     
    Last edited by a moderator: Dec 26, 2013
  2. bertus

    Administrator

    Apr 5, 2008
    15,649
    2,348
    Hello,

    You just hijacked a thread created by an other user:
    LED lighting

    Now you have a thread of your own.

    Bertus
     
  3. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    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.
     
Loading...