How can I Loop through an array?

Here is a small snip of code:

if (ntZ001[0] == 3) then
    strDefrost_status[1] = "Pump Down";
  elseif (ntZ001[0] == 4) then
    strDefrost_status[1] = "Preheat";
  elseif (ntZ001[0] == 5) then
    strDefrost_status[1] = "Fan Stop";
  elseif (ntZ001[0] == 6) then
    strDefrost_status[1] = "Water On";
  elseif (ntZ001[0] == 7) then
    strDefrost_status[1] = "Drip";
  elseif (ntZ001[0] == 8) then
    strDefrost_status[1] = "Restart";
    strDefrost_status[1] = "";

I want to do this check several times without re-doing the code over and over. The only things that change are “ntZ001[0]” and “strDefrost_status[1]”

ntZ001[0] would change to ntZ002[0], ntZ003[0], etc.
strDefrost_status[1] would change to strDefrost_status[2], strDefrost_status[3], etc.

There are about a dozen zones I want to roll through and then start over. How would I go about doing that?

Any help is appreciated. Thank you!

Am I correct in assuming you are using OptoScript for this?
Do you have Node-RED or Ignition in this application that you could use for this loop?

It is optoscript. I suppose I could use node-red, but I would prefer to keep it all optoscript since it’s part of a much larger strategy running on an S1 processor.

I think it would be simple with a case statement and few pointer tables.
If I get some time today I’ll try it.


See if this is what you had in mind.

Test (16.5 KB)


@tjcrusher Thanks for posting your solution, I pulled it down and had a look, seems like a really elegant way to do this!

That’s great! Thank you.
I can’t seem to find documentation on the ‘while’ and ‘switch’ commands.

I just did something similar a few months ago using the routine that was posted a long time ago to put text to controller errors. I modified it to also log my own events that are written to a text file which is used by our field service techs to see what is going on and it’s location in the strategy. Customer descriptions don’t always match what actually happens.