adding days to a date

Join Date
May 2011
Location
South Carolina
Posts
87
Hello everyone.

I am in need of some assistance. I want to add some logic to a PLC to trigger an alert on the HMI that it is time to clean the machine tooling. This is supposed to be done every 14 days but I am unsure the best way to handle it. I can do it using comparisons based on the month and day but I was hoping someone else had an easier way of doing this. For instance, I was going to say if the month is = to 1 (or one of the other months with 31 days) and the day is 17 or less then just add the 14 to the day. But if it is over 17 then I would have to use some math to get to the right day and then advance the month to the next month. Any ideas on an easier way to do this?
 
If you allready have clock on PLC, then make midnigt / day change pulse (hours and minutes = 00).
Then only count up to 14 these pulses, trigger alert and zero your counter.
Your counter also shows how many days to alert, if you count down.
 
I guess I need to be a little more specific about what I am trying to do. So lets say that today is 1-27-2020 (Jan. 27, 2020) and I pop up an alert on the screen that says time to clean the tooling. When this is acknowledged, I want to automatically change the "due date" for the next alert to 2-10-2020 (Feb. 10, 2020). I also plan on pulling this "due date" out of the PLC and displaying on a separate HMI, that will be getting this information from numerous machines (26 different machines). So originally the due date would say 1-27-2020 on the remote HMI and then would change to 2-10-2020 and then 2-24-2020, etc. as it is acknowledged on the machine.
 
I think you would have to create an array 365 words long. Start at 101 and go to 1231
Another array for leap years 366 words long.
See if it is a leap year [Year / 4 to DINT & Year / 4 to REAL, if REAL=DINT then it's a leap year]

Know what day today is and look ahead 14 in the array. If you don't want to have to worry about rolling over the new year make the arrays 14 words longer and go 101 up to 1231 and then 101 to 113.

Split the 115 result into month 1 and day 15 and that is your due date. You can use that to trigger a due popup.
 
Would this work then on PLC?





CASE month_now OF
1:
days:=31
2:
days:=28
3:
days:=31
4:
days:=30
5:
days:=31
6:
days:=30
7:
days:=31
8:
days:=31
9:
days:=30
10:
days:=31
11:
days:=30
12:
days:=31

END_CASE;


Days_remaining:=days-day_now


if days_remaining >=14 then
next_serv_month:=month_now
next_serv_day:=day_now+14)
end_if


if days_remaining <14 then
next_serv_month:=month_now+1
next_serv_day:=14-days_remaining
end_if



 
Honestly, Calendar math is stupid hard because of months with different numbers of days, leap years, daylight savings, etc.


What PLC are you in? It may already have a useful way of parsing time/date values into months/etc. I'd recommend using that over trying to roll your own solution.
 
Would this work then on PLC?...




what about leap years (((y mod 4) == 0 and (y mod 100) != 0) or ((y mod 400) == 0))?


what about leap seconds? (just kidding)


yeah, mk42 is right: calendar math is probably one of the hardest things to program correctly, i would not even blindly trust a PLC OEM to get it right, and would instead make and access a web api on a linux box (e.g. RPi/Python one-liner); i'll bet it would be operating correctly sooner.


Calendar math will be painful in any PLC language so why re-invent the wheel?


Unless maybe someone uploaded an example to the plctalk downloads repositories for your brand? It's worth a shot to look.
 
Last edited:
Just increment a counter every 24 hours, when it gets to 14, splash a banner which says 'blah blah now due'

Included a reset service days button for the maintenance guy.
 
what about leap years (((y mod 4) == 0 and (y mod 100) != 0) or ((y mod 400) == 0))?


what about leap seconds? (just kidding)


yeah, mk42 is right: calendar math is probably one of the hardest things to program correctly, i would not even blindly trust a PLC OEM to get it right, and would instead make and access a web api on a linux box (e.g. RPi/Python one-liner); i'll bet it would be operating correctly sooner.


