Intro:
Other discussions like this one get into some more advanced topics related to tables. However, for those just getting started with PAC Control, you might wonder–what are those for? How/when/why would I use one?
For you expert table users out there, feel free to read this example, keep me honest, and chime in with other options/suggestions and also try the little quiz at the end of this post!
Recommended prerequisites:
The three-part “Quick Start” series:
Part 1 - Hello World! (create a strategy & variable)
Part 2 - Adding basic Logic and a Loop
Part 3 - Debug Mode Basics
Also be sure to check out Chapters 4, 9, and 10 in the PAC Control [B]User’s Guide[/B] (form 1700), also available from within PAC Control under: Help > Manuals > User’s Guide.
String Table Example
Let’s say you’ve created a few charts and variables, but you’re starting to expand your system. You suspect you might have the need for a Table but you’re not sure the difference between, say, a “String Variable” and a “String Table” – and where/when to use one vs. the other.
Use case: Logging with a timestamp
Consider the “build time string” example shown in the Quick Start videos mentioned above. A string is initialized and expanded using a couple of string commands:
Suppose you want to store several strings like this, perhaps for troubleshooting the system, or logging what your HMI users are doing when. Instead of naming a whole bunch of individual strings, you can use a table since (as explained in Chapter 9 of the PAC Control User’s Guide):
“In a table variable, the variable name represents a group of values, not just one. Table variables are one-dimensional arrays, which means that each table is like a numbered list of values. You refer to each item in the list by its number, or index. Indexes start at 0, not at 1.”
For this example, I’ll add a String Table named stDailyLog. Notice the String Table configuration window looks very similar to the String window, with the addition of the Table Length, which I chose as 100 for this example, meaning this variable will hold 100 strings:
Notice for the String Table, I used the naming convention discussed in Chapter 11 of the User’s Guide. Also see more on naming conventions in Chapter 4, and form 2073 - Best Practices Tech Note.
Also notice for the string table, I’ve configured it as Persistent, meaning the values in that table will persist through a power cycle.
Since this is a “Daily Log,” I’ll have some logic to check if the day has just changed and if so, wipe out what’s in the current daily log to start fresh for a new day. A couple of commands can come in handy for doing that:
Note that using -1 for the “End Index” means “all elements.” I could’ve put a value of 99 here, but it’s better to use the -1 in case you change the length of this table in the future.
Side note on table lengths:
You could also get the length of the table by using the Get Length of Table command:
Just keep in mind that this will return 100 and since our table indices start with 0 rather than 1, you’d need to subtract 1 from the table length to get the highest element number that’s valid for this table.
Let’s look at an example of how this might be used in a chart:
Here’s what’s in that pink block where we build a string and put it in the appropriate String Table element:
Note that all this can be done in fewer lines of OptoScript, like this:
To test this logic, I ran it in the debugger, and changed my value for nHMI_button a few times, then watched it log:
Extra-bonus quiz for those of you working on your Opto 22 blackbelt:
QUIZ:
- Why did I put a delay in this loop?
- Which of the Numeric Variables shown in the strategy tree are persistent?
- Why do you suppose I chose those to be persistent?
- What will happen if I have more than 100 messages to log in one day?
- What might you do to avoid that problem?
- In the picture above where I added the string table stDailyLog, why is the “Initial Value” grayed out?
- If it had not been grayed out, and I put a value in there, where would that value go?
Extra bonus points if you answer:
What might you do to improve this chart?
Which of the Opto 22 Best Practices recommendations have I violated here?
Note: fabulous prizes in store for the first person to Private Message me all the correct answers, or the highest-scoring person in the next few days. To send a Private Message, right click on my username (mstjohn) just over my picture on the left there, then click “Private Message.”
Can’t wait to see your answers!
-OptoMary