<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.12">
  <POU Name="fbPulseDelay" Id="{a2eafd9b-e5e8-4663-850a-b038ce6f47d9}" SpecialFunc="None">
    <Declaration><![CDATA[FUNCTION_BLOCK fbPulseDelay
VAR_INPUT
	PulseIN : BOOL; // Pulse to delay
	DelayTime : TIME; // Delay Time in ms
	Tx : TIME; // System Time
END_VAR
VAR_OUTPUT
	PulseOut : BOOL; // Delayed Pulse
END_VAR
VAR
	bPulseRise : BOOL; // PulseIN rising edge
	DelaySP : TIME; // Stored DelayTime value to avoide changes during execution
	tStart : TIME;
	tStop : TIME;
	tPulseDuration : TIME; // Measured PulseIN duration
//	tPulseDurationLast : TIME; // Measured PulseIN duration
END_VAR
]]></Declaration>
    <Implementation>
      <ST><![CDATA[// Reset pulse rising edge bit

IF PulseIN AND NOT bPulseRise THEN
	tStart := GVL.tx; // On pulse rising edge record the System time
	DelaySP := DelayTime; // Write DelayTime value to avoide changes during execution
	tPulseDuration := TIME#0MS; // Reset Pulse duration counter
	bPulseRise := TRUE;
END_IF

IF PulseIN AND bPulseRise THEN
	tPulseDuration := GVL.tx - tStart; // Calculate the time (pulse duration)
END_IF

IF NOT PulseIN AND bPulseRise THEN // The first scan with PulseIN OFF
	;
END_IF

IF NOT PulseIn THEN // When PulseIN is OFF, reset pulse rising edge to be ready for the next pulse
	bPulseRise := FALSE;
END_IF

// Create the delayed pulse
// Start pulse with delay and stop with delay
//
IF GVL.tx >= (tStart + DelaySP) AND GVL.tx <= (tStart + DelaySP + tPulseDuration) THEN
	PulseOut := TRUE;
ELSE
	PulseOut := FALSE;	
END_IF

]]></ST>
    </Implementation>
    <LineIds Name="fbPulseDelay">
      <LineId Id="29" Count="0" />
      <LineId Id="20" Count="1" />
      <LineId Id="24" Count="0" />
      <LineId Id="74" Count="0" />
      <LineId Id="39" Count="0" />
      <LineId Id="22" Count="1" />
      <LineId Id="25" Count="2" />
      <LineId Id="19" Count="0" />
      <LineId Id="34" Count="0" />
      <LineId Id="36" Count="1" />
      <LineId Id="35" Count="0" />
      <LineId Id="31" Count="2" />
      <LineId Id="30" Count="0" />
      <LineId Id="54" Count="0" />
      <LineId Id="41" Count="0" />
      <LineId Id="55" Count="1" />
      <LineId Id="40" Count="0" />
      <LineId Id="42" Count="0" />
      <LineId Id="51" Count="1" />
      <LineId Id="43" Count="0" />
      <LineId Id="48" Count="0" />
      <LineId Id="50" Count="0" />
    </LineIds>
  </POU>
</TcPlcObject>