Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 2nd July 2009, 01:03 AM   #1
Default GOOD looking PWM LED candle

after seeing other LED candles I wanted something that looked REAL, not just on n off leds.
I left the orginal code in but added //
to change the flicker just change the value of the subtraction of RandomVal
Code:
Device = 18F1320
Clock = 8

Include "IntOSC8.bas" 
Include "RandGen.bas"
Include "Utils.bas" 
Dim TMR2IE As PIE1.1,         // TMR2 interrupt enable
    TMR2IF As PIR1.1,         // TMR2 overflow flag
    TMR2ON As T2CON.2,        // Enables TMR2 to begin incrementing
    Signal_Pin As PORTA.0     // Signal output to frequency meter
 
Dim Red_Pin As PORTB.0,
    Green_Pin As PORTB.1,
    Blue_Pin As PORTB.2,
    Red_Duty As Byte,
    Green_Duty As Byte,
    Blue_Duty As Byte,
    Red_DutyVal As Byte,
    Green_DutyVal As Byte,
    Blue_DutyVal As Byte,
    RandomVal As Byte
   
 
Dim uS As Word,
    mS As Word           
   // ranval(1)as word
Interrupt TMR2_Interrupt()
    High(Signal_Pin)
    Save(0)                   // Back up system variables   
    If TMR2IF = 1 Then        // Check if the interrupt was from TMR2   
        TMR2IF = 0            // Clear the TMR2 interrupt flag
        uS = uS + 50
        If uS >= 1000 Then
            uS = uS - 1000
            Inc(mS)
        EndIf       
        Inc(Red_DutyVal)
        Inc(Green_DutyVal)
        Inc(Blue_DutyVal)
        If Red_DutyVal > Red_Duty Or Red_Duty = 0 Then
            Red_Pin = 0
        Else
            Red_Pin = 1
        EndIf
        If Green_DutyVal > Green_Duty Or Green_Duty = 0 Then
            Green_Pin = 0
        Else
            Green_Pin = 1
        EndIf
        If Blue_DutyVal > Blue_Duty Or Blue_Duty = 0 Then
            Blue_Pin = 0
        Else
            Blue_Pin = 1
        EndIf               
    EndIf                     //
    Restore                   // Restore system variables
    Low(Signal_Pin)
End Interrupt
 
Private Sub TMR2_Initialize()
    TMR2ON = 0                // Disable TMR2
    TMR2IE = 0                // Turn off TMR2 interrupts   
 
    PR2 = 149                 // TMR2 Period register PR2
    T2CON = %00000001         // T2CON 0:1 = Prescale
                              //        00 = Prescale is 1:1
                              //        01 = Prescale is 1:4
                              //        1x = Prescale is 1:16                                 
                              //      3:6 = Postscale              
                              //     0000 = 1:1 postscale
                              //     0001 = 1:2 postscale
                              //     0010 = 1:3 postscale...
                              //     1111 = 1:16 postscale
 
    TMR2 = 0                  // Reset TMR2 Value   
    TMR2IE = 1                // Enable TMR2 interrupts
    TMR2ON = 1                // Enable TMR2 to increment
    Enable(TMR2_Interrupt)
End Sub
 
// Start Of Program...
Low(Red_Pin)
Low(Green_Pin)
Low(Blue_Pin)
Red_Duty = 30
Green_Duty = 0
Blue_Duty = 0
Red_DutyVal = 0
Green_DutyVal = 0
Blue_DutyVal =0
 
uS = 0
mS = 0

RandGen.Initialize(128)       // Initialize the Random Number generator
TMR2_Initialize               // Setup and enable TMR2
 
While True                    // Create an infinite loop
    RandomVal = RandGen.Rand  // Grab a random number from 0 to 255 
    // Select RandomVal         // Find out what colour to increase/decrease
        //Case 0 To 42
             Red_Duty =RandomVal-5 
                //mS = 0
             // (Red_Duty)=Rand(1)
               // Repeat
               // Until mS = 19
               // and Red_Duty>0
           // endif
           
            
        //Case 43 To 84
            If Red_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Red_Duty)
            EndIf
       // Case 84 To 127
             Green_Duty =RandomVal-20 
              //Then  mS = 0
              //  Repeat
              //  Until mS = 19
              //  Inc(Green_Duty)
           // EndIf
       // Case 128 To 170
            if Green_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Green_Duty)
            EndIf
      //  Case 170 To 212
             Blue_Duty = RandomVal-30
             // Then  mS = 0
              //  Repeat
              //  Until mS = 19
              //  Inc(Blue_Duty)
           // EndIf
       // Case 212 To 255
            If Blue_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Blue_Duty)
            EndIf
            Wend
          
         End
any suggestions or tweeking is appreciated
MrDEB is offline  
Old 2nd July 2009, 01:15 AM   #2
Default

You've become quite the programmer MrDeb, nice work.
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com/
blueroomelectronics is online now  
Old 2nd July 2009, 01:44 AM   #3
Default I try

the original code link was posted by be80be
I just majorly edited it.
just kept plugging away, trying LOTS of different things until I hiton using RandomVal then subtracting from it.
BAM it works
just needs a little clean up and tweeking.
I owe most of my programming to be80be and Pommie as well as others on this site as well as
Welcome
hopefully someday I will grasp the whole meaning of all this code.
MrDEB is offline  
Old 2nd July 2009, 01:57 AM   #4
Default

MrDeb That's what i'm talking about the looks real good you get your Pro badge tomorrow
in the mail great job. You got flow now. I like it.
be80be is offline  
Old 2nd July 2009, 02:58 AM   #5
Default Thanks all

just change the -5 to -25, it looks better (a better flicker)
now to get back to the critter ridder.
I got a code that will compile but havn't actually tried it out.
I will be looking for that pro badge in the mail
Hey be80be have you tried seeing this code in action.
I only have 3 blue LEDs but maybe tomorrow will try for a red, bkue and yellow LEDs anmd a styrofoam cup
MrDEB is offline  
Old 2nd July 2009, 03:11 AM   #6
Default

I was going to load her up now and see what how it looks
be80be is offline  
Old 2nd July 2009, 03:45 AM   #7
Default

I tried it the Red_Duty led works real good the blue and green don't work but it would
make a candle with the red led just need to get blue and green to work
be80be is offline  
Old 2nd July 2009, 12:06 PM   #8
Default WHY don't the blue n green work??

I have LEDS on pins 8,9,17,18
the pins 8,9,17 are the ones I have to come on.
????????????
if you can't get all to work then your worrying me.
hopefully there is not a problem with my code??
keep me updated.
MrDEB is offline  
Old 2nd July 2009, 01:24 PM   #9
Default video if I did it right

YouTube - LED candle using PWM

I only have blue connected to test board.
final will have blue, red and green behind an obscure yellow glass
MrDEB is offline  
Old 2nd July 2009, 02:09 PM   #10
Default

did darth vader shoot that video?
OutToLunch is offline  
Reply

Tags
candle, good, led, pwm

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
LED candle effect using PWM MrDEB Micro Controllers 10 26th June 2009 12:26 PM
Led candle with 16f84a gregted Micro Controllers 4 3rd March 2009 02:01 PM
And you though the candle and nail hoax was bad! Andy1845c Chit-Chat 62 25th November 2007 10:41 PM
Candle Light Dudeler General Electronics Chat 2 29th April 2004 07:13 PM
Way to Light A candle yojo77 General Electronics Chat 10 23rd December 2003 03:30 AM



All times are GMT. The time now is 04:40 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker