fg71
In this example I like to demonstrate how we could blink a LED with analog output. In the plc program I have included
FB_PulseAnalogSignal block in Structured text
 
Good value for Frequency is between 0 - 1, the HighValue could be used as brightness of LED.
In this example I have set pin 13 to the first Analog output.
 
Todo: Include Fade effect
 
[203128%2F9145117%2Fmceclip0]
 
[203128%2F9145120%2Fmceclip0]
 
(* Calculate the period of the pulse *)
Period := 1.0 / Frequency;

_TMP_OUT := (Period * DutyCycle * 0.01);
_TMP_REAL_TO_TIME0_OUT := REAL_TO_TIME(_TMP_OUT);
_TMP_REAL_TO_TIME1_OUT := REAL_TO_TIME(Period);

(* Calculate the duration of the high state based on the duty cycle *)
PulseDuration := _TMP_REAL_TO_TIME0_OUT;

{__SET_VAR(data__->,CURRENT_TIME,,__CURRENT_TIME)}

(* Increment the elapsed time *)
ElapsedTime := ElapsedTime + (CURRENT_TIME - LastTime);
LastTime := CURRENT_TIME;


IF ElapsedTime >= _TMP_REAL_TO_TIME1_OUT THEN
    (* Reset the elapsed time at the end of the period *)
    ElapsedTime := T#0s;
END_IF;

(* Generate the pulse signal *)
IF ElapsedTime <= PulseDuration THEN
    (* Set the pulse value to high during the high state *)
    PulseValue := HighValue;
ELSE
    (* Set the pulse value to low during the low state *)
    PulseValue := LowValue;
END_IF;
 
Reply 0 0
fg71
Here is update with Fade Effect from Low to High for LED pin 13
 
This settings give a nice Fade Effect IN/OUT
 
[203128%2F9145216%2Fmceclip0]
 
(* Calculate the period of the pulse *)
Period := 1.0 / Frequency;

_TMP_OUT := (Period * DutyCycle * 0.01);
_TMP_REAL_TO_TIME0_OUT := REAL_TO_TIME(_TMP_OUT);
_TMP_REAL_TO_TIME1_OUT := REAL_TO_TIME(Period);

(* Calculate the duration of the high state based on the duty cycle *)
PulseDuration := _TMP_REAL_TO_TIME0_OUT;

{__SET_VAR(data__->,CURRENT_TIME,,__CURRENT_TIME)}

(* Increment the elapsed time *)
ElapsedTime := ElapsedTime + (CURRENT_TIME - LastTime);
LastTime := CURRENT_TIME;


IF ElapsedTime >= _TMP_REAL_TO_TIME1_OUT THEN
    (* Reset the elapsed time at the end of the period *)
    ElapsedTime := T#0s;
END_IF;

(* Calculate the fade step *)
FadeStep := (HighValue - LowValue) / (TIME_TO_REAL(FadeDuration) / TIME_TO_REAL(T#10ms));

(* Apply fade effect *)
IF FadeDirection THEN
  FadeValue := FadeValue + FadeStep;
  IF FadeValue >= HighValue THEN
    FadeValue := HighValue;
    FadeDirection := FALSE; (* Start fading out *)
  END_IF;
ELSE
  FadeValue := FadeValue - FadeStep;
  IF FadeValue <= LowValue THEN
    FadeValue := LowValue;
    FadeDirection := TRUE; (* Start fading in *)
  END_IF;
END_IF;

(* Generate the pulse signal *)
IF ElapsedTime <= PulseDuration THEN
    (* Set the pulse value to high during the high state *)
    PulseValue := FadeValue;
ELSE
    (* Set the pulse value to low during the low state *)
    PulseValue := LowValue;
END_IF;
 
Reply 0 0
Reply