Yes, another PLC code question...

ChubRock1

Member
I think this must be simple, but I must be missing something as I haven't figure it out yet.

Simply, I want EM3 to send when H1 has been greater than 75 for 5 minutes.

Right now I have it set to send EM3 when H1 is over 75 which sends the EM3 instantly, but I can't seem to figure out how to add the parameter that H1 should be over 75 for 5 mins before it sends EM3. I thought about adding a delay step in a sub that would trigger once H1 is over 75, which would delay for 5 min then check if H1 is still over 75 and that seems to work but I really can't hold up the code for that long so I need something that doesn't use delay. Thoughts?
 
CAI has a non-blocking delay operator, but I don't know if it can go up to 5 minutes. It goes in brackets [] after the tstgt command. tstgt[500] will require the greater than condition has already been true for more than 500 milliseconds or it considers the statement false. PLC code does not stop and wait 500 milliseconds, it keeps right on going and if it the statement is true, but hasn't been true for at least 500 milliseconds, it evaluates as false.

The instructions state that it is a 32 bit number, which is larger than 300,000 milliseconds (5 minutes), so I assume it should work. I've never tried it with such a big number.

If that doesn't work you can use the current minute function. It gets a little tricky since you have to account for the fact that it resets to 0 at the top of the hour, but there is a way to do it using variables as temporary place holders.

Also, if memory serves me, I believe that cai added a minute counter that just keeps counting plus one every minute forever (or until it hits some max limit). I'm not positive that this was actually added or just talked about, and if it was, I don't know what firmware it was added to.

EDIT: I just wrote the following code which I think would do the trick also. It should send an email only if humidity goes above 75 and stays above it for 5 minutes.


Start
Tstgt h1 75
Callsub timer
Tstlt h1 76
Set ram1 0
Tsteq cm var1
Callsub notify
end


Timer:
Tsteq ram1 1
ret
Set ram1 1
Add cm 5 var1
Tstgt var1 59
Sub var1 60 var1
Ret

Notify:
Tsteq ram1 1
Email em1
set var1 60
ret
 
@lou - thanks for the idea, I think the non blocking delay may be the simplest so I am trying that now. After looking at my data closer I only need the delay to be 2 mins so I have setup TSTGT H1[120000] 75 to see how that works.
 
hmm, well not sure it worked. when H1 rose over 75 it did not initially send out the email, however a couple mins later I got an email even though H1 was already below 75. Is it possible that the delay is literally just delaying the action of jumping to the sub routine and not showing the TSTGT as false?
 
Should not be the case. Here is the description directly from the plc manual.

"[] = non-blocking delay operator optional to TTL input/output and VARs. 32 bit
unsigned number, represent resolution of 0.001 seconds. When the delay operator is
used on input operands the current value of that input is only used if it has had that
value for greater than the delay period specified between the brackets"

However, I have not used it for humidity, and this description does not speak to humidity specifically. The humidity is read differently (1 wire sensor), so perhaps this function does not work.

Try cutting and pasting my code into your PLC. Before weaving it into whatever other code you already have, just cut and paste your current code into something else (like MS word), then put my code in for testing. If it works then you can weave it into your current code.

Alternatively, It does specifically say that it works with variables. So you could set a variable to the humidity and try putting the [] delay on the variable.

ie
set var1 h1
tstgt var1[120000] 75
 
I'm going to let it test one more time as is, which would be at 4:00. I did lengthen it to 4 mins to see if that has any effect on the outcome.
 
hmm, well not sure it worked. when H1 rose over 75 it did not initially send out the email, however a couple mins later I got an email even though H1 was already below 75. Is it possible that the delay is literally just delaying the action of jumping to the sub routine and not showing the TSTGT as false?

What version firmware? CAI admitted to me when I reported this as buggy that yes, there was a problem and that it was only fixed in (I think) 3.2.11
 
I'm using 3.2.14 but I think the delay may not work with H1 as again it sent the email immediatly. As Lou had suggested I have set VAR1 to H1 and used the delay step on VAR1 and so far it seems to be working correctly. Thanks again for the help.
 
I'm using 3.2.14 but I think the delay may not work with H1 as again it sent the email immediatly. As Lou had suggested I have set VAR1 to H1 and used the delay step on VAR1 and so far it seems to be working correctly. Thanks again for the help.

Another possible solution


Code:
 tstle H1 75    # is humidity under 75%?
 set ram1 cti    # reset time if yes
 add ram1 300 ram1    # when is 5 minutes from now
 tsteq ram1 cti    # is it 5 mins since?
 email em3    # trigger email
 sub ram1 300 ram1    # restore timer (saves using a second ram variable)
 delay 1000 #    (prevent code executing too quickly and sending multiple emails
 
Another possible solution


Code:
tstle H1 75 # is humidity under 75%?
set ram1 cti # reset time if yes
add ram1 300 ram1 # when is 5 minutes from now
tsteq ram1 cti # is it 5 mins since?
email em3 # trigger email
sub ram1 300 ram1 # restore timer (saves using a second ram variable)
delay 1000 # (prevent code executing too quickly and sending multiple emails

I pulled up the most recent manual after seeing your code. I see that yes cai did add a continuous second counter. It was called "cts" in the manual. I assume that is what you meant when you put "cti" in the above program.

@chubrock1
Good to hear setting the variable did the trick. It clearly is the shortest and simplest way to do this as well as introducing no blocking delays. Unfortunately you have to use up a variable.
 
I pulled up the most recent manual after seeing your code. I see that yes cai did add a continuous second counter. It was called "cts" in the manual. I assume that is what you meant when you put "cti" in the above program.

Yes, CTS, typo sorry.
 
Back
Top