Stuck on porting code to 18F4520


Closed Thread
Results 1 to 40 of 43

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Robert,
    I don't think that's it.
    With your version of the formula one hour "is worth" as much as one minute. Try it out with 1h 0min, then try it again with 0h 1min - if I'm not mistaken you'll get the same result.

    Not that *I* see any real problem with the original formula but you could try:
    Code:
    blue_delay_in VAR WORD
    blue_delay_in = (fadesetHR[0] * 3600 + fadesetMN[0] * 60) / 255
    /Henrik.

  2. #2
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Morning guys,

    Thanks for the suggestions. Yes the variable is a word, sorry should of pointed that out.

    I haven't tried the suggestions yet, but wondered if Henrik's suggestion could be modified to
    Code:
    blue_delay_in = (fadesetHR[0] * 3600) + (fadesetMN[0] * 60) / 255
    In my mind this will work out the seconds for both hours and minutes then add them together and divide the result by 255 ??

    I never have been good at calculations with lots of brackets when programming !!

  3. #3
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Tried both Henriks version and my revised version and both give a zero value for the variable when the delay is set to 1 hr. Works fine if set to 59 minutes.

    Thinking it might be something to do with the menu option to set the delay times, I used the data statements that contain preset settings to set the delay to 1 hr and still got zero for the result.

    It seems PBP must handle bracketed equations different to "normal"

    EDIT - Most illogical captain...

    I tried it without the brackets
    Code:
    blue_delay_in = fadesetHR[0] * 3600 + fadesetMN[0] * 60 / 255
    Setting the delay to 1 hour gives a value of 14 for the variable !!!!!
    Last edited by Scampy; - 16th December 2013 at 10:32.

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


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Darn, Henrik is right, again. Shoot, my formula makes things worse.

    I was sure I figured a math problem, oh well, I suck.

    Robert

  5. #5
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Quote Originally Posted by Demon View Post
    Darn, Henrik is right, again. Shoot, my formula makes things worse.

    I was sure I figured a math problem, oh well, I suck.

    Robert
    Robert... it's fine, just put it down to recovering from the party

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


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Hi,
    In my mind this will work out the seconds for both hours and minutes then add them together and divide the result by 255 ??
    I believe that's exactly what mine does. Multiplication and division has precedence over addition and subtraction. So mine will take the hours and multiply that by 3600, then it'll take the minutes and multiply those by 60. It will then add those two results together because they are enclosed within brackets and THEN divide that result by 255 - which I believe is what you want.

    /Henrik.

  7. #7
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Guys, see my edited post above - it works without the brackets - why I have no idea !!

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


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    OK, seems you've got it going now but during that time I tried "my version" of the formula with the following code:
    Code:
    PsuedoSeconds VAR WORD
    Hours VAR WORD
    Minutes VAR WORD
    
    Start:
        HSEROUT["Program start",13,13]
    
        Minutes = 30
        Hours = 0
        GOSUB Calculate
        GOSUB PrintResult
        
        Minutes = 59
        Hours = 0
        GOSUB Calculate
        GOSUB PrintResult
        
        Minutes = 0
        Hours = 1
        GOSUB Calculate
        Gosub PrintResult
        
        Hours = 2
        Minutes = 15
        GOSUB Calculate
        Gosub PrintResult
    
        Hours = 3
        Minutes = 59
        GOSUB Calculate
        Gosub PrintResult
                
        Hours = 10
        Minutes = 0
        GOSUB Calculate
        Gosub PrintResult
        
        Pause 100
        
    END
    
    Calculate:
        PsuedoSeconds = (Hours * 3600 + Minutes * 60) / 255
    RETURN
    
    PrintResult:
        HSEROUT[DEC2 Hours, ":", DEC2 Minutes, " - ", DEC PsuedoSeconds,13]
    RETURN
    And it outputs:
    Code:
    Program start
    
    00:30 - 7
    00:59 - 13
    01:00 - 14
    02:15 - 31
    03:59 - 56
    10:00 - 141
    Which seems correct to me.

    /Henrik.

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


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    I have no idea what you're doing.....
    I've tried three versions of the formula and the only one to give correct results are the one I posted. Please see the following code and the results:
    Code:
    PsuedoSeconds VAR WORD
    Hours VAR WORD
    Minutes VAR WORD
    
    Start:
        HSEROUT["Program start",13,13]
    
        Minutes = 30
        Hours = 0
        GOSUB Calculate
        
        Minutes = 59
        Hours = 0
        GOSUB Calculate
        
        Minutes = 0
        Hours = 1
        GOSUB Calculate
      
        Hours = 2
        Minutes = 15
        GOSUB Calculate
    
        Hours = 3
        Minutes = 59
        GOSUB Calculate
        
        Hours = 10
        Minutes = 0
        GOSUB Calculate
       
        Pause 100
        
    END
    
    Calculate:
        PsuedoSeconds = (Hours * 3600 + Minutes * 60) / 255
        HSEROUT["Version 1: "]
        GOSUB PrintResult
        
        PsuedoSeconds = (Hours * 3600) + (Minutes * 60) / 255
        HSEROUT["Version 2: "]
        GOSUB PrintResult
        
        PsuedoSeconds = Hours * 3600 + Minutes * 60 / 255
        HSEROUT["Version 3: "]
        GOSUB PrintResult
        
        HSEROUT[13]
    RETURN
    
    PrintResult:
        HSEROUT[DEC2 Hours, ":", DEC2 Minutes, " - ", DEC PsuedoSeconds,13]
    RETURN
    Result:
    Code:
    Program start
    
    Version 1: 00:30 - 7
    Version 2: 00:30 - 7
    Version 3: 00:30 - 7
    
    Version 1: 00:59 - 13
    Version 2: 00:59 - 13
    Version 3: 00:59 - 13
    
    Version 1: 01:00 - 14
    Version 2: 01:00 - 3600
    Version 3: 01:00 - 3600
    
    Version 1: 02:15 - 31
    Version 2: 02:15 - 7203
    Version 3: 02:15 - 7203
    
    Version 1: 03:59 - 56
    Version 2: 03:59 - 10813
    Version 3: 03:59 - 10813
    
    Version 1: 10:00 - 141
    Version 2: 10:00 - 36000
    Version 3: 10:00 - 36000
    As you can see, the only one to give correct results are the first one. Why, because the other two both will take the result of the Minutes*60 divided by 255 and add THAT result to Hours*60 while the first (correct) one will divide the sum of the multiplications by 255. How you are getting the correct results from the formula without any parenthesis is beyond me.

    /Henrik.

  10. #10
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    I found the cause. I had duplicated the fade in / fade out lines within another subroutine which was being called and thus overwriting the changes made - I guess that's what happens when you spend too long coding into the wee hours of the night !! - Mistakes happen

  11. #11
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Stuck on porting code to 18F4520

    Hi Guys,

    I've stumbled on a small issue that is doing my head in.

    Whilst using 8 bit resolution for the fading (ie 0 - 255) the code works and allows a minimum fade up / down time of 5 minutes, I now want to increase the resolution to 4095 steps.

    Code:
    blue_delay_in = fadesetHR[0] * 3600 + fadesetMN[0] * 60 / 255
    white_delay_in = fadesetHR[1] * 3600 + fadesetMN[1] * 60 / 255
    blue_delay_out = fadeoutHR[0] * 3600 + fadeoutMN[0] * 60 / 255
    white_delay_out = fadeoutHR[1] * 3600 + fadeoutMN[1] * 60 / 255
    If the above is changed by removing the 255 at the end of each line and replacing that with 4095 it means the minimum fade in/out time is now 68 minutes as anything less results in an FP decimal which isn't supported by PBP

    The resulting value for blue_fade_in etc is then used in the case statements to perform the PWM

    Code:
    case DAWN 
    lcdout $FE,$80+15,"DAWN "   
    if ss//blue_delay_in = 0 then
    if ss != old_ss_blue then
    B_PWM=B_PWM+1
    old_ss_blue=ss
    endif
    endif
    if B_PWM = B_Mid then
    Blue_Day_Cycle = MORN
    endif
    Is there anything I can do to the code as it stands to get the timing so that I can use the 4095 resolution, and still have fade in/out times of 5 minutes or more rather than 68 minutes.

Similar Threads

  1. Porting code to new PIC
    By malc-c in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 31st December 2010, 23:20
  2. Kind of stuck
    By gti_uk in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 31st May 2009, 20:45
  3. Getting Stuck in loop
    By Frozen001 in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 19th November 2008, 15:46
  4. Any idea's on porting this to PBP?
    By Ryan7777 in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 10th October 2008, 19:21
  5. Replies: 1
    Last Post: - 8th May 2008, 00:52

Members who have read this thread : 1

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