TechTip: For-Next Loop in Weintek HMI Macro

In this TechTip, we’ll be looking at For-Next loops in the Weintek HMI macro language. It will be assumed that you’ve already got some basic knowledge of Weintek macros.

As in other macro TechTips we’ll start off by declaring some variables:

macro_command main()
// Declare variables
bool Lamp // Lamp is a bit (bool)
short i // i is the loop variable
short millisecs // the time for delay in mS
short loop //number of loops set using LW1

We’ll use GetData to get the values for DELAY from LW0 and for the number of loops from LW1 (we could have used constants instead if we wished to).

GetData(millisecs, “Local HMI”, LW, 0, 1) // Get the value in millisecs from LW0
GetData(loop, “Local HMI”, LW, 1, 1) //Get the Loop value from LW1 as our loop is 0 to value…. we’ll subtract 1 and put the result back into “loop”

loop = loop – 1 // Subtract 1 from the Loop value because we’re starting our loop from 0

This is our For – Next Loop:

for i = 0 to loop // loop by specified number of times
Lamp = 1 // set Lamp to 1 (ON)
SetData(Lamp, “Local HMI”, LB, 0, 1) // Write Lamp to HMI LB0
DELAY(millisecs) // wait
Lamp = 0 //set Lamp to 0 (OFF)
SetData(Lamp, “Local HMI”, LB, 0, 1) // Write Lamp to HMI LB0
DELAY(millisecs) // wait
next i // increment the loop and go round again.

end macro_command

Put it all together and we’ll have:

A demo project is available here for download.