Sysmac Studio Β· NJ/NX Β· IEC 61131-3

The Donut Box

One name. Many numbered slots. That's an array β€” the tidiest way to hold a row of the same thing on an Omron controller. Let's fill a box of donuts and watch the variables behave.

🍩 ARRAY[0..9] OF Donut ~10 min
01 Β· The problem

Ten donuts, ten lonely variables?

The messy way 😩

donut0 : BOOL; donut1 : BOOL; donut2 : BOOL; donut3 : BOOL; donut4 : BOOL; … donut9

Ten separate variables. Want to loop over them? You can't. Want an eleventh? Add another line by hand. This doesn't scale.

The array way ✨

donuts : ARRAY[0..9] OF BOOL;

One declaration. A box of 10 numbered slots, indices 0 to 9. Reach any slot by its number: donuts[3]. Loop over all of them with a FOR loop. That's the whole idea.

πŸ’‘ Sysmac note: Omron NJ/NX arrays start at index 0 by default β€” so ARRAY[0..9] gives you exactly 10 slots. You can pick any range you like ([1..10], [-5..5]), but 0-based is the house style.

02 Β· Play

Fill the box

Click a slot to select it, then store a donut or clear it. The Structured Text on the right is exactly what you'd type in Sysmac Studio to do the same thing.

Selected: none 0 / 10 filled

Live Structured Text

// click a slot…
donuts := box of 10 BOOLs
select a slot to inspect it
03 Β· Indexing

Reach in by number

The index in the square brackets is just a number β€” and it can be a variable. That's the superpower: change i and the same line of code reaches a different slot.

donuts[3]
pick an index and press Read

⚠️ Reach past the end β€” say donuts[10] β€” and Sysmac Studio flags an out-of-range access. The box has slots 0..9; there is no slot 10.

04 Β· The payoff

Count them with one loop

count := 0;
FOR i := 0 TO 9 DO
  IF donuts[i] THEN
    count := count + 1;
  END_IF;
END_FOR;

One loop walks every slot, no matter how big the box. Run it against the box you filled above:

press Run β€” counts the donuts you stored
05 Β· Check yourself

One quick question

In donuts : ARRAY[0..9] OF BOOL; β€” how many slots, and what's the last valid index?