Calendar math will be painful in any PLC language so why re-invent the wheel?


Unless maybe someone uploaded an example to the plctalk downloads repositories for your brand? It's worth a shot to look.


It was fast couple minutes coding. It won't take account leap years and automatic time adjuments on spring and autumn. (y)


Leap years are easy to add with couple more lines leke I_Automation's leap year calculation.
 
Why does everyone link this to the RTC or think expensive calculations need to be done ?


Hour = 0 ONS NewDay = NewDay + 1


if NewDay = 14 ; CleanMe NewDay = 0

if CleanMe Clean Me !
 
Why does everyone link this to the RTC or think expensive calculations need to be done ?...


I agree with daba's relative calculation, but the answer to that query is this:


...I also plan on pulling this "due date" out of the PLC and displaying on a separate HMI, that will be getting this information from numerous machines (26 different machines). ...




That said, maybe it would be an acceptable alternative to say "N days left to next cleaning" instead of "next cleaning on YYYY-mm-dd," and run daba's NewDay calculation backwards from 14 to 0.
 
Insomnia, and I just did this somewhere else in Java no less.

Prerequisites/assumptions/caveats


* Untested

* Integer tags cury, curm, curd are current year, month of year (1-12), and day of month (1-31)
* Integer tags nxty, nxtm, nxtd are year, month of year, and day of month for next cleaning (14d hence)
* The code is only executed when the "finished cleaning" is pressed and generates a one-shot
* Presumably that occurs on one scan per fortnight
* So this is set up as a program file (AB-speak) that returns early, having done naught, on most scans
* tagname/N is bit (boolean) N of integer tagname

Code:
########################################################################
### Exit this routine unless "finished cleaning" one-shot is 1
    
   fin_cln_ons
