Subtracting minutes from time stored as a decimal.

evilthorne

Member
Join Date
Jun 2016
Location
Kitchener Ontario
Posts
24
Hello,

In CLX5000 I have a DINT being used to store the shift's break time as a whole number ie 1:00 would be 100. The issue is if I want to subtract two minutes from this I get 98 instead of 58. Is there an easy way to calculate this possibly to give me a number value? I have the PLC doing a bunch of math but it is clumsy honestly, I divide the total by 100 then do a MOD to get the minutes and on and on and it seems excessive. I will need to do it for several different times depending on the shift.

Thank you!
 
Hello,

In CLX5000 I have a DINT being used to store the shift's break time as a whole number ie 1:00 would be 100. The issue is if I want to subtract two minutes from this I get 98 instead of 58. Is there an easy way to calculate this possibly to give me a number value? I have the PLC doing a bunch of math but it is clumsy honestly, I divide the total by 100 then do a MOD to get the minutes and on and on and it seems excessive. I will need to do it for several different times depending on the shift.

Thank you!


So 100 => 1:00 => 1h0m?


What language are you using e.g. ladder logic?


I don't see how to get away from using MOD at a minimum, basically it needs to do subtraction the way we learned in school, but with 60 as the carry for the last two digits.


So a DINT with digits HMM becomes minutes, MM, and hours, H:
MM = HMM MOD 100
H = (HMM - MM) / 100
Then to subtract another hour+minutes time, hmm, break that into h and mm the same way, then
diff_mm = MM - mm


if diff_mm < 0 then
diff_mm = diff_mm + 60
H = h - 1
diff_h = H - h


diff_hmm = (diff_h * 100) + dif_mm
Couldn't this be broken into two AIOs? The first does the hmm => (h,mm) conversion, and the second does the subtraction, using the first internally?




If the subtrahend is not in hmm form, but is whole minutes e.g. 1,2,3,...,59,60,61,..., then do the subtraction in centiminutes i.e.

result_minutes = (((HHMM - (HHMM MOD 100)) * 60) + (((HMM MOD 100) - minutesDINT) * 100)) / 100

If that need to get back to hmm form:


result_hmm = (((result_minutes - (result_minutes MOD 60)) * 100) / 60) + (result_minutes MOD 60)
But that is probably what you are already doing. Is it in a CPT or AOI?
 
Last edited:
Why not store the minutes in one integer & seconds in the other, Inc the seconds and when = 60 inc minutes & reset seconds you then have minutes & seconds so easy to do any maths. Or just count the seconds & divide by 60 leaves minutes and remainder as seconds.
 
You could just use the RTC and work in separate hours and minutes. Using a GSV instruction you can get the date and time down to milliseconds if you need it. The RTC data is still in 24 hour form, so you could grab data like "19/06/2020 15:34:29.12345".

When I write up an OEE routine for new machines coming in, I typically create a DateTime UDT that is 7 elements long for the RTC data, and then a ShiftDateTime UDT that stores start times, end times, breaks, and hourly production data. All I have to do from there is use compare functions. This allows me to change break schedules, shift times, and shift lengths on the fly without doing any extra unneeded math. It has been very handy lately as all of our production cells have been staggered to keep traffic to a minimum throughout the plant as production starts to ramp back up.
 
If you are subtracting hmm from HMM, and HMM > hmm, then



Code:
---[MOD         ]---
   [SourceA  HMM]
   [SourceB  100]
   [Dest      MM]

---[MOD         ]---
   [SourceA  hmm]
   [SourceB  100]
   [Dest      mm]

---[SUB         ]---
   [SourceA  HMM]
   [SourceB  hmm]
   [Dest    diff]

---[LES         ]---[SUB         ]---
   [SourceA   MM]   [SourceA diff]
   [SourceB   mm]   [SourceB   40]
                    [Dest    diff]


The result, diff, is in that same HMM format.
 
