PDA

View Full Version : Working out hours and minutes from a word variable



Scampy
- 12th March 2016, 23:52
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.



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 ?

gebillpap
- 13th March 2016, 09:59
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

Scampy
- 13th March 2016, 12:57
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

Tabsoft
- 13th March 2016, 13:18
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,

gebillpap
- 14th March 2016, 08:03
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

Scampy
- 14th March 2016, 17:48
Thanks for the replies. If I get stuck I'll be back :)