TechTip: Calculating The Difference Between Two Values Using a Macro In a Weintek HMI

This TechTip is about using a macro for a simple calculation to determine the difference between 2 values. To calculate the difference between 2 values, first we use the “>=” operator to determine if value1 is greater than or equal to value2, if this is the case, our result is calculated value1 – value2. If value2 is greater than value1 then our result calculation is value2 – value1.

The macro is shown below:


macro_command main()

short value1 // value1 is a 16bit word
short value2 // value2 is a 16bit word
short result // result is a 16bit word

GetData(value1, "Local HMI", LW, 0, 1) // get value1 from HMI LW 0
GetData(value2, "Local HMI", LW, 1, 1) // get value2 from HMI LW 1

if value1 >= value2 then // if value 1 is greater than or equal to value2 then...
result = value1 - value2 // result = value1 - value2
else if value2 > value1 then // if value 2 is greater than value1 then...
result = value2 - value1 // result = value2 - value1
end if

SetData(result, "Local HMI", LW, 10, 1)	// put result in LW 10

end macro_command

As usual there’s a demo project available for you to try out using EasyBuilder Pro’s excellent simulator. Download the demo here.