# TechTip: Dynamic Limits and Tolerance Using a Macro on Weintek HMI

In this TechTip, we’re looking at using dynamic limits in numeric objects, alarm colours, and using a macro to calculate the limits based on a +/- tolerance.

In the demo program, the “Actual” value is in LW-0, the “Target” is in LW-2, “Tolerance” in LW-4 and LW-10 & LW-12 are Lower and Upper limits respectively.

The macro code deals with the case of a 0 target, and also negative target and negative limits:

``````
macro_command main()

float target = 0 // target value
float tolerance = 0 // tolerance
float percent = 0 // percentage tolerance
float upper = 0 // upper dynamic limit
float lower = 0 // lower dynamic limit

GetData(target, "Local HMI", LW, 2, 1) // get target value

GetData(percent, "Local HMI", LW, 4, 1) // get target value

target = target * 100

tolerance = (target / 100) * percent // target / 100 x percentage

if target == 0 then //  if target is 0... then
lower = (target + percent) // lower limit is target + percent
upper = (target - percent) // upper limit is target - percent
else // otherwise...
lower = (target - tolerance)/100 // lower limit is target - tolerance
upper = (target + tolerance)/100 // upper limit is target + tolerance
end if

if upper < 0 or lower < 0 then  // if either go negative
SetData(lower, "Local HMI", LW, 12, 1) // then swap upper/lower
SetData(upper, "Local HMI", LW, 10, 1)
else if target == 0 then // or if target 0 then normal upper lower
SetData(lower, "Local HMI", LW, 10, 1)
SetData(upper, "Local HMI", LW, 12, 1)
else // or if target positive then
SetData(lower, "Local HMI", LW, 10, 1) // normal
SetData(upper, "Local HMI", LW, 12, 1) //
end if

end macro_command

``````