# Write Multiple Output Lines

Use the WriteDiscrete function Functions are tools that are available in Spreadsheet for processing and analyzing acquisitions or other results. You can add functions to your Spreadsheet job to create tool chains and produce results for specific applications. to write on multiple output lines, which can be non-contiguous.

## Details of the Example

For this example, we write on multiple, non-contiguous output lines. This means that you do not need two separate cells to write on OUT0 and INOUT3 if the output lines in between are not used. Consider the following for this example:

- An In-Sight 2800 vision system is used, which has 4 output lines and 3 output LEDs. Depending on which vision system you use, the number of output lines can be different, therefore, the maximum value allowed for the Number of Bits parameter can be lower.

- The example uses the first four output lines, indexed from 0 to 3:

- The bidirectional lines INOUT2 and INOUT3 are set to Output.

- Bits are written on the following lines:

  - OUT0, indexed 0

  - INOUT2, indexed 2

  - INOUT3, indexed 3

- Therefore, the parameters of WriteDiscrete are as follows:

  - Start Bit is 0.

  - Number of Bits is 4.

  - Value is 13.

## How to Calculate the Value Parameter

The Value parameter of the WriteDiscrete function is a decimal value. The written bits are represented as a binary number, which is then converted to a decimal value. The following table shows how the written bits can be represented by a binary value, then converted to individual decimal values:

Output Line | INOUT3 | INOUT2 | OUT1 | OUT0
--- | --- | --- | --- | ---
Index | 3 | 2 | 1 | 0
Power of two | 23 = 8 | 22 = 4 | 21 = 2 | 20 = 1
Written bit | 1 | 1 | 0 | 1
Decimal value of the binary value representation of the written bit | 8 | 4 | 0 | 1

Note: The index is calculated from the Start Bit. If you want to start from INOUT2, then the index of INOUT2 in this binary value representation is 0, not 2. The index shown in the Discrete I/O Settings window is not relevant here.

If you add the decimal values, you get the value you need to input for the Value parameter, in this case, it is 13. Add a WriteDiscrete function to a cell:

## Add Checkboxes to Modify the Written Bits Dynamically

You can use checkboxes to enable each bit individually with the CheckBox function. The advantage of this method is that you can test each line individually by enabling and disabling the checkboxes separately, without having to open the property sheet every time.

Cell Values for Dynamic Checkbox Example
Note: The following dropdown contains the Spreadsheet cell values used in this example. The cell values in this example start from cell A2. To copy the cell values into your own Spreadsheet job A job is a user-configured program in In-Sight Spreadsheet consisting of functions and designed for specific applications. You can create and edit jobs using the In-Sight Spreadsheet interface, and load them on a vision system for deployment in a live environment., copy and paste the value for each cell individually. If you add these cells to your job starting from a cell other than A2, adjust the cell references accordingly. You can also adjust the number of rows according to the number of output lines you have.

| A | B | C | D | E
--- | --- | --- | --- | --- | ---
2 | 'Index | 'Enable | 'Out bit | 'Decimal sum | 'WriteDiscrete
3 | 0 | CheckBox("OUT0") | Power(2,A3)*B3 | Sum(C3:C6) | WriteDiscrete($A$0,0,4,$D$3,0,0)
4 | 1 | CheckBox("OUT1") | Power(2,A4)*B4 |  |
5 | 2 | CheckBox("INOUT2") | Power(2,A5)*B5 |  |
6 | 3 | CheckBox("INOUT03") | Power(2,A6)*B6 |  |

To create a Spreadsheet job with checkboxes to enable writing bits on output lines individually:

1. Set the Signal Type to Programmed for each output line in the Discrete I/O Settings dialog. For more information, see Discrete I/O Settings.

2. Populate the A3 to A6 cells with indices from 0 to 3.

3. Add checkboxes with the corresponding name of the output lines from the B3 to B6 cells, for example the following formula in the B3 cell:

       CheckBox("OUT0")

   The checkbox returns a 0 or a 1 value, which you can use to enable or disable sending bits on individual output lines.

4. Add a formula to the C3 to C6 cells that calculates the individual decimal value based on the position of the written bit:

       Power(2,A3)*B3

   The Power function calculates which decimal value to use, so the Exp parameter needs to reference the index of the corresponding output line in the A column. By multiplying the result of the Power function with the result of the CheckBox function of the corresponding output line in the B column allows you to enable or disable the bits individually in the decimal value calculation. For example, the cell referencing the third bit returns 4 if the CheckBox function in the same row returns 1, and returns 0 if the CheckBox in the same row returns 0.

5. Add the values of C3 to C6 using the Sum function in a separate cell, for example, D3:

       Sum(C3:C6)

   This cell returns the decimal value that the Value parameter of the WriteDiscrete function uses.

6. Add the WriteDiscrete function to the E3 cell that references the sum in D3 as the Value:

7. Enable the checkboxes for the output lines to write from. In this example, enable INOUT2 and INOUT3 in B5 and B6 to make the Value property of the WriteDiscrete function 12: