RGB Fading Using 3 LEDs and PWM


Results 1 to 28 of 28

Threaded View

  1. #21
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Using your setup, but led are directly connected to the PIC and gnd (i uses an EasyPic 4 board ), you'll use this version
    Code:
    @ __CONFIG  _INTRC_OSC_NOCLKOUT & _MCLRE_OFF  &  _LVP_OFF & _WDT_OFF & _PWRTE_ON  & _BODEN_ON  
    TRISB = 0
    
    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
    
    main:
        for rainbowval = 0 to 255
            gosub rbgrainbow 
        next
        for rainbowval=255 to 0 step - 1
            gosub rbgrainbow 
        next
        goto main
    
    
    rbgrainbow:
        for rbg = 0 to 2
            section = ((rainbowval + ((rbg * 2)*delta))/ delta) // 6 ' this gives what section it is in
            
            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
            Select Case y
                Case 0
                     pwm PORTB.0, rbgarray[y], 1
                case 1
                     pwm PORTB.1, rbgarray[y], 1
                case 2
                     pwm PORTB.2, rbgarray[y], 1
            end select 
        next
        return
    Last edited by mister_e; - 20th February 2007 at 16:56.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

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, 15:50
  2. Slowing Down RGB PWM Routine
    By Tissy in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th March 2005, 09:22
  3. Tidying Up PWM Routine
    By Tissy in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 21st February 2005, 00:26
  4. PWM conflict
    By Srigopal007 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 1st September 2004, 15:18
  5. pics with 3 hardware PWM ports
    By CBUK in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th August 2004, 00: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