Last edited:
Have you looked at the AOIs in the Add_On_Instructions_Samples that comes with v19? It has several functions that address math and time.
Welcome Drew to the forum, above would be cheating (not) :whistle: as well as take away all the fun of members to come up with alternate methods
 
Perhaps we ought to ask the OP what 100 equates to ? is it seconds ?, minutes ? etc. seems wrong to me he wants to subtract 2 minutes (160 seconds) from a number of 100, 100 what is it minutes then he has the answer if it's seconds then he should have minus seconds (pause for thought... am I reading this wrong) In all my years if I want to log or display a down time or some other time then it's simple you do it all in seconds or minutes & seconds or even in hours, minutes & seconds. I usually create a function that counts seconds, increments the minutes, reset the seconds & if required increments the hours etc, slightly different if you wand to log this to a DB.
 
Perhaps we ought to ask the OP what 100 equates to ? is it seconds ?, minutes ? etc. seems wrong to me he wants to subtract 2 minutes (160 seconds) from a number of 100, 100 what is it minutes then he has the answer if it's seconds then he should have minus seconds (pause for thought... am I reading this wrong) In all my years if I want to log or display a down time or some other time then it's simple you do it all in seconds or minutes & seconds or even in hours, minutes & seconds. I usually create a function that counts seconds, increments the minutes, reset the seconds & if required increments the hours etc, slightly different if you wand to log this to a DB.



100 equates to 1:00am
1300 equates to 1:00pm
I am using it to automatically shut motors off during their lunch break and then turn them on again automatically two minutes before the end of their break.


As it was previously said, our shop changes their break times weekly it seems and with all this covid19 going on I need a dynamic way to change it. Honestly their break was set at 11:30 and 21:30 forever and I didn't even think about this whole two minute subtraction on the hour issue.


So 100 => 1:00 => 1h0m?


What language are you using e.g. ladder logic?


I don't see how to get away from using MOD at a minimum, basically it needs to do subtraction the way we learned in school, but with 60 as the carry for the last two digits.


So a DINT with digits HMM becomes minutes, MM, and hours, H:

MM = HMM MOD 100
H = (HMM - MM) / 100

Then to subtract another hour+minutes time, hmm, break that into h and mm the same way, then
diff_mm = MM - mm


if diff_mm < 0 then
diff_mm = diff_mm + 60
H = h - 1

diff_h = H - h


diff_hmm = (diff_h * 100) + dif_mm

Couldn't this be broken into two AIOs? The first does the hmm => (h,mm) conversion, and the second does the subtraction, using the first internally?




If the subtrahend is not in hmm form, but is whole minutes e.g. 1,2,3,...,59,60,61,..., then do the subtraction in centiminutes i.e.

result_minutes = (((HHMM - (HHMM MOD 100)) * 60) + (((HMM MOD 100) - minutesDINT) * 100)) / 100

If that need to get back to hmm form:


result_hmm = (((result_minutes - (result_minutes MOD 60)) * 100) / 60) + (result_minutes MOD 60)

But that is probably what you are already doing. Is it in a CPT or AOI?


This is pretty much how I am doing it right now. I originally set it up as separate divide and/subtract instructions but am ready to go to CPT if there is no easier way
 
Last edited:

Similar Topics

Hi I am trying to find the simplist way to add/subtract/multiply/divide from all the dint's in a 42dint array. The timing on our main oven loads...
Replies
3
Views
2,991
Hello: I'm working in the Connected Component Workbench environment with a Micro820 PLC. Is there a way to add-subtract date & time values. Thanks
Replies
7
Views
3,518
Basically I have a digital value I need to scale down using this equation: 'end_value = (D1-1000)/100' but I'm having trouble writing it down to...
Replies
3
Views
2,161
in a slc 5, Is there a way to subtract one counter acc from another one and then say if the subtraction is over 5 that it would trigger and output...
Replies
3
Views
1,583
Rslogix 500 Im trying to subtract 10 from the level of a tank we have and then use a less than or equal compare to start a pump. How can I...
Replies
11
Views
2,046
Back
Top Bottom