setting values and percentages


Closed Thread
Results 1 to 38 of 38

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Posts
    583

    Default setting values and percentages

    Hi,

    I'm in the process of developing my LED Aquarium light controller a stage further and smoothing out the pulse width modulation by using 0 - 4095 steps rather than the current 255 setps. I have the leds fading nicely when using preset hard coded values, but I want to be able to program the maximum setting for brightness via the LCD as a percentage between 0 and 100 %. When I used 255 steps the following code worked

    Code:
    brightness:
     
    Lcdout $FE,2
    LCDOUT $FE,$80,"Set Max Brightness"
    IF H_butt = 0 THEN GOSUB up
    IF M_butt = 0 THEN GOSUB down
    If (maxbright * 100)/255 = 100 then
    LCDOUT $FE,$C0,"Channel 1 "
    LCDOut $FE,$D4,dec3 (maxbright *100)/255,"%"
    endif
    
    If (maxbright * 100)/255 =0 or (maxbright * 100)/255 <10 then
    LCDOUT$FE,$C0,"Channel 1 "
    LCDOut $FE,$D4,dec1 (maxbright *100)/255,"% "
    endif
    
    if (maxbright * 100)/255 >=10 and (maxbright * 100)/255 <100 then
    LCDOUT$FE,$C0,"Channel 1 "
    LCDOut $FE,$D4,dec2 (maxbright *100)/255,"%   "
    endif
        B_max = maxbright 
    write 8,b_max      
        
    If S_butt = 0 then
        pause 200 
        goto brightness2
    endif 
    goto brightness
    But I'm stumped trying to convert this to use 4095 steps. Replacing the 255 with 4095 seemed the logical answer, but that didn't work. Changing the < = or > values (ie 10% would be 409.5) didn't work either.

    Any suggestions ?

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,640


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    you will find that the led 'brightness' will not be a linear progression over the full 4096 steps anyway . its probably easier to have a finite number of brightness levels in a lookup table (how many brightness levels do you think the average person could differentiate between?)
    try starting with 20 levels [ 0=off ,( and 18 between ) , 4095=full on] , those in between levels could be established by observation

  3. #3
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,154


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Wouldn't he be better just staying with 255? Or am I missing something obvious?

    Robert

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,640


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    maybe malcom could refresh us on the issue , but as I understand it
    1.the external pwm chip has 4096 steps
    2.the loop update time is in 1 sec intervals
    3. the desired fade in /out period divided by the loop time yields too few "pwm" steps for a smooth looking transition if you simply divide 4096 by the number of available steps

    my theory is that if the steps are chosen well the effect may be smoother

  5. #5
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Quote Originally Posted by richard View Post
    maybe malcom could refresh us on the issue , but as I understand it
    1.the external pwm chip has 4096 steps
    2.the loop update time is in 1 sec intervals
    3. the desired fade in /out period divided by the loop time yields too few "pwm" steps for a smooth looking transition if you simply divide 4096 by the number of available steps

    my theory is that if the steps are chosen well the effect may be smoother
    Close

    With a friends help I've implemented a routine using the internal timers to get timings under a second.

    Code:
    ASM
    INT_LIST  macro   ;  IntSource,   Label,        Type, ResetFlag?
            INT_Handler   TMR1_INT,  _ToggleLED1,   ASM,  yes        
        endm
        INT_CREATE                                  ; Creates the interrupt processor
    ENDASM
    
    T1CON = %00000001                               ; free-running, 1:1 prescaler
    TMR1H = %11111111
    TMR1L = %11111011
    @   INT_ENABLE   TMR1_INT                       ; enable Timer1 interrupts
    And

    Code:
    ToggleLED1:
    TMR1H = %11111111
    TMR1L = %11111011
    pcaPwmValue0=pcaPwmValue0+1 
    pcaPwmValue1=pcaPwmValue1+1 
    @ INT_RETURN
    I can go from 0 - 4095 in less than two minutes, which helps in test runs.

    The ramp up is controlled by the case statement

    Code:
    case DAWN
    lcdout $FE,$80+15,"DAWN "  
    if pcaPwmValue0 => blue_delay_in then 
    B_PWM=B_PWM+1
    pcaPwmValue0 = 0 
    endif
    if B_PWM => B_MAX then   
    Blue_Day_Cycle = DAY
    endif
    The value for the pulse width is then written to the PCA chip in the main loop

    Code:
    pcaChannel = 0                                              ;Set a PWM channel
    lcdout $FE,$80,"CH1"
    i2cControl = $6 + 4*pcaChannel                              ;LED0_ON_L $6
    I2CWRITE SDApin,SCLpin,i2cWriteAddress,i2cControl,[0,0,0,B_PWM ]
    I have everything working fine, when using pre-set values for the fade in times and brightness and can do a full dawn, day, dusk, night cycle in five minutes, I just need to be able to sort out the manual settings for adjusting the the brightness so I can enter 0-100% rather than 0 - 4095 on the LCD

    Hope this additional information helps

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    How about
    Code:
    PWMValue = Percentage */ 10483
    ?

  7. #7
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Bit of a fudge, but it works well enough

    Code:
    '*******************************************************************************
    ' Set Brightness levels for each channel
    
    
    brightness:
    
    B_max = (4095/100)*maxbright
    
    Lcdout $FE,2
    LCDOUT $FE,$80,"Set Max Brightness"
    IF H_butt = 0 THEN GOSUB up
    IF M_butt = 0 THEN GOSUB down
    
    If maxbright  = 100 then
    LCDOUT $FE,$C0,"Channel 1 "
    LCDOut $FE,$D4,#maxbright,"%"
    endif
    
    If maxbright  =0 or maxbright <10 then
    LCDOUT$FE,$C0,"Channel 1 "
    LCDOut $FE,$D4,dec1 maxbright,"% "
    endif
    
    if maxbright >=10 and maxbright <100 then
    LCDOUT$FE,$C0,"Channel 1 "
    LCDOut $FE,$D4,dec2 maxbright,"%   "
    endif
         
    If S_butt = 0 then
        pause 100 
        goto brightness2
    endif 
    goto brightness
    
    
    
    brightness2: 
     
    W_max = (4095/100)*maxbright
    
    Lcdout $FE,2
    LCDOUT $FE,$80,"Set Max Brightness"
    IF H_butt = 0 THEN GOSUB up
    IF M_butt = 0 THEN GOSUB down
    
    If maxbright  = 100 then
    LCDOUT $FE,$C0,"Channel 2 "
    LCDOut $FE,$D4,#maxbright,"%"
    endif
    
    If maxbright  =0 or maxbright <10 then
    LCDOUT$FE,$C0,"Channel 2 "
    LCDOut $FE,$D4,dec1 maxbright,"% "
    endif
    
    if maxbright >=10 and maxbright <100 then
    LCDOUT$FE,$C0,"Channel 2 "
    LCDOut $FE,$D4,dec2 maxbright,"%   "
    endif
    
    pause 200
    If S_butt = 0 then
        pause 100 
        LCDOUT $FE,1   
        goto mainmenu
    endif
    goto brightness2
    
    up:
        maxbright = maxbright + 1
        IF maxbright >= 100 THEN maxbright = 100
        pause 250
    RETURN
    down:
        maxbright = maxbright - 1
        IF maxbright <= 0 THEN maxbright = 0
        pause 250
    RETURN
    Again, the lack of floating point maths in PBP means the calculations are not exact, but this way every 1% increment equals a block of 41 steps on the PCA chip, so 50% brightness = 2050 but that's close enough to the 2047 is should be and to be honest I doubt if the human eye could distinguish the difference

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Hi,
    If it works it works.
    If you want it a little better PWMValue = Percentage */ 10483 will give you 2047 at 50%.

    /Henrik.

  9. #9
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Quote Originally Posted by Scampy View Post
    Hi,

    I'm in the process of developing my LED Aquarium light controller a stage further and smoothing out the pulse width modulation by using 0 - 4095 steps rather than the current 255 setps. I have the leds fading nicely when using preset hard coded values, but I want to be able to program the maximum setting for brightness via the LCD as a percentage between 0 and 100 %. When I used 255 steps the following code worked

    Code:
    brightness:
     
    Lcdout $FE,2
    LCDOUT $FE,$80,"Set Max Brightness"
    IF H_butt = 0 THEN GOSUB up
    IF M_butt = 0 THEN GOSUB down
    If (maxbright * 100)/255 = 100 then
    LCDOUT $FE,$C0,"Channel 1 "
    LCDOut $FE,$D4,dec3 (maxbright *100)/255,"%"
    endif
    
    If (maxbright * 100)/255 =0 or (maxbright * 100)/255 <10 then
    LCDOUT$FE,$C0,"Channel 1 "
    LCDOut $FE,$D4,dec1 (maxbright *100)/255,"% "
    endif
    
    if (maxbright * 100)/255 >=10 and (maxbright * 100)/255 <100 then
    LCDOUT$FE,$C0,"Channel 1 "
    LCDOut $FE,$D4,dec2 (maxbright *100)/255,"%   "
    endif
        B_max = maxbright 
    write 8,b_max      
        
    If S_butt = 0 then
        pause 200 
        goto brightness2
    endif 
    goto brightness
    But I'm stumped trying to convert this to use 4095 steps. Replacing the 255 with 4095 seemed the logical answer, but that didn't work. Changing the < = or > values (ie 10% would be 409.5) didn't work either.

    Any suggestions ?
    This code displays the maxbright value which comes from the up/down routines could you post these? Also obviously maxbright has to be a word?

  10. #10
    Join Date
    May 2013
    Location
    australia
    Posts
    2,640


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    just notice your brackets are still incorrect too . if you use multiple logical conditions in if statements they must be parenthesized
    eg
    If (maxbright * 100)/255 =0 or (maxbright * 100)/255 <10 then

    should be

    If ((maxbright * 100/255 =0 ) or ( (maxbright * 100/255 <10 ) then

  11. #11
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Quote Originally Posted by Scampy View Post
    When I used 255 steps the following code worked
    Richard,

    I am puzzled because like you I believe the parenthesis are required but in this case apparently not. Certainly they are for other software and therefore I use them as you do as "good practice".

  12. #12
    Join Date
    May 2013
    Location
    australia
    Posts
    2,640


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    with out the brackets you rely on the "operator precedence" order that the compiler will use , its often not what you really want . best bet is to always use brackets it eliminates the doubt

    ps I typed reply #6 wrong left the closing brackets after 100's out
    should be
    If ((maxbright * 100)/255 =0 ) or ( (maxbright * 100)/255 <10 ) then
    Last edited by richard; - 29th June 2014 at 11:34.

  13. #13
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: setting values and percentages

    Hi Guys and thanks for the input.

    As requested here is the complete brightness routine that works when using Darrels software PWM.

    Code:
    brightness:
     
    Lcdout $FE,2
    LCDOUT $FE,$80,"Set Max Brightness"
    IF H_butt = 0 THEN GOSUB up
    IF M_butt = 0 THEN GOSUB down
    If (maxbright * 100)/255 = 100 then
    LCDOUT $FE,$C0,"Channel 1 "
    LCDOut $FE,$D4,dec3 (maxbright *100)/255,"%"
    endif
    
    If (maxbright * 100)/255 =0 or (maxbright * 100)/255 <10 then
    LCDOUT$FE,$C0,"Channel 1 "
    LCDOut $FE,$D4,dec1 (maxbright *100)/255,"% "
    endif
    
    if (maxbright * 100)/255 >=10 and (maxbright * 100)/255 <100 then
    LCDOUT$FE,$C0,"Channel 1 "
    LCDOut $FE,$D4,dec2 (maxbright *100)/255,"%   "
    endif
        B_max = maxbright 
    write 8,b_max      
        
    If S_butt = 0 then
        pause 200 
        goto brightness2
    endif 
    goto brightness
    
    brightness2: 
     
    Lcdout $FE,2
    LCDOUT $FE,$80,"Set Max Brightness"
    IF H_butt = 0 THEN GOSUB up
    IF M_butt = 0 THEN GOSUB down
    If (maxbright * 100)/255 = 100 then
    LCDOUT $FE,$C0,"Channel 2 "
    LCDOut $FE,$D4,dec3 (maxbright *100)/255,"%"
    endif
    
    If (maxbright * 100)/255 =0 or (maxbright * 100)/255 <10 then
    LCDOUT$FE,$C0,"Channel 2 "
    LCDOut $FE,$D4,dec1 (maxbright *100)/255,"% "
    endif
    
    if (maxbright * 100)/255 >=10 and (maxbright * 100)/255 <100 then
    LCDOUT$FE,$C0,"Channel 2 "
    LCDOut $FE,$D4,dec2 (maxbright *100)/255,"%   "
    endif
        
        W_max = maxbright
    write 9,W_max 
    pause 200
    If S_butt = 0 then
        pause 200 
        LCDOUT $FE,1   
        goto mainmenu
    endif
    goto brightness2
    
    up:
        maxbright = maxbright + 1
        IF maxbright > 255 THEN maxbright = 0
        pause 50
    RETURN
    down:
        maxbright = maxbright - 1
        IF maxbright = 0 THEN maxbright = 255
        pause 50
    RETURN
    I can't argue with the brackets - all I know when I enter that menu option I can press the up and down buttons and the LCD displays 0% - 100%, and when checked using a diagnostics section which displays the variable value it correctly shows 0 - 255 and 127 for a 50% value - However I'm now using a PCA 9685 chip, which has a resolution of 4095 steps, so the progression is nice and smooth, and all I need is to repeat this but with 4095 steps, ie so 100% = 4095, 1%=40.95 (or 41 as it would need to be an integer) and 50% would equal 2047, but just can't seem to get my head round the calculation (never was any good at percentages at school all those years ago !

    Malcolm

Similar Threads

  1. POT part values
    By Michael in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 24th January 2009, 10:41
  2. Port Values
    By Pesticida in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 2nd January 2007, 20:44
  3. t in POT, c's values
    By selbstdual in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th August 2006, 23:30
  4. Numeric Values
    By Armando Herjim in forum General
    Replies: 8
    Last Post: - 28th June 2006, 01:54
  5. Wrong values with COSINE!
    By Eng4444 in forum mel PIC BASIC Pro
    Replies: 24
    Last Post: - 8th May 2006, 16:59

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