Author Topic: ST coding and Initial Value of Var  (Read 2663 times)

Offline federico.furlani

  • PLUS+1 Novice
  • *
  • Posts: 4
  • Karma: +0/-0
ST coding and Initial Value of Var
« on: November 08, 2023, 10:46:39 AM »
Hi everybody,
I'm struggling with using the ST code on Guide. In particular, my problem of the day is to initialize a retain Var.
How can I access the 5 as Initial Value?

Thanks

Offline FluidPowerTom

  • PLUS+1 Guru
  • *****
  • Posts: 363
  • Karma: +33/-0
Re: ST coding and Initial Value of Var
« Reply #1 on: November 08, 2023, 05:18:57 PM »
I do this programmatically by having another variable function as a sort of default-setting flag:

FUNCTION_BLOCK BrakeLogic
VAR_INPUT
  BrakeTimerSetting2 : USINT;
  SetDefault : BOOL;
  SaveChanges : BOOL;
END_VAR
VAR_OUTPUT
  BrakeTimerSetting : USINT;
END_VAR
VAR
  startup : BOOL;
END_VAR
VAR RETAIN
  BrakeTimer : USINT;
  NVMem_Init : UDINT;
END_VAR

-----------------------------------------------------------------


IF SetDefault OR NVMem_Init<>666666 THEN
  BrakeTimerSetting:=0;
  BrakeTimer:=0;
  NVMem_Init:=666666;
END_IF

IF NVMem_Init=666666 AND NOT startup THEN
  BrakeTimerSetting:=BrakeTimer;
  startup:=1;
END_IF

IF SaveChanges AND NVMem_Init=666666 THEN
  BrakeTimer:=BrakeTimerSetting2;
  BrakeTimerSetting:=BrakeTimerSetting2;
END_IF


EDIT:  As you'll see in this case I'm using a variable named NVMem_Init for that, and we initialize the BrakeTimer as 0.
« Last Edit: November 08, 2023, 05:20:35 PM by FluidPowerTom »
Controls Engineer
Hydra-Power Systems