PLC code help

bbrendon

Active Member
I'm using an output to turn my heater on/off. I think my biggest problem is doing nested if's. What I've written so far just keeps turning the heater off every second. I want to:
 
if heater on (OP1=1)
  has it been on for 20+ min? turn off
  is t1 > 75 degreess? turn off
endif
 
Thanks for any pointers on this.
 


START
CALLSUB HVAC_SAFE
END


HVAC_SAFE:
TSTEQ OP1 1 # if OP1 is on
TSTGT T1 750 # and If > 75.0 deg, turn it off.
CALLSUB HVAC_OFF
RET


HVAC_OFF:
SET OP1 0
EMAIL EM3
RET
 
 
 
bbrendon said:
I'm using an output to turn my heater on/off. I think my biggest problem is doing nested if's. What I've written so far just keeps turning the heater off every second. I want to:
 
if heater on (OP1=1)
  has it been on for 20+ min? turn off
  is t1 > 75 degreess? turn off
endif
 
Thanks for any pointers on this.

 

 
 
Not tested, but perhaps:
 
start
   tsteq op1[1200000] 1
  set op1 0
 
  tstgt t1 750
  set op1 0
end
 
I presume you will turn the heat on manually since you haven't mentioned how it gets turned on, just how it gets turned off!
 
Doesn't this set op1 to 0 every second after t1 is greater than 75? Is that bad for the hardware? I was thinking it would be proper to check if it was on first before performing any actions on op1.
 
bbrendon said:
Doesn't this set op1 to 0 every second after t1 is greater than 75? Is that bad for the hardware? I was thinking it would be proper to check if it was on first before performing any actions on op1.
 
A TTL output is either on, or off. If the output is already off, setting it off does nothing.
Where I DO take additional steps though, is if you had something like this:
 
   tstlt t1 500   # is temp less than 50 deg?
   set op1 1   # turn on output
 
  tsteq op1[30000] 1  # has output been on >30 seconds?
  set op1 0   # turn it off
 
This code would not do what you expected due to the constant resetting of op1
 
Similarly code that sets the output high in one place, then sets it low again shortly after, will result in "needle pulses" on the output. These can be hard to find or diagnose, and can cause all sorts of odd behavior - particularly if you are using solid-state relays on the outputs!
 
the way it looks you will be getting non stop emails.I have my hottub setup to turn heater on and off with email.Look at THIS POST see if it will help.
this should work
 
 
start
          set var1 0      --sets all rams incase power wentout
loop:
        callsub resetvars   --check befor continues
        delay 2000           --gives it time to check
        tstgt  t1 750       --checks to see if t1 is greater then 75
        callsub hvac_off  ---if it is go to here
       delay 1000       --give it time to do
        goto loop    --go back to top
        end
 
resetvars:
        TSTLT T1 720  ---check to see if t1 less then 72
         SET VAR1 0    --if so set var1 to 0
         RET
 
HVAC_OFF:
        SET OP1 0   --turn off
        CALLSUB SEND_EM1  
        RET
 
SEND_EM1:
         TSTEQ VAR1 0  --check if var1 is 0
          EMAIL EM1     --if var1 is true send email
          DELAY 2000 --gives it time to send email
          SET VAR1 1  -set var1 1 (keeps email from sending continuously)
          DELAY 2000
          RET
 
bbrendon said:
I'm using an output to turn my heater on/off. I think my biggest problem is doing nested if's. What I've written so far just keeps turning the heater off every second. I want to:
 
if heater on (OP1=1)
  has it been on for 20+ min? turn off
  is t1 > 75 degreess? turn off
endif
 
Thanks for any pointers on this.
 


START
CALLSUB HVAC_SAFE
END


HVAC_SAFE:
TSTEQ OP1 1 # if OP1 is on
TSTGT T1 750 # and If > 75.0 deg, turn it off.
CALLSUB HVAC_OFF
RET


HVAC_OFF:
SET OP1 0
EMAIL EM3
RET
 
 
 
 
START
CALLSUB HVAC_SAFE
END


HVAC_SAFE:
TSTEQ OP1 1 # if OP1 is on     If this is false (heater is off), it will skip the next step and go straight to callsub hvac_off)
TSTGT T1 750 # and If > 75.0 deg, turn it off.     If this is true, it will run hvac off, if false, it skips to ret
CALLSUB HVAC_OFF
RET


HVAC_OFF:
SET OP1 0
EMAIL EM3     You will get an email every single time hvac_off is executed, which is going to be a million emails since every time the code runs and the heater is already off it will run because of my above comment.
RET
 
The following program will turn the heat on if it is less than 72 degrees, but only if the heat has already been off for 25 minutes.  It will turn the heat off if the temp is greater than 75 or if the heat has been on for 25 minutes, but only if the heat was already on.  It won't turn the heat off if it isn't currently on and it won't send an email either.  It only sends an email when OP1 changes from on to off.
 
Your original post did not include any mechanism for the heat to come on, so I added my own.  But if you are going to have it turn off after 25 minutes, and if it isn't up to temp already, I had to put a delay in to turn it back on, or it would just turn back on immediately.
 
 
START
[SIZE=10.5pt]TSTGT T1 750[/SIZE]
[SIZE=10.5pt]CALLSUB HVAC_OFF[/SIZE]
[SIZE=10.5pt]TSTLT T1 720[/SIZE]
[SIZE=10.5pt]CALLSUB HVAC_ON[/SIZE]
[SIZE=10.5pt]TSTGT CTS RAM1[/SIZE]
[SIZE=10.5pt]CALLSUB HVAC_OFF[/SIZE]
[SIZE=10.5pt]END[/SIZE]
 
[SIZE=10.5pt]HVAC_ON:[/SIZE]
[SIZE=10.5pt]TSTEQ OP1 1[/SIZE]
RET
TSTLT RAM2 CTS
RET
SET OP1 1
ADD CTS 1500 RAM1
RET
 
[SIZE=10.5pt]HVAC_OFF:
TSTEQ OP1 0[/SIZE]
[SIZE=10.5pt]RET[/SIZE]
[SIZE=10.5pt]SET OP1 0[/SIZE]
[SIZE=10.5pt]ADD CTS 1500 RAM2[/SIZE]
[SIZE=10.5pt]EMAIL EM3
RET[/SIZE]
 
Back
Top