Working out hours and minutes from a word variable


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2009
    Posts
    583

    Default Working out hours and minutes from a word variable

    Hi guys,

    it's late and my tiny pair of brain cells have stopped working !

    I use a set of array variables to set up hours and minutes and then convert them into a word variable which is then used to match other word variables to activate pins on the pic.

    Code:
    CH1_on_Time = (lightsetHR1*60)+lightsetMN1
    So the word variable CH1_on_time is the result of multiplying the lightsetHR1 variable (which would be anything from 00 to 23) by 60 to get the minutes, and then add on the value of lightsetMN1 (which will be anything from 00 to 59) which then gives the total minutes since 00:00 hrs which is thus stored in CH1_on_Time. I then convert the current time from a DS1307 into minutes since midnight and store the result in another word variable and use simple IF / THEN statements to do functions when the two variables match. It's crude I know, but it works and is less prone to errors when matching Hrs and Mins directly.

    However I now have a need to reverse the above function and convert the value stored in the CH1_on_time so that I get the value for lightsetHR1 and lightsetMN1.

    For example setting the on time to 14:30 the value for CH1_on_time = 870 (14*60 = 840 + 30). I want to take the 870 and convert it back so the value for lightsetHR1 = 14 and lightsetMN1 = 30. Using simple division, ie 870 / 60, but this fails as it =14.5 and PBP doesn't support FP and I would then have to somehow 0.5 hrs in to minutes by multiplying by 60. Is there a way of using a remainder function. So that so that it takes the 870 / 60 takes the integer value of 14 and placed that in lightsetHR1, multiplies that integer by 60 to get 840, then takes the 840 away from 870 to get the remainder of 30 and placed that in the lightsetMN1 variable ?

  2. #2
    Join Date
    Oct 2005
    Posts
    18


    Did you find this post helpful? Yes | No

    Default Re: Working out hours and minutes from a word variable

    Hi Scampy
    Perhaps

    CH1_on_Time = (lightsetHR1*60)+lightsetMN1

    lightsetHR1=lightsetHR1/60 ' you get the integer part your example =14
    lightsetMN1=CH1_on_Time - lightsetHR1 ' what remains is always between 0-60 i.e.values 1,2,......59 'in your example 30

    Bill

  3. #3
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Working out hours and minutes from a word variable

    Thanks for the reply Bill,

    The only issue is that both lightsetHR1 and lightsetMN1 are initially zero value when powered up, where as CH1_on_time is read back from memory so will always have a value. so doing lightsetHR1=lightsetHR1/60 will give 0 as the result.

    What I'm after is convert the stored value in CH1_on_time back into it's whole hours and minutes values and place then into lightsetHR and lightsetMN..

    Taking my 14:30 time as the example, the resulting value is 870, so dividing that by 60 gives 14.5 which as I mentioned isn't supported in PBP. The normal practice is to multiply the result by 10 to get a hole number and then somehow divide that once it's been processed...thus being complicated !

    What I need is basically to divide the result (870) by 60, ignore the 0.5 remainder, then multiply the integer by 60 to get the hole hour equivalent (840 for the 14 hrs) then deduct this from the 870 and place the result (30) in lightsetMN1... it's finding a way to ignore the 0.5 so that only the integer part is used

  4. #4
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Working out hours and minutes from a word variable

    Scampy,

    Use the regular Division "/" operator for the Hours computation and use the Modulo "//" operator for the minutes computation.

    lightsetHR1 = (CH1_on_time / 60)
    lightsetMN1 = (CH1_on_time // 60)




    Cheers,
    Regards,
    TABSoft

  5. #5
    Join Date
    Oct 2005
    Posts
    18


    Did you find this post helpful? Yes | No

    Default Re: Working out hours and minutes from a word variable

    Hi
    My mistake
    the line lightsetHR1=lightsetHR1/60 ' you get the integer part your example =14
    should be lightsetHR1=CH1_on_time/60 ' you get the integer part your example =14
    then it produces the same results with the more elegant way that Tabsoft suggests.
    But I think that we should have used another new variable instead of CH1_on_time lets say Time_read_from_timer
    Then either way once you get the time from DS1307 you can split it into hours and minutes and then go on displaying it.
    Bill

  6. #6
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Working out hours and minutes from a word variable

    Thanks for the replies. If I get stuck I'll be back

Similar Threads

  1. Help with word variable
    By Scampy in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 26th November 2015, 13:16
  2. Storing hours and minutes into single byte?
    By CuriousOne in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 5th June 2014, 13:28
  3. Word Variable and I2C
    By Scampy in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 21st May 2014, 09:45
  4. Stumped with word variable
    By Scampy in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 11th November 2013, 14:11
  5. word variable to 25lc640
    By TONIGALEA in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 6th July 2004, 20:59

Members who have read this thread : 2

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