-------]/[---------<RET>---


########################################################################
### Assume count of days in current month, tag curdim, is 30
    _______________
---[MOV            ]----
   [Source       30]
   [Dest     curdim]
    ---------------


########################################################################
### Calculations to check for leap year
### - Int tag yN is a year evenly divisible by N and near cury
### - Strictly speaking, this calculation is not needed unless curm is 2
### - Assumes modulo instruction is not available
### - No more than y4 is really needed; this will be irrelevant ca. 2100
        _____________
---+---[AND          ]---+---
   |   [SourceA  cury]   |
   |   [SourceB     3]   |
   |   [Dest       y4]   |
   |    -------------    |
   |    _____________    |
   +---[SUB          ]---+
       [SourceA  cury]
       [SourceB     3]
       [Dest       y4]
        -------------

        _____________
---+---[DIV          ]---+---
   |   [SourceA  cury]   |
   |   [SourceB   100]   |
   |   [Dest     y100]   |
   |    -------------    |
   |    _____________    |
   +---[MUL          ]---+
       [SourceA  y100]
       [SourceB   100]
       [Dest     y100]
        -------------

        _____________
---+---[DIV          ]---+---
   |   [SourceA  cury]   |
   |   [SourceB   400]   |
   |   [Dest     y400]   |
   |    -------------    |
   |    _____________    |
   +---[MUL          ]---+
       [SourceA  y400]
       [SourceB   400]
       [Dest     y400]
        -------------


########################################################################
### Adjust 30d assumption above for Jan, Mar, May, Jul, Aug, Oct, Dec

        _____________   curm/0        __________________
---+---[LES          ]----] [----+---[ADD               ]------
   |   [SourceA  curm]           |   [SourceA     curdim]
   |   [SourceB     8]           |   [SourceB          1]
   |    -------------            |   [Dest        curdim]
   |                             |    ------------------
   |    _____________   curm/0   |
   +---[GRT          ]----]/[----+
       [SourceA  curm]
       [SourceB     7]
        ------------- 


########################################################################
### Adjust 30d assumption above for Feb:
### 30-2 => 28, then +1 => 29 for leap years

    _____________                                                 _______________
---[EQU          ]---+-------------------------------------------[SUB            ]---+--
   [SourceA  curm]   |                                           [SourceA  curdim]   |
   [SourceB     2]   |                                           [SourceB      -2]   |
    -------------    |                                           [Dest     curdim]   |
                     |                                            ---------------    |
                     |    _____________     _____________         _______________    |
                     +---[EQU          ]---[NEQ          ]---+---[ADD            ]---+
                     |   [SourceA  cury]   [SourceA  cury]   |   [SourceA  curdim]
                     |   [SourceB    y4]   [SourceB  y100]   |   [SourceB       1]
                     |    -------------     -------------    |   [Dest     curdim]
                     |    _____________                      |    ---------------
                     +---[EQU          ]---------------------+
                         [SourceA  cury]
                         [SourceB  y400]
                          -------------


########################################################################
### Move cury & curm to nxty & nxtm as initial year and month estimates
### Calculate nxtd, day of month a fortnight hence
### Correct for rollover at end of month


        ____________
---+---[MOV         ]------------------------------+---
   |   [Source  cury]                              |
   |   [Dest    nxtd]                              |
   |    ------------                               |
   |    _____________                              |
   +---[MOV         ]------------------------------+
   |   [Source  curm]                              |
   |   [Dest    nxtm]                              |
   |    ------------                               |
   |    _____________                              |
   +---[ADD          ]-----------------------------+
   |   [SourceA  curd]                             |
   |   [SourceB    14]                             |
   |   [Dest     nxtd]                             |
   |    -------------                              |
   |    _______________         _______________    |
   +---[GRT            ]---+---[SUB            ]---+
       [SourceA    nxtd]   |   [SourceA    nxtd]   |
       [SourceB  curdim]   |   [SourceB  curdim]   |
        ---------------    |   [Dest       nxtd]   |
                           |    ---------------    |
                           |    _____________      |
                           +---[ADD          ]-----+
                               [SourceA  nxtm]
                               [SourceB     1]
                               [Dest     nxtm]
                                -------------


########################################################################
### Correct for rollover at end of year
    _______________         _______________
---[GRT            ]---+---[MOV            ]---+---
   [SourceA    nxtm]   |   [SourceA       1]   |
   [SourceB      12]   |   [SourceB    nxtm]   |
    ---------------    |    ---------------    |
                       |    _____________      |
                       +---[ADD          ]-----+
                           [SourceA  nxty]
                           [SourceB     1]
                           [Dest     nxty]
                            -------------
Kinda makes daba's relative approach more attractive, eh?
 
Last edited:
Why does everyone link this to the RTC or think expensive calculations need to be done ?


Hour = 0 ONS NewDay = NewDay + 1


if NewDay = 14 ; CleanMe NewDay = 0

if CleanMe Clean Me !


I posted same allready on 2nd post on this thread.

OP answered then that day-mont-year calculation is wanted.
Why everyone aren't answering to 3rd post (dd-mm-yyyy calculation question)?
 

Similar Topics

Hello, I have a pair of redundant 1756-L71 controllers rev 24. These controllers currently have produced and consumed tag interfaces to 3 other...
Replies
2
Views
99
I'm adding an IAI Gateway with 2 axes connected to it. To an ethernet network on my PLC. So, I think the math is correct on the Input and Output...
Replies
0
Views
68
Hi Folks. I am gearing up to add a 2nd identical HMI to a project. This 2nd HMI will basically be an exact clone of the 1st one. The equipment...
Replies
3
Views
229
Hello all, I have been tasked with flashing our processors to the newest version of Studio 5000, and have downloaded the DMK file for the...
Replies
4
Views
173
Hi First post here! I admit I am not too used to handle interrupt OBs in Siemens. I have a customer who has been buying a standardized solution...
Replies
4
Views
214
Back
Top Bottom