Structured Text repeating timer based on true/false bit

How would I make a timer that would repeat on an interval as long as a certain value is true.

IF EVENT THEN
EVENTXBIT := TRUE
ENDIF


IF EVENTXBIT = TRUE THEN
DO THIS
WAIT X MINUTES
END_IF


IF ANOTHER EVENT THEN
EVENTXBIT := FALSE
ENDIF



This is what I have so far. It counts up to 3 min and then stops, instead of resetting. I figured since I change the variable to false it should retrigger.

Code:
TEST_VAR := TRUE;
TIMER_RETRIGGER_ALM0(TEST_VAR,T#3m);

IF TIMER_RETRIGGER_ALM0.Q THEN
	TEST_VAR  := FALSE;
END_IF;
 
TEST_VAR := TRUE;
TIMER_RETRIGGER_ALM0(TEST_VAR,T#3m);
IF TIMER_RETRIGGER_ALM0.Q THEN TEST_VAR := FALSE;
TIMER_RETRIGGER_ALM0(TEST_VAR,T#3m);
END_IF;



The timer is always called with a TRUE signal and does not 'know' the signal shift.
Try to add the cursive line and see if that works.
The output (.Q) will likely reset also, so you will not be able to catch it outside this code without copying it to another variable.

Kalle
 
This is how I would do it. Note that this is CoDeSys and it looks like the timer call is a little different than what you are using.

To have something to do each time the timer times out, I just incremented a variable. The thing you want to happen at each timeout should go in the IF/THEN block where I'm incrementing the i variable.

I don't like calling the timer in multiple places as if they get spread out, it can be very difficult to troubleshoot.

Code:
PROGRAM PLC_PRG
VAR
    I: INT;
    FirstPassDone: BOOL;
    Timer_Retrigger_ALM0: TON;
    TEST_VAR: BOOL;
    ALM0_Trigger: BOOL;
END_VAR

(* Run Code on First Pass through Program *)
IF NOT(FirstPassDone) THEN
    I:=0;
    FirstPassDone := TRUE;
END_IF

(* Call the Timer *)
Timer_Retrigger_ALM0(IN:= ALM0_Trigger , PT:= T#5s);

(* Set the Timer Trigger *)
ALM0_Trigger := (TEST_VAR AND NOT(Timer_Retrigger_ALM0.Q));

(* Do Something each time the timer finishes *)    
IF Timer_Retrigger_ALM0.Q THEN
    i := i + 1;
END_IF
 
This is what I figured out before seeing your responses. Let me know what you think. Basically if the ACK bit is the conditional variable.


Code:
TIMER_RETRIGGER_ALM0(NOT TIMER_RETRIGGER_ALM0.Q AND ALM0_ACK,T#2m);


IF TIMER_RETRIGGER_ALM0.Q = TRUE THEN
	 DATA_inputONOFFSMSText := '';
   	 DATA_inputONOFFSMSText := 'Alarm detected: Input 1 is ON';	
  	 UTIL_SEND_SMS_2( TRUE, CONFIG_slotID, CONFIG_receiverList, DATA_inputONOFFSMSText);
	 
END_IF;
 
TEST_VAR := TRUE; TIMER_RETRIGGER_ALM0(TEST_VAR,T#3m); (* after 3m Q will be high always. IF TIMER_RETRIGGER_ALM0.Q THEN TEST_VAR := FALSE; END_IF;
In codesys you can not wait (all plc cant)
but you can use an AND if timer not expired and input true then do your thing.
 

Similar Topics

Is it possible to automatically get the caller information, for example the file name and rung number making the call?
17
Replies
643
Views
I am working to get a stepper motor moving with this PLC. I have tried help from two 2 AI Agents, but just get run around in circles with no...
4
Replies
490
Views
Hey everyone, I’ve been experimenting with using AI to generate Structured Text for PLCs (mainly Codesys and Beckhoff), mostly because I got...
98
Replies
7,269
Views
Hi all, I'm using a micro 800 series PLC for PID control of furnace heating elements. I don't have PLC experience aside from reading through ABs...
10
Replies
1,108
Views
New to structure text, can you pls explain the difference between := and [:=]. Thanks,
3
Replies
738
Views
Top