RGB Fading Using 3 LEDs and PWM


Results 1 to 28 of 28

Threaded View

  1. #9
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154


    Did you find this post helpful? Yes | No

    Default

    I have found this piece of code, which i think may do what i need. How do i change the outputs so that it fades the LEDs on RC0 - RC2 and not RB5 through to RB7?

    Is there anything else that'll need modifiying if i am using a PIC16F876.

    Many thanks.
    Code:
    ' PicBasic Pro program for fading RGB LED's.  
    ' Note that this program uses the 
    ' Basic Stamp-equivalent pin numbers, 5 through 7.  
    ' These correspond to pins RB5 through RB7.
    
    include "modedefs.bas"
    
    DEFINE OSC 4
    DEFINE ADC_BITS 8
    DEFINE ADC_CLOCK 3
    DEFINE SAMPLEUS 50
    
    
    TRISB = %00000000
    TRISA = %11111111
    TRISD = %00000000
    
    ADCON1 = %00000010
    
    rbgArray     var byte[3]  ' holds the rgb values in this case the led is rbg 
    
    
    x                var byte
    y                var byte
    adcVar       var byte
    rbg             var byte        'which value we are changing in the rbg array
    rainbowVal      var byte        'the current value to produce rainbow
    Delta               var byte        ' find the size of each section in the 6stage rainbow
    Section            var byte        'which section it is in
    
    maxValue        con 255       'the maximum input value for generating rainbow
    
    delta   =   maxValue/6
    
    y       =   0
    
    
    for x = 5 to 7
    
         high x
         pause 1000
         low x
    
    next
    main:
    
    
    
    adcin 0, rainbowval
    'serout2 portc.6, 16468, ["rainbow val: ", dec rainbowval, 13, 10]
    
    
    gosub rbgrainbow   
    
    
    
    goto main
    
    
    rbgrainbow:
    
        for rbg = 0 to 2
            section = ((rainbowval + ((rbg * 2)*delta))/ delta) // 6   ' this gives what section it is in
            'serout2 portc.6, 16468, ["rbg: ", dec rbg, 13,10,"section: ", dec section, 13, 10]
            select case section
                case 0
                    rbgarray[rbg] = (rainbowval // delta)*6 'this is how far it has gone in its section
                case 1
                    rbgarray[rbg] = 255
                case 2
                    rbgarray[rbg] = 255
                case 3
                    rbgarray[rbg] = 255 - ((rainbowval //delta)*6)
                case 4
                    rbgarray[rbg] = 0
                case 5
                    rbgarray[rbg] = 0
                case 6          'this one is for pics bad math
                    rbgarray[rbg] = 0
            end select
        next
    
            gosub ledpwm       
    
    return
    
    ledPWM:
    for y = 0 to 2
        pwm 5 + y, rbgarray[y], 1
        'serout2 portc.6, 16468, ["color",dec y, ": ", dec rbgarray[y], 13, 10]
    next
    
    return
    Last edited by ScaleRobotics; - 17th June 2010 at 16:14. Reason: added code tags

Similar Threads

  1. RGB Fading Using 3 LEDs and PWM
    By _abello in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 19th April 2006, 16:50
  2. Slowing Down RGB PWM Routine
    By Tissy in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th March 2005, 10:22
  3. Tidying Up PWM Routine
    By Tissy in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 21st February 2005, 01:26
  4. PWM conflict
    By Srigopal007 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 1st September 2004, 16:18
  5. pics with 3 hardware PWM ports
    By CBUK in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th August 2004, 01:14